86 lines
3.0 KiB
PHP
86 lines
3.0 KiB
PHP
<?php
|
|
namespace ProVM\Common\Controller;
|
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
use Psr\Http\Message\ServerRequestInterface;
|
|
use ProVM\Common\Exception\Request\MissingArgument;
|
|
use ProVM\Common\Implement\Controller\Json;
|
|
use ProVM\Common\Service\Jobs as Service;
|
|
use function Safe\json_decode;
|
|
|
|
class Jobs
|
|
{
|
|
use Json;
|
|
|
|
public function __invoke(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
|
{
|
|
$jobs = $service->getRepository()->fetchAll();
|
|
return $this->withJson($response, compact('jobs'));
|
|
}
|
|
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
|
{
|
|
$body = $request->getBody();
|
|
$json = json_decode($body->getContents());
|
|
if (!isset($json->jobs)) {
|
|
throw new MissingArgument('jobs', 'array', 'job commands with arguments');
|
|
}
|
|
$output = [
|
|
'jobs' => $json->jobs,
|
|
'total' => count($json->jobs),
|
|
'scheduled' => 0
|
|
];
|
|
foreach ($json->jobs as $job) {
|
|
if ($service->queue($job->command, $job->arguments)) {
|
|
$output['scheduled'] ++;
|
|
}
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function pending(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
|
{
|
|
$pending = $service->getPending();
|
|
$output = [
|
|
'total' => count($pending),
|
|
'jobs' => $pending
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function pendingCommands(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
|
{
|
|
$body = $response->getBody();
|
|
$json = json_decode($body->getContents());
|
|
if (!isset($json->commands)) {
|
|
throw new MissingArgument('commands', 'array', 'job commands');
|
|
}
|
|
$output = [
|
|
'commands' => $json->commands,
|
|
'total' => count($json->commands),
|
|
'pending' => []
|
|
];
|
|
foreach ($json->commands as $command) {
|
|
$pending = $service->getPendingByCommand($command);
|
|
if (count($pending) === 0) {
|
|
continue;
|
|
}
|
|
$output['pending'][$command] = $pending;
|
|
}
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function finish(ServerRequestInterface $request, ResponseInterface $response, Service $service, $job_id): ResponseInterface
|
|
{
|
|
$output = [
|
|
'job_id' => $job_id,
|
|
'status' => $service->finish($job_id)
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
public function failed(ServerRequestInterface $request, ResponseInterface $response, Service $service, $job_id): ResponseInterface
|
|
{
|
|
$output = [
|
|
'job_id' => $job_id,
|
|
'status' => $service->failed($job_id)
|
|
];
|
|
return $this->withJson($response, $output);
|
|
}
|
|
}
|