Skip to content

Commit 3ee2cd6

Browse files
committed
feat: implement add console command as arguments+name rather than together
1 parent 8bfc23a commit 3ee2cd6

14 files changed

+88
-103
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ install:
2525
- if [ "$deps" = "low" ]; then composer --prefer-lowest -n --prefer-stable --prefer-dist update; fi;
2626

2727

28-
script: bin/phpunit && bin/phpstan analyse -l 3 ./
28+
script: bin/phpunit && bin/phpstan analyse -l 5 ./
2929

3030
notifications:
3131

Command/AddCommandJobToQueueCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
*/
1515
class AddCommandJobToQueueCommand extends Command
1616
{
17+
protected static $defaultName = 'markup:job_queue:add:command';
18+
1719
/**
1820
* @var JobManager
1921
*/
@@ -31,7 +33,6 @@ public function __construct(JobManager $jobby)
3133
protected function configure()
3234
{
3335
$this
34-
->setName('markup:job_queue:add:command')
3536
->setDescription('Adds a single job that executes a command via the job queue')
3637
->addArgument(
3738
'cmd',
@@ -66,7 +67,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
6667
$timeout = intval($input->getOption('timeout'));
6768
$idleTimeout = intval($input->getOption('idle_timeout'));
6869

69-
$this->jobby->addCommandJob($command, $topic, $timeout, $idleTimeout);
70+
$this->jobby->addConsoleCommandJob($command, [], $topic, $timeout, $idleTimeout);
7071

7172
$output->writeln('<info>Added command to job queue</info>');
7273
}

Command/AddRecurringConsoleJobToQueueCommand.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,9 @@ private function addRecurringJobs(OutputInterface $output)
106106
continue;
107107
}
108108
}
109-
$this->jobManager->addCommandJob(
109+
$this->jobManager->addConsoleCommandJob(
110110
$configuration->getCommand(),
111+
[],
111112
$configuration->getTopic(),
112113
$configuration->getTimeout(),
113114
$configuration->getTimeout()
@@ -118,9 +119,8 @@ private function addRecurringJobs(OutputInterface $output)
118119
$configuration->getCommand(),
119120
$configuration->getTopic()
120121
);
121-
if ($configuration->nextRun()) {
122-
$message = sprintf('%s. Will next be added %s', $message, $configuration->nextRun()->format('r'));
123-
}
122+
123+
$message = sprintf('%s. Will next be added %s', $message, $configuration->nextRun()->format('r'));
124124
$output->writeln(sprintf('<info>%s</info>', $message));
125125
}
126126

Command/AddScheduleJobToQueueCommand.php

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,44 +2,80 @@
22

33
namespace Markup\JobQueueBundle\Command;
44

5-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
5+
use Markup\JobQueueBundle\Service\JobManager;
6+
use Markup\JobQueueBundle\Service\ScheduledJobService;
7+
use Psr\Log\LoggerInterface;
8+
use Symfony\Component\Console\Command\Command;
69
use Symfony\Component\Console\Input\InputInterface;
710
use Symfony\Component\Console\Output\OutputInterface;
811

