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
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @CommandAnnotation |
16
|
|
|
*/ |
17
|
|
|
class ExecuteJob extends Command |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var Worker |
22
|
|
|
*/ |
23
|
|
|
private $worker; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Worker $worker |
27
|
|
|
*/ |
28
|
3 |
|
public function __construct(Worker $worker) |
29
|
|
|
{ |
30
|
3 |
|
$this->worker = $worker; |
31
|
|
|
|
32
|
3 |
|
parent::__construct(null); |
33
|
3 |
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* {@inheritdoc} |
37
|
|
|
*/ |
38
|
3 |
|
protected function configure() |
39
|
|
|
{ |
40
|
|
|
$this |
41
|
3 |
|
->setName('messagequeue:execute') |
42
|
3 |
|
->setDescription('Runs message queue job') |
43
|
3 |
|
->addArgument('job', InputArgument::REQUIRED); |
44
|
3 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
2 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
50
|
|
|
{ |
51
|
2 |
|
$data = $input->getArgument('job'); |
52
|
2 |
|
if (strpos($data, '#') !== false) { |
53
|
2 |
|
list (, $data) = explode('#', $data, 2); |
54
|
|
|
} |
55
|
|
|
|
56
|
2 |
|
$raw = @base64_decode($data); |
57
|
2 |
|
$job = @unserialize($raw); |
58
|
|
|
|
59
|
2 |
|
if (!$job instanceof Job) { |
60
|
1 |
|
throw new Exception(sprintf('Invalid job: %s', $input->getArgument('job'))); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
$this->worker->executeJob($job); |
64
|
1 |
|
} |
65
|
|
|
} |
66
|
|
|
|