1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\QueueBundle\Command; |
4
|
|
|
|
5
|
|
|
use Illuminate\Queue\Jobs\Job; |
6
|
|
|
use SfCod\QueueBundle\Failer\MongoFailedJobProvider; |
7
|
|
|
use SfCod\QueueBundle\Service\JobQueue; |
8
|
|
|
use SfCod\QueueBundle\Service\MongoDriverInterface; |
9
|
|
|
use Symfony\Component\Console\Command\Command; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Input\InputOption; |
12
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
13
|
|
|
use Symfony\Component\Console\Style\SymfonyStyle; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* Class ReleaseFailedCommand |
17
|
|
|
* |
18
|
|
|
* @author Virchenko Maksim <[email protected]> |
19
|
|
|
* |
20
|
|
|
* @package SfCod\QueueBundle\Command |
21
|
|
|
*/ |
22
|
|
|
class RetryCommand extends Command |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @var JobQueue |
26
|
|
|
*/ |
27
|
|
|
protected $queue; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var MongoFailedJobProvider |
31
|
|
|
*/ |
32
|
|
|
protected $failer; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* RetryCommand constructor. |
36
|
|
|
* |
37
|
|
|
* @param JobQueue $queue |
38
|
|
|
* @param MongoDriverInterface $mongoDriver |
|
|
|
|
39
|
|
|
*/ |
40
|
|
|
public function __construct(JobQueue $queue, MongoFailedJobProvider $failer) |
41
|
|
|
{ |
42
|
|
|
$this->queue = $queue; |
43
|
|
|
$this->failer = $failer; |
44
|
|
|
|
45
|
|
|
parent::__construct(); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Configure command |
50
|
|
|
*/ |
51
|
|
|
protected function configure() |
52
|
|
|
{ |
53
|
|
|
$this->setName('job-queue:retry') |
54
|
|
|
->setDescription('Release all failed jobs.') |
55
|
|
|
->addOption('id', null, InputOption::VALUE_OPTIONAL, 'Job id to retry', null); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Execute command |
60
|
|
|
* |
61
|
|
|
* @param InputInterface $input |
62
|
|
|
* @param OutputInterface $output |
63
|
|
|
* |
64
|
|
|
* @return int|null|void |
65
|
|
|
*/ |
66
|
|
|
public function execute(InputInterface $input, OutputInterface $output) |
67
|
|
|
{ |
68
|
|
|
$io = new SymfonyStyle($input, $output); |
69
|
|
|
|
70
|
|
|
$jobsCount = 0; |
71
|
|
|
if (is_null($input->getOption('id'))) { |
72
|
|
|
foreach ($this->failer->all() as $job) { |
73
|
|
|
if ($this->retryJob($job)) { |
74
|
|
|
++$jobsCount; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
} else { |
78
|
|
|
$job = $this->failer->find($input->getOption('id')); |
79
|
|
|
$this->retryJob($job); |
|
|
|
|
80
|
|
|
|
81
|
|
|
++$jobsCount; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$io->success(sprintf("[%d] job(s) has been released.\n", $jobsCount)); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Retry job |
89
|
|
|
* |
90
|
|
|
* @param Job $job |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
protected function retryJob($job): bool |
95
|
|
|
{ |
96
|
|
|
$payload = json_decode($job->payload, true); |
|
|
|
|
97
|
|
|
|
98
|
|
|
if ($payload && isset($payload['job'], $payload['data'])) { |
99
|
|
|
$this->queue->push($payload['job'], $payload['data']); |
100
|
|
|
$this->failer->forget($job->_id); |
|
|
|
|
101
|
|
|
|
102
|
|
|
return true; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return false; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.