1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BrainExe\Core\MessageQueue\Command; |
4
|
|
|
|
5
|
|
|
use BrainExe\Core\MessageQueue\Job; |
6
|
|
|
use BrainExe\Core\MessageQueue\Worker; |
7
|
|
|
use Exception; |
8
|
|
|
use Symfony\Component\Console\Command\Command; |
9
|
|
|
use Symfony\Component\Console\Input\InputArgument; |
10
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
11
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
12
|
|
|
use BrainExe\Core\Annotations\Command as CommandAnnotation; |
13
|
|
|
use BrainExe\Annotations\Annotations\Inject; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @CommandAnnotation("MessageQueue.Command.ExecuteJob") |
17
|
|
|
*/ |
18
|
|
|
class ExecuteJob extends Command |
19
|
|
|
{ |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @Inject("@MessageQueue.Worker") |
23
|
|
|
* @param Worker $worker |
24
|
|
|
*/ |
25
|
4 |
|
public function __construct(Worker $worker) |
26
|
|
|
{ |
27
|
4 |
|
$this->worker = $worker; |
|
|
|
|
28
|
|
|
|
29
|
4 |
|
parent::__construct(null); |
30
|
4 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* {@inheritdoc} |
34
|
|
|
*/ |
35
|
4 |
|
protected function configure() |
36
|
|
|
{ |
37
|
|
|
$this |
38
|
4 |
|
->setName('messagequeue:execute') |
39
|
4 |
|
->setDescription('Runs message queue job') |
40
|
4 |
|
->addArgument('job', InputArgument::REQUIRED); |
41
|
4 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* {@inheritdoc} |
45
|
|
|
*/ |
46
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
47
|
|
|
{ |
48
|
2 |
|
$data = $input->getArgument('job'); |
49
|
2 |
|
if (strpos($data, '#') !== false) { |
50
|
2 |
|
list (, $data) = explode('#', $data, 2); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
$raw = @base64_decode($data); |
54
|
2 |
|
$job = @unserialize($raw); |
55
|
|
|
|
56
|
2 |
|
if (!$job instanceof Job) { |
57
|
1 |
|
throw new Exception(sprintf('Invalid job: %s', $input->getArgument('job'))); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
$this->worker->executeJob($job); |
61
|
1 |
|
} |
62
|
|
|
} |
63
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: