Skip to content

Commit 13e9403

Browse files
committed
refactor: make console commands work in symfony 4.4
1 parent 7bf0c51 commit 13e9403

19 files changed

+273
-149
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 2 ./
28+
script: bin/phpunit && bin/phpstan analyse -l 3 ./
2929

3030
notifications:
3131

Command/AddRecurringConsoleJobToQueueCommand.php

Lines changed: 54 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,11 @@
33
namespace Markup\JobQueueBundle\Command;
44

55
use Markup\JobQueueBundle\Model\RecurringConsoleCommandConfiguration;
6-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
6+
use Markup\JobQueueBundle\Repository\CronHealthRepository;
7+
use Markup\JobQueueBundle\Repository\JobLogRepository;
8+
use Markup\JobQueueBundle\Service\JobManager;
9+
use Markup\JobQueueBundle\Service\RecurringConsoleCommandReader;
10+
use Symfony\Component\Console\Command\Command;
711
use Symfony\Component\Console\Input\InputInterface;
812
use Symfony\Component\Console\Output\OutputInterface;
913

@@ -14,15 +18,57 @@
1418
*
1519
* This command should be run every minute via crontab
1620
*/
17-
class AddRecurringConsoleJobToQueueCommand extends ContainerAwareCommand
21+
class AddRecurringConsoleJobToQueueCommand extends Command
1822
{
23+
protected static $defaultName = 'markup:job_queue:recurring:add';
24+
25+
/**
26+
* @var RecurringConsoleCommandReader
27+
*/
28+
private $recurringConsoleCommandReader;
29+
30+
/**
31+
* @var JobManager
32+
*/
33+
private $jobManager;
34+
35+
/**
36+
* @var CronHealthRepository
37+
*/
38+
private $cronHealthRepository;
39+
40+
/**
41+
* @var JobLogRepository
42+
*/
43+
private $jobLogRepository;
44+
45+
/**
46+
* @var string
47+
*/
48+
private $environment;
49+
50+
public function __construct(
51+
RecurringConsoleCommandReader $recurringConsoleCommandReader,
52+
JobManager $jobManager,
53+
CronHealthRepository $cronHealthRepository,
54+
JobLogRepository $jobLogRepository,
55+
string $environment
56+
) {
57+
$this->recurringConsoleCommandReader = $recurringConsoleCommandReader;
58+
$this->jobManager = $jobManager;
59+
$this->cronHealthRepository = $cronHealthRepository;
60+
$this->jobLogRepository = $jobLogRepository;
61+
$this->environment = $environment;
62+
63+
parent::__construct(null);
64+
}
65+
1966
/**
2067
* @see Command
2168
*/
2269
protected function configure()
2370
{
2471
$this
25-
->setName('markup:job_queue:recurring:add')
2672
->setDescription('Adds any configured recurring jobs, which are due NOW, to the specified job queue');
2773
}
2874

@@ -40,17 +86,15 @@ protected function execute(InputInterface $input, OutputInterface $output)
4086
*/
4187
private function addRecurringJobs(OutputInterface $output)
4288
{
43-
$recurringConsoleCommandReader = $this->getContainer()->get('markup_job_queue.reader.recurring_console_command');
44-
45-
$due = $recurringConsoleCommandReader->getDue();
89+
$due = $this->recurringConsoleCommandReader->getDue();
4690

4791
foreach ($due as $configuration) {
4892
if (!$configuration instanceof RecurringConsoleCommandConfiguration) {
4993
throw new \Exception('Invalid configuration');
5094
}
5195

5296
if ($configuration->getEnvs()) {
53-
$env = $this->getContainer()->get('kernel')->getEnvironment();
97+
$env = $this->environment;
5498

5599
if (!in_array($env, $configuration->getEnvs())) {
56100
$output->writeln(
@@ -62,7 +106,7 @@ private function addRecurringJobs(OutputInterface $output)
62106
continue;
63107
}
64108
}
65-
$this->getContainer()->get('jobby')->addCommandJob(
109+
$this->jobManager->addCommandJob(
66110
$configuration->getCommand(),
67111
$configuration->getTopic(),
68112
$configuration->getTimeout(),
@@ -80,11 +124,11 @@ private function addRecurringJobs(OutputInterface $output)
80124
$output->writeln(sprintf('<info>%s</info>', $message));
81125
}
82126

83-
$this->getContainer()->get('markup_job_queue.repository.cron_health')->set();
127+
$this->cronHealthRepository->set();
84128
}
85129

86130
private function maintainJobLogs()
87131
{
88-
$this->getContainer()->get('markup_job_queue.repository.job_log')->removeExpiredJobs();
132+
$this->jobLogRepository->removeExpiredJobs();
89133
}
90134
}

Command/AddTestJobCommand.php

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44

55
use Markup\JobQueueBundle\Job\BadJob;
66
use Markup\JobQueueBundle\Job\ExceptionJob;
7-
use Markup\JobQueueBundle\Job\MonologErrorJob;
87
use Markup\JobQueueBundle\Job\SleepJob;
98
use Markup\JobQueueBundle\Job\WorkJob;
10-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
9+
use Markup\JobQueueBundle\Service\JobManager;
10+
use Symfony\Component\Console\Command\Command;
1111
use Symfony\Component\Console\Input\InputArgument;
1212
use Symfony\Component\Console\Input\InputInterface;
1313
use Symfony\Component\Console\Output\OutputInterface;
@@ -16,26 +16,38 @@
1616
* This command can add a variety of test jobs
1717
* Exists for testing and development purposes of the job queue
1818
*/
19-
class AddTestJobCommand extends ContainerAwareCommand
19+
class AddTestJobCommand extends Command
2020
{
21+
protected static $defaultName = 'markup:job_queue:add:test';
22+
2123
const TYPE_SLEEP = 'sleep';
2224
const TYPE_BAD = 'bad';
23-
const TYPE_ERROR = 'error';
2425
const TYPE_EXCEPTION = 'exception';
2526
const TYPE_WORK = 'work';
2627

28+
/**
29+
* @var JobManager
30+
*/
31+
private $jobby;
32+
33+
public function __construct(JobManager $jobby)
34+
{
35+
$this->jobby = $jobby;
36+
37+
parent::__construct();
38+
}
39+
2740
/**
2841
* @see Command
2942
*/
3043
protected function configure()
3144
{
3245
$this
33-
->setName('markup:job_queue:add:test')
3446
->setDescription('Adds a single job to allow testing of the job queue')
3547
->addArgument(
3648
'type',
3749
InputArgument::REQUIRED,
38-
'The type of job to add. Should be one of `sleep`, `bad` (fatal error), `error` (monolog error), `work` (cryptography) or `exception` (uncaught exception)'
50+
'The type of job to add. Should be one of `sleep`, `bad` (fatal error), `work` (cryptography) or `exception` (uncaught exception)'
3951
)
4052
->addArgument(
4153
'quantity',
@@ -63,9 +75,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
6375
case self::TYPE_BAD:
6476
$job = new BadJob([], $topic);
6577
break;
66-
case self::TYPE_ERROR:
67-
$job = new MonologErrorJob([], $topic);
68-
break;
6978
case self::TYPE_EXCEPTION:
7079
$job = new ExceptionJob([], $topic);
7180
break;
@@ -79,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
7988

8089
$quantity = $input->getArgument('quantity');
8190
for ($i = 0; $i < $quantity; $i++) {
82-
$this->getContainer()->get('jobby')->addJob($job);
91+
$this->jobby->addJob($job);
8392
}
8493
$output->writeln(sprintf('<info>Added %s job * %s</info>', $type, $quantity));
8594
}

Command/CheckRecurringJobConfigurationCommand.php

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,28 @@
44

55
use Markup\JobQueueBundle\Exception\InvalidConfigurationException;
66
use Markup\JobQueueBundle\Service\RecurringConsoleCommandReader;
7-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7+
use Symfony\Component\Console\Command\Command;
88
use Symfony\Component\Console\Input\InputInterface;
99
use Symfony\Component\Console\Output\OutputInterface;
1010

11-
class CheckRecurringJobConfigurationCommand extends ContainerAwareCommand
11+
class CheckRecurringJobConfigurationCommand extends Command
1212
{
13+
protected static $defaultName = 'markup:job_queue:recurring:check';
14+
15+
/**
16+
* @var RecurringConsoleCommandReader
17+
*/
18+
private $recurringConsoleCommandReader;
19+
20+
public function __construct(RecurringConsoleCommandReader $recurringConsoleCommandReader)
21+
{
22+
$this->recurringConsoleCommandReader = $recurringConsoleCommandReader;
23+
parent::__construct(null);
24+
}
25+
1326
protected function configure()
1427
{
1528
$this
16-
->setName('markup:job_queue:recurring:check')
1729
->setDescription('Checks the recurring job config files for validity.');
1830
}
1931

@@ -23,14 +35,12 @@ protected function configure()
2335
*/
2436
protected function execute(InputInterface $input, OutputInterface $output)
2537
{
26-
$reader = $this->getContainer()->get('markup_job_queue.reader.recurring_console_command');
27-
2838
$message = '';
2939
/**
3040
* @var RecurringConsoleCommandReader $reader
3141
*/
3242
try {
33-
$reader->getConfigurations();
43+
$this->recurringConsoleCommandReader->getConfigurations();
3444
$isGood = true;
3545
} catch (InvalidConfigurationException $e) {
3646
$isGood = false;

Command/ConsumerCommand.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
66
use PhpAmqpLib\Message\AMQPMessage;
7-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7+
use Symfony\Component\Console\Command\Command;
88
use Symfony\Component\Console\Input\InputArgument;
99
use Symfony\Component\Console\Input\InputInterface;
1010
use Symfony\Component\Console\Input\InputOption;
@@ -13,19 +13,31 @@
1313
/**
1414
* Consumes messages from the Go consumer
1515
*/
16-
class ConsumerCommand extends ContainerAwareCommand
16+
class ConsumerCommand extends Command
1717
{
18+
protected static $defaultName = 'markup:job_queue:consumer';
19+
1820
const STRICT_CODE_ACK = 0;
1921
const STRICT_CODE_REJECT = 3;
2022
const STRICT_CODE_REJECT_REQUEUE = 4;
2123
const STRICT_CODE_NEG_ACK = 5;
2224
const STRICT_CODE_NEG_ACK_REQUEUE = 6;
2325

26+
/**
27+
* @var ConsumerInterface
28+
*/
29+
private $consumer;
30+
31+
public function __construct(ConsumerInterface $consumer)
32+
{
33+
parent::__construct(null);
34+
$this->consumer = $consumer;
35+
}
36+
2437
protected function configure()
2538
{
2639
$this
2740
->addArgument('event', InputArgument::REQUIRED)
28-
->setName('markup:job_queue:consumer')
2941
->addOption(
3042
'strict-exit-code',
3143
null,
@@ -49,7 +61,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4961
}
5062

5163
$strict = $input->getOption('strict-exit-code');
52-
$consumerReturn = $this->getContainer()->get('markup_job_queue.consumer')->execute($message);
64+
$consumerReturn = $this->consumer->execute($message);
5365

5466
// if not running in strict mode - always acknowledge the message otherwise it will requeue forever
5567
if (!$strict) {

Command/RabbitMqConsumerCommand.php

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use OldSound\RabbitMqBundle\RabbitMq\ConsumerInterface;
66
use PhpAmqpLib\Message\AMQPMessage;
7-
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
7+
use Symfony\Component\Console\Command\Command;
88
use Symfony\Component\Console\Input\InputArgument;
99
use Symfony\Component\Console\Input\InputInterface;
1010
use Symfony\Component\Console\Input\InputOption;
@@ -13,19 +13,32 @@
1313
/**
1414
* Consumes messages from the Go consumer
1515
*/
16-
class RabbitMqConsumerCommand extends ContainerAwareCommand
16+
class RabbitMqConsumerCommand extends Command
1717
{
18+
protected static $defaultName = 'markup:job_queue:rabbitmq_consumer';
19+
1820
const STRICT_CODE_ACK = 0;
1921
const STRICT_CODE_REJECT = 3;
2022
const STRICT_CODE_REJECT_REQUEUE = 4;
2123
const STRICT_CODE_NEG_ACK = 5;
2224
const STRICT_CODE_NEG_ACK_REQUEUE = 6;
2325

26+
/**
27+
* @var ConsumerInterface|null
28+
*/
29+
private $consumer;
30+
31+
public function __construct(?ConsumerInterface $consumer = null)
32+
{
33+
$this->consumer = $consumer;
34+
35+
parent::__construct();
36+
}
37+
2438
protected function configure()
2539
{
2640
$this
2741
->addArgument('event', InputArgument::REQUIRED)
28-
->setName('markup:job_queue:rabbitmq_consumer')
2942
->addOption(
3043
'strict-exit-code',
3144
null,
@@ -39,18 +52,15 @@ protected function configure()
3952
*/
4053
protected function execute(InputInterface $input, OutputInterface $output)
4154
{
55+
if (!$this->consumer) {
56+
return 0;
57+
}
58+
4259
$data = json_decode(base64_decode($input->getArgument('event')), true);
4360

4461
$strict = $input->getOption('strict-exit-code');
4562

46-
/** @var ConsumerInterface $consumer */
47-
$consumer = $this->getContainer()->get('simple_bus.rabbit_mq_bundle_bridge.commands_consumer');
48-
49-
if (!$consumer instanceof ConsumerInterface) {
50-
return 0;
51-
}
52-
53-
$consumerReturn = $consumer->execute(new AMQPMessage($data['body'], $data['properties']));
63+
$consumerReturn = $this->consumer->execute(new AMQPMessage($data['body'], $data['properties']));
5464

5565
// if not running in strict mode - always acknowledge the message otherwise it will requeue forever
5666
if (!$strict) {

0 commit comments

Comments
 (0)