50 lines
1.6 KiB
PHP
50 lines
1.6 KiB
PHP
![]() |
<?php
|
||
|
namespace Incoviba\Command\Queue;
|
||
|
|
||
|
use Throwable;
|
||
|
use Symfony\Component\Console;
|
||
|
use Incoviba\Service;
|
||
|
|
||
|
#[Console\Attribute\AsCommand(name: 'queue:push', description: 'Push a job to the queue')]
|
||
|
class Push extends Console\Command\Command
|
||
|
{
|
||
|
public function __construct(protected Service\Job $jobService, ?string $name = null)
|
||
|
{
|
||
|
parent::__construct($name);
|
||
|
}
|
||
|
|
||
|
protected function configure(): void
|
||
|
{
|
||
|
$this->addOption('configurations', 'c', Console\Input\InputOption::VALUE_REQUIRED | Console\Input\InputOption::VALUE_IS_ARRAY, 'Job configuration, must be in valid JSON format');
|
||
|
}
|
||
|
|
||
|
protected function execute(Console\Input\InputInterface $input, Console\Output\OutputInterface $output): int
|
||
|
{
|
||
|
$io = new Console\Style\SymfonyStyle($input, $output);
|
||
|
$io->title("Pushing job");
|
||
|
|
||
|
$configurations = $input->getOption('configurations');
|
||
|
if ($configurations === null) {
|
||
|
$io->error('Missing configurations');
|
||
|
return self::FAILURE;
|
||
|
}
|
||
|
$result = self::SUCCESS;
|
||
|
foreach ($configurations as $configuration) {
|
||
|
if (!json_validate($configuration)) {
|
||
|
$io->error("Invalid JSON: {$configuration}");
|
||
|
continue;
|
||
|
}
|
||
|
$configuration = json_decode($configuration, true);
|
||
|
|
||
|
try {
|
||
|
$job = $this->jobService->push($configuration);
|
||
|
$io->success("Job pushed with ID {$job['id']}");
|
||
|
} catch (Throwable $exception) {
|
||
|
$io->error($exception->getMessage());
|
||
|
$result = self::FAILURE;
|
||
|
}
|
||
|
}
|
||
|
return $result;
|
||
|
}
|
||
|
}
|