1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TreeHouse\WorkerBundle\Command; |
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\Output\OutputInterface; |
9
|
|
|
use TreeHouse\WorkerBundle\QueueManager; |
10
|
|
|
use TreeHouse\WorkerBundle\WorkerEvents; |
11
|
|
|
|
12
|
|
|
class ExecuteCommand extends Command |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* @var QueueManager |
16
|
|
|
*/ |
17
|
|
|
protected $manager; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @param QueueManager $queueManager |
21
|
|
|
*/ |
22
|
|
|
public function __construct(QueueManager $queueManager) |
23
|
|
|
{ |
24
|
|
|
$this->manager = $queueManager; |
25
|
|
|
|
26
|
|
|
parent::__construct(); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @inheritdoc |
31
|
|
|
*/ |
32
|
|
|
protected function configure() |
33
|
|
|
{ |
34
|
|
|
$this |
35
|
|
|
->setName('worker:execute') |
36
|
|
|
->addArgument('action', InputArgument::REQUIRED, 'The action to execute') |
37
|
|
|
->addArgument('payload', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The payload to send to the action') |
38
|
|
|
->setDescription('Executes a single job with given payload. This doesn\'t use the regular queue') |
39
|
|
|
; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @inheritdoc |
44
|
|
|
*/ |
45
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
46
|
|
|
{ |
47
|
|
|
$action = $input->getArgument('action'); |
48
|
|
|
$payload = $input->getArgument('payload'); |
49
|
|
|
|
50
|
|
|
$output->writeln(sprintf('Performing action <info>%s</info> with payload <info>%s</info>', $action, json_encode($payload, JSON_UNESCAPED_SLASHES))); |
51
|
|
|
|
52
|
|
|
$timeStart = microtime(true) * 1000; |
53
|
|
|
$memStart = memory_get_usage(true); |
54
|
|
|
|
55
|
|
|
$result = $this->manager->execute($action, $payload); |
|
|
|
|
56
|
|
|
|
57
|
|
|
$duration = microtime(true) * 1000 - $timeStart; |
58
|
|
|
$usage = memory_get_usage(true) - $memStart; |
59
|
|
|
|
60
|
|
|
$output->writeln( |
61
|
|
|
sprintf( |
62
|
|
|
'Completed in <comment>%dms</comment> using <comment>%s</comment> with result: <info>%s</info>', |
63
|
|
|
$duration, |
64
|
|
|
$this->formatBytes($usage), |
65
|
|
|
json_encode($result, JSON_UNESCAPED_SLASHES) |
66
|
|
|
) |
67
|
|
|
); |
68
|
|
|
|
69
|
|
|
$this->manager->getDispatcher()->dispatch(WorkerEvents::FLUSH); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @param int $bytes |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
View Code Duplication |
private function formatBytes($bytes) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
$bytes = (int) $bytes; |
|
|
|
|
80
|
|
|
|
81
|
|
|
if ($bytes > 1024*1024) { |
82
|
|
|
return round($bytes/1024/1024, 2).'MB'; |
83
|
|
|
} elseif ($bytes > 1024) { |
84
|
|
|
return round($bytes/1024, 2).'KB'; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
return $bytes . 'B'; |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.