@@ -79,23 +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- $ retryAfter = $ params ['retry-after ' ] ?? CLI ::getOption ('retry-after ' );
98- $ 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 ;
99109
100110 if (array_key_exists ('stop-when-empty ' , $ params ) || CLI ::getOption ('stop-when-empty ' )) {
101111 $ stopWhenEmpty = true ;
@@ -111,7 +121,7 @@ public function run(array $params)
111121
112122 CLI ::write (PHP_EOL );
113123
114- $ priority = array_map ('trim ' , explode (', ' , $ priority ));
124+ $ priority = array_map ('trim ' , explode (', ' , ( string ) $ priority ));
115125
116126 while (true ) {
117127 $ work = service ('queue ' )->pop ($ queue , $ priority );
@@ -175,6 +185,42 @@ public function run(array $params)
175185 }
176186 }
177187
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+
178224 private function handleWork (QueueJob $ work , QueueConfig $ config , ?int $ tries , ?int $ retryAfter ): void
179225 {
180226 timer ()->start ('work ' );
@@ -190,7 +236,7 @@ private function handleWork(QueueJob $work, QueueConfig $config, ?int $tries, ?i
190236
191237 CLI ::write ('The processing of this job was successful ' , 'green ' );
192238 } catch (Throwable $ err ) {
193- if (isset ($ job ) && ++$ work ->attempts < $ tries ?? $ job ->getTries ()) {
239+ if (isset ($ job ) && ++$ work ->attempts < ( $ tries ?? $ job ->getTries () )) {
194240 // Schedule for later
195241 service ('queue ' )->later ($ work , $ retryAfter ?? $ job ->getRetryAfter ());
196242 } else {
0 commit comments