912
/**
1013
* This command adds scheduled jobs to the job-queue
1114
*
1215
*/
13-
class AddScheduleJobToQueueCommand extends ContainerAwareCommand
16+
class AddScheduleJobToQueueCommand extends Command
1417
{
18+
protected static $defaultName = 'markup:scheduled_job:add';
19+
20+
/**
21+
* @var JobManager
22+
*/
23+
private $jobManager;
24+
25+
/**
26+
* @var LoggerInterface|null
27+
*/
28+
private $logger;
29+
30+
/**
31+
* @var ScheduledJobService
32+
*/
33+
private $scheduledJobService;
34+
35+
public function __construct(
36+
JobManager $jobManager,
37+
ScheduledJobService $scheduledJobService,
38+
?LoggerInterface $logger = null
39+
) {
40+
$this->jobManager = $jobManager;
41+
$this->logger = $logger;
42+
$this->scheduledJobService = $scheduledJobService;
43+
44+
parent::__construct(null);
45+
}
46+
1547
/**
1648
* @see Command
1749
*/
1850
protected function configure()
1951
{
2052
$this
21-
->setName('markup:scheduled_job:add')
2253
->setDescription('Adds scheduled jobs to the job-queue');
2354
}
2455

2556
protected function execute(InputInterface $input, OutputInterface $output)
2657
{
27-
$scheduledJobService = $this->getContainer()->get('markup_job_queue.scheduled');
28-
$logger = $this->getContainer()->get('logger');
29-
30-
if ($jobs = $scheduledJobService->getUnqueuedJobs()) {
58+
if ($jobs = $this->scheduledJobService->getUnqueuedJobs()) {
3159
foreach ($jobs as $job) {
3260
try {
33-
$this->getContainer()->get('jobby')->addCommandJob(
61+
$this->jobManager->addConsoleCommandJob(
3462
$job->getJob(),
63+
[],
3564
$job->getTopic(),
3665
3600,
3766
3600
3867
);
3968
$job->setQueued(true);
40-
$scheduledJobService->save($job, $flush = true);
41-
} catch(\Exception $e) {
42-
$logger->error(sprintf('There was an error adding the job "%s" to the job-queue, error: %s', $job->getJob(), $e->getMessage()));
69+
70+
$this->scheduledJobService->save($job, $flush = true);
71+
} catch (\Exception $e) {
72+
$this->logger->error(
73+
sprintf(
74+
'There was an error adding the job "%s" to the job-queue, error: %s',
75+
$job->getJob(),
76+
$e->getMessage()
77+
)
78+
);
4379
}
4480
}
4581
}

Command/AddTestJobCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ protected function configure()
5353
'quantity',
5454
InputArgument::OPTIONAL,
5555
'The number of times to add the job',
56-
1
56+
'1'
5757
)
5858
->addArgument(
5959
'topic',
@@ -86,7 +86,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
8686
break;
8787
}
8888

89-
$quantity = $input->getArgument('quantity');
89+
$quantity = intval($input->getArgument('quantity'));
9090
for ($i = 0; $i < $quantity; $i++) {
9191
$this->jobby->addJob($job);
9292
}

Exception/JobFailedException.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ class JobFailedException extends \Exception
1515
* @param string $message
1616
* @param null $exitCode
1717
* @param int $code
18-
* @param \Exception|null $previous
18+
* @param \Throwable|null $previous
1919
*/
2020
public function __construct(
2121
$message = "",
2222
$exitCode = null,
2323
$code = 0,
24-
\Exception $previous = null
24+
\Throwable $previous = null
2525
) {
2626
$this->exitCode = $exitCode;
2727
parent::__construct($message, $code, $previous);

Job/ConsoleCommandJob.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,11 @@ public function run(ParameterBagInterface $parameterBag): string
2424

2525
$command[] = $this->getConsolePath($parameterBag->get('markup_job_queue.console_dir'));
2626

27-
/**
28-
* This is less than ideal, but trying to support legacy and v3 and v4 symfony
29-
*/
30-
// If the command has been provided like `do:something "hello"` allow it through
31-
if (stripos($this->args['command'], '"') !== false) {
32-
$command[] = $this->args['command'];
33-
} else {
34-
// If the command has been provided like `do:something hello` split so the escaping is correct
35-
$command = array_merge($command, explode(' ', $this->args['command']));
36-
}
27+
$command[] = $this->args['command'];
3728

29+
foreach ($this->args['arguments'] as $argument) {
30+
$command[] = $argument;
31+
};
3832

3933
$uuid = isset($this->args['uuid']) ? $this->args['uuid']: null;
4034
if($uuid) {
@@ -84,16 +78,9 @@ public function run(ParameterBagInterface $parameterBag): string
8478
} catch (JobFailedException $e) {
8579
throw $e;
8680
} catch (RuntimeException $e) {
87-
// if process has been signalled then use the termSignal as the exit code
88-
try {
89-
$code = sprintf('SIGNAL %s', $process->getTermSignal());
90-
} catch(\Exception $e) {
91-
// RuntimeException may have been thrown for some other reason
92-
$code = 'UNKNOWN';
93-
}
94-
throw new JobFailedException($e->getMessage(), $code);
95-
} catch (\Exception $e) {
96-
throw new JobFailedException($e->getMessage());
81+
throw new JobFailedException($e->getMessage(), $process->getExitCode(), $e->getCode(), $e);
82+
} catch (\Throwable $e) {
83+
throw new JobFailedException($e->getMessage(), $process->getExitCode(), $e->getCode(), $e);
9784
}
9885
}
9986

Job/SleepJob.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public function validate()
3030
*/
3131
public function run(ParameterBagInterface $parameterBag): string
3232
{
33-
$process = new Process(sprintf('sleep %s', $this->args['time']));
33+
$process = new Process(['sleep', $this->args['time']]);
3434
$process->run();
3535

3636
return '';

Model/RecurringConsoleCommandConfiguration.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public function previousRun($time = 'now')
131131

132132
if ($cron->isDue($time)) {
133133
$now = new \DateTime();
134-
$now->setTime($now->format('H'), $now->format('i'));
134+
$now->setTime(intval($now->format('H')), intval($now->format('i')));
135135
return $now;
136136
}
137137
return $cron->getPreviousRunDate($time);

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,9 @@ Jobs can also be added directly. There is a utility method for adding 'command'
8181

8282
```php
8383
$container->get('jobby')
84-
->addCommandJob(
85-
'your:console:command --plus=any --options or arguments', #this needs to be a valid command
84+
->addConsoleCommandJob(
85+
'your:console:command',
86+
['--test']
8687
'a_valid_topic', # should be a valid topic name
8788
600, # allowed timeout for command (see symfony process component documentation)
8889
600, # allowed idle timeout for command (see symfony process component documentation)

0 commit comments

Comments
 (0)