Skip to content

Commit 440d844

Browse files
committed
read options and validate them
1 parent a2db811 commit 440d844

1 file changed

Lines changed: 57 additions & 13 deletions

File tree

src/Commands/QueueWork.php

Lines changed: 57 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,33 @@ public function run(array $params)
7979
$stopWhenEmpty = false;
8080
$waiting = false;
8181

82-
// Read params
82+
// Read queue name from params
8383
if (! $queue = array_shift($params)) {
8484
CLI::error('The queueName is not specified.');
8585

8686
return EXIT_ERROR;
8787
}
8888

8989
// Read options
90-
$sleep = $params['sleep'] ?? CLI::getOption('sleep') ?? 10;
91-
$rest = $params['rest'] ?? CLI::getOption('rest') ?? 0;
92-
$maxJobs = $params['max-jobs'] ?? CLI::getOption('max-jobs') ?? 0;
93-
$maxTime = $params['max-time'] ?? CLI::getOption('max-time') ?? 0;
94-
$memory = $params['memory'] ?? CLI::getOption('memory') ?? 128;
95-
$priority = $params['priority'] ?? CLI::getOption('priority') ?? $config->getQueuePriorities($queue) ?? 'default';
96-
$tries = $params['tries'] ?? CLI::getOption('tries');
97-
$tries = ($tries !== null) ? (int) $tries : $tries;
98-
$retryAfter = $params['retry-after'] ?? CLI::getOption('retry-after');
99-
$retryAfter = ($retryAfter !== null) ? (int) $retryAfter : $retryAfter;
100-
$countJobs = 0;
90+
[
91+
$error,
92+
$sleep,
93+
$rest,
94+
$maxJobs,
95+
$maxTime,
96+
$memory,
97+
$priority,
98+
$tries,
99+
$retryAfter
100+
] = $this->readOptions($params, $config, $queue);
101+
102+
if ($error !== null) {
103+
CLI::write($error, 'red');
104+
105+
return EXIT_ERROR;
106+
}
107+
108+
$countJobs = 0;
101109

102110
if (array_key_exists('stop-when-empty', $params) || CLI::getOption('stop-when-empty')) {
103111
$stopWhenEmpty = true;
@@ -113,7 +121,7 @@ public function run(array $params)
113121

114122
CLI::write(PHP_EOL);
115123

116-
$priority = array_map('trim', explode(',', $priority));
124+
$priority = array_map('trim', explode(',', (string) $priority));
117125

118126
while (true) {
119127
$work = service('queue')->pop($queue, $priority);
@@ -177,6 +185,42 @@ public function run(array $params)
177185
}
178186
}
179187

188+
private function readOptions(array $params, QueueConfig $config, string $queue): array
189+
{
190+
$options = [
191+
'error' => null,
192+
'sleep' => $params['sleep'] ?? CLI::getOption('sleep') ?? 10,
193+
'rest' => $params['rest'] ?? CLI::getOption('rest') ?? 0,
194+
'maxJobs' => $params['max-jobs'] ?? CLI::getOption('max-jobs') ?? 0,
195+
'maxTime' => $params['max-time'] ?? CLI::getOption('max-time') ?? 0,
196+
'memory' => $params['memory'] ?? CLI::getOption('memory') ?? 128,
197+
'priority' => $params['priority'] ?? CLI::getOption('priority') ?? $config->getQueuePriorities($queue) ?? 'default',
198+
'tries' => $params['tries'] ?? CLI::getOption('tries'),
199+
'retryAfter' => $params['retry-after'] ?? CLI::getOption('retry-after'),
200+
];
201+
202+
// Options that, being defined, cannot be `true`
203+
$keys = ['sleep', 'rest', 'maxJobs', 'maxTime', 'memory', 'priority', 'tries', 'retyAfter'];
204+
205+
foreach ($keys as $key) {
206+
if ($options[$key] === true) {
207+
$options['error'] = sprintf('Option: "-%s" must have a defined value.', $key);
208+
209+
return array_values($options);
210+
}
211+
}
212+
// Options that, being defined, have to be `int`
213+
$keys = array_diff($keys, ['priority']);
214+
215+
foreach ($keys as $key) {
216+
if ($options[$key] !== null && ! is_int($options[$key])) {
217+
$options[$key] = (int) $options[$key];
218+
}
219+
}
220+
221+
return array_values($options);
222+
}
223+
180224
private function handleWork(QueueJob $work, QueueConfig $config, ?int $tries, ?int $retryAfter): void
181225
{
182226
timer()->start('work');

0 commit comments

Comments
 (0)