Jobs setup
This commit is contained in:
@ -1,7 +1,6 @@
|
||||
<?php
|
||||
namespace ProVM\Common\Controller;
|
||||
|
||||
use ProVM\Emails\Model\Job;
|
||||
use Psr\Http\Message\ResponseInterface;
|
||||
use Psr\Http\Message\ServerRequestInterface;
|
||||
use ProVM\Common\Exception\Request\MissingArgument;
|
||||
@ -13,23 +12,25 @@ class Jobs
|
||||
{
|
||||
use Json;
|
||||
|
||||
public function schedule(ServerRequestInterface $request, ResponseInterface $response, Service $service, \ProVM\Common\Service\Messages $messagesService): ResponseInterface
|
||||
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->messages)) {
|
||||
throw new MissingArgument('messages', 'array', 'messages ids');
|
||||
if (!isset($json->jobs)) {
|
||||
throw new MissingArgument('jobs', 'array', 'job commands with arguments');
|
||||
}
|
||||
$output = [
|
||||
'messages' => $json->messages,
|
||||
'total' => count($json->messages),
|
||||
'jobs' => $json->jobs,
|
||||
'total' => count($json->jobs),
|
||||
'scheduled' => 0
|
||||
];
|
||||
foreach ($json->messages as $message_id) {
|
||||
if ($service->schedule($message_id)) {
|
||||
$message = $messagesService->getRepository()->fetchById($message_id);
|
||||
$message->doesHaveScheduledDownloads();
|
||||
$messagesService->getRepository()->save($message);
|
||||
foreach ($json->jobs as $job) {
|
||||
if ($service->queue($job->command, $job->arguments)) {
|
||||
$output['scheduled'] ++;
|
||||
}
|
||||
}
|
||||
@ -37,12 +38,47 @@ class Jobs
|
||||
}
|
||||
public function pending(ServerRequestInterface $request, ResponseInterface $response, Service $service): ResponseInterface
|
||||
{
|
||||
$pending = array_map(function(Job $job) {
|
||||
return $job->toArray();
|
||||
}, $service->getPending());
|
||||
$pending = $service->getPending();
|
||||
$output = [
|
||||
'total' => count($pending),
|
||||
'pending' => $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);
|
||||
}
|
||||
|
Reference in New Issue
Block a user