1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Zenstruck\Queue\Bridge\Symfony\Console; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Command\Command; |
6
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
7
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
8
|
|
|
use Symfony\Component\Console\Input\InputOption; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Zenstruck\Queue\Subscriber; |
11
|
|
|
use Zenstruck\Queue\Subscriber\ExitStrategy\ChainExitStrategy; |
12
|
|
|
use Zenstruck\Queue\Subscriber\ExitStrategy\MaxCountExitStrategy; |
13
|
|
|
use Zenstruck\Queue\Subscriber\ExitStrategy\MemoryLimitExitStrategy; |
14
|
|
|
use Zenstruck\Queue\Subscriber\ExitStrategy\TimeoutExitStrategy; |
15
|
|
|
use Zenstruck\Queue\SubscriberRegistry; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @author Kevin Bond <[email protected]> |
19
|
|
|
*/ |
20
|
|
|
class SubscriberCommand extends Command |
21
|
|
|
{ |
22
|
|
|
private $subscriberRegistry; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param SubscriberRegistry $subscriberRegistry |
26
|
|
|
*/ |
27
|
5 |
|
public function __construct(SubscriberRegistry $subscriberRegistry) |
28
|
|
|
{ |
29
|
5 |
|
parent::__construct(); |
30
|
|
|
|
31
|
5 |
|
$this->subscriberRegistry = $subscriberRegistry; |
32
|
5 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
5 |
|
protected function configure() |
38
|
|
|
{ |
39
|
5 |
|
$this |
40
|
5 |
|
->setName('zenstruck:queue:subscribe') |
41
|
5 |
|
->setDescription('Subscribe to a registered queue') |
42
|
5 |
|
->addArgument('subscriber', InputArgument::OPTIONAL, 'The name of the subscriber to listen to') |
43
|
5 |
|
->addOption('max-attempts', null, InputOption::VALUE_REQUIRED, 'The number of times to attempt a job before marking as failed, 0 for unlimited', 50) |
44
|
5 |
|
->addOption('wait-time', null, InputOption::VALUE_REQUIRED, 'Time in seconds to wait before consuming another job') |
45
|
5 |
|
->addOption('max-jobs', null, InputOption::VALUE_REQUIRED, 'The number of jobs to consume before exiting') |
46
|
5 |
|
->addOption('timeout', null, InputOption::VALUE_REQUIRED, 'The number of seconds to consume jobs before exiting') |
47
|
5 |
|
->addOption('memory-limit', null, InputOption::VALUE_REQUIRED, 'The memory limit in MB - will exit if exceeded') |
48
|
|
|
; |
49
|
5 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
5 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
55
|
|
|
{ |
56
|
5 |
|
$subscriberName = $input->getArgument('subscriber'); |
57
|
5 |
|
$exitStrategy = new ChainExitStrategy(); |
58
|
|
|
|
59
|
5 |
|
if (!$subscriberName) { |
60
|
1 |
|
return $this->listSubscribers($output); |
61
|
|
|
} |
62
|
|
|
|
63
|
4 |
|
if (null !== $maxJobs = $input->getOption('max-jobs')) { |
64
|
2 |
|
$exitStrategy->addExitStrategy(new MaxCountExitStrategy($maxJobs)); |
65
|
2 |
|
} |
66
|
|
|
|
67
|
4 |
|
if (null !== $timeout = $input->getOption('timeout')) { |
68
|
2 |
|
$exitStrategy->addExitStrategy(new TimeoutExitStrategy($timeout)); |
69
|
2 |
|
} |
70
|
|
|
|
71
|
4 |
|
if (null !== $memoryLimit = $input->getOption('memory-limit')) { |
72
|
2 |
|
$exitStrategy->addExitStrategy(new MemoryLimitExitStrategy($memoryLimit)); |
73
|
2 |
|
} |
74
|
|
|
|
75
|
4 |
|
$subscriber = $this->subscriberRegistry->get($subscriberName); |
76
|
4 |
|
$reason = $subscriber->subscribe($exitStrategy, null, $input->getOption('wait-time'), $input->getOption('max-attempts')); |
77
|
4 |
|
$output->writeln(sprintf('<comment>Subscriber Exited</comment>: %s', $reason)); |
78
|
|
|
|
79
|
4 |
|
return 0; |
80
|
|
|
} |
81
|
|
|
|
82
|
1 |
|
private function listSubscribers(OutputInterface $output) |
83
|
|
|
{ |
84
|
1 |
|
$output->writeln('<info>Available Subscribers:</info>'); |
85
|
|
|
|
86
|
1 |
|
foreach (array_keys($this->subscriberRegistry->all()) as $name) { |
87
|
1 |
|
$output->writeln(sprintf(' - %s', $name)); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
1 |
|
return 0; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|