-
Notifications
You must be signed in to change notification settings - Fork 38
Expand file tree
/
Copy pathCreateJobCommand.php
More file actions
270 lines (229 loc) · 7.55 KB
/
Copy pathCreateJobCommand.php
File metadata and controls
270 lines (229 loc) · 7.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<?php
namespace Dtc\QueueBundle\Command;
use Dtc\QueueBundle\Exception\WorkerNotRegisteredException;
use Dtc\QueueBundle\Manager\WorkerManager;
use Dtc\QueueBundle\Model\Job;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
class CreateJobCommand extends Command
{
/** @var WorkerManager */
private $workerManager;
protected function configure(): void
{
$this
->setName("dtc:queue:create_job")
->addOption(
'json-args',
'j',
InputOption::VALUE_NONE,
'Consume the args as a single JSON-encoded array'
)
->addOption(
'php-args',
'p',
InputOption::VALUE_NONE,
'Consume the args as a single PHP-serialized array'
)
->addOption( // For 5.0 this should become the default
'interpret-args',
null,
InputOption::VALUE_NONE,
'(beta) Make a best guess as to the type of the argument passed in (DEFAULT for future releases - also note the "interpretation" may change in upcoming releases).'
)
->addArgument(
'worker_name',
InputArgument::REQUIRED,
'Name of worker',
null
)
->addArgument(
'method',
InputArgument::REQUIRED,
'Method of worker to invoke',
null
)
->addArgument(
'args',
InputArgument::IS_ARRAY,
'Argument(s) for invoking worker method'
)
->setDescription('Create a job - for expert users')
->setHelp($this->getHelpMessage())
;
}
private function getHelpMessage()
{
$helpMessage = sprintf(
"%s --json-args %s %s '%s'".PHP_EOL,
$this->getName(), // command
'my-worker-name', // worker_name
'myMethod', // method
json_encode([ // args
'first parameter', // argv[0] (string)
null, // argv[1] (null)
3, // argv[2] (int)
[ // argv[3] (array)
'fourth',
'param',
'is',
'an',
'array',
],
])
);
$helpMessage .= PHP_EOL;
$helpMessage .= '';
return $helpMessage;
}
public function setWorkerManager($workerManager)
{
$this->workerManager = $workerManager;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$workerName = $input->getArgument('worker_name');
$methodName = $input->getArgument('method');
$args = $this->getArgs($input);
$worker = $this->workerManager->getWorker($workerName);
if (!$worker) {
throw new WorkerNotRegisteredException("Worker `{$workerName}` is not registered.");
}
$when = \Dtc\QueueBundle\Util\Util::getMicrotimeDateTime();
$batch = true;
$priority = 1;
$jobClass = $worker->getJobManager()->getJobClass();
/** @var Job $job */
$job = new $jobClass($worker, $batch, $priority, $when);
$job->setMethod($methodName);
$job->setArgs($args);
$worker->getJobManager()->save($job);
return $this::SUCCESS;
}
protected function getArgs(InputInterface $input)
{
$args = $input->getArgument('args');
$jsonArgs = $input->getOption('json-args');
$phpArgs = $input->getOption('php-args');
$interpretArgs = $input->getOption('interpret-args');
$this->validateJsonArgs($jsonArgs, $phpArgs, $interpretArgs);
if ($jsonArgs) {
return $this->decodeJsonArgs($args);
}
if ($phpArgs) {
return $this->decodePHPArgs($args);
}
if ($interpretArgs) {
return $this->interpretArgs($args);
}
return $args;
}
protected function validateJsonArgs($jsonArgs, $phpArgs, $interpretArgs)
{
if ($jsonArgs) {
if ($phpArgs || $interpretArgs) {
throw new \InvalidArgumentException('Should not have both JSON args plus another type of args');
}
}
$this->validatePHPArgs($phpArgs, $interpretArgs);
}
protected function validatePHPArgs($phpArgs, $interpretArgs)
{
if ($phpArgs) {
if ($interpretArgs) {
throw new \InvalidArgumentException('Should not have both PHP args plus another type of args');
}
}
}
protected function interpretArgs($args)
{
if (null === $args || 0 == count($args)) {
return $args;
}
$finalArgs = [];
foreach ($args as $arg) {
$finalArgs[] = $this->booleanInterpretation($arg);
}
return $finalArgs;
}
private function booleanInterpretation($arg)
{
if ('true' === $arg || 'TRUE' === $arg) {
return true;
}
if ('false' === $arg || 'FALSE' === $arg) {
return false;
}
return $this->integerInterpretation($arg);
}
private function integerInterpretation($arg)
{
if (ctype_digit($arg)) {
$intArg = intval($arg);
if (strval($intArg) === $arg) {
return $intArg;
}
// Must be a super-long number
return $arg;
}
return $this->floatInterpretation($arg);
}
private function floatInterpretation($arg)
{
if (is_numeric($arg)) {
$floatArg = floatval($arg);
return $floatArg;
}
return $this->nullInterpretation($arg);
}
private function nullInterpretation($arg)
{
if ('null' === $arg || 'NULL' === $arg) {
return null;
}
return $arg;
}
protected function decodeJsonArgs($jsonArgs)
{
if (1 !== count($jsonArgs)) {
throw new \InvalidArgumentException('args should be a single string containing a JSON-encoded array when using --json-args');
}
$args = json_decode($jsonArgs[0], true);
if (null === $args) {
throw new \InvalidArgumentException('unable to decode JSON-encoded args: '.$jsonArgs[0]);
}
return $this->testArgs('JSON', $args);
}
/**
* @param string $type
* @param array $args
*
* @return mixed
*/
protected function testArgs($type, $args)
{
if (!is_array($args)) {
throw new \InvalidArgumentException('unable to decode '.$type.'-encoded args into an array.');
}
if (array_values($args) !== $args) {
throw new \InvalidArgumentException('Expecting numerically-indexed array in '.$type.' arguments');
}
return $args;
}
/**
* @param string $phpArgs
*
* @return mixed
*/
protected function decodePHPArgs($phpArgs)
{
if (1 !== count($phpArgs)) {
throw new \InvalidArgumentException('args should be a single string containing a PHP-encoded array when using --php-args');
}
$args = unserialize($phpArgs[0], ['allowed_classes' => true]);
return $this->testArgs('PHP', $args);
}
}