@@ -217,19 +217,45 @@ public function start(): self
217217 call_user_func_array ($ this ->workerStartHook ->getAction (), $ this ->getArguments ($ this ->workerStartHook ));
218218 }
219219
220- $ this ->adapter ->consumer ->consume (
221- $ this ->adapter ->queue ,
222- function (Message $ message ) {
223- $ receivedAtTimestamp = microtime (true );
224- Console::info ("[Job] Received Job ( {$ message ->getPid ()}). " );
225- try {
226- $ waitDuration = microtime (true ) - $ message ->getTimestamp ();
227- $ this ->jobWaitTime ->record ($ waitDuration );
228-
229- $ this ->resources = [];
230- self ::setResource ('message ' , fn () => $ message );
220+ while (true ) {
221+ $ this ->adapter ->consumer ->consume (
222+ $ this ->adapter ->queue ,
223+ function (Message $ message ) {
224+ $ receivedAtTimestamp = microtime (true );
225+ Console::info ("[Job] Received Job ( {$ message ->getPid ()}). " );
226+ try {
227+ $ waitDuration = microtime (true ) - $ message ->getTimestamp ();
228+ $ this ->jobWaitTime ->record ($ waitDuration );
229+
230+ $ this ->resources = [];
231+ self ::setResource ('message ' , fn () => $ message );
232+ if ($ this ->job ->getHook ()) {
233+ foreach ($ this ->initHooks as $ hook ) { // Global init hooks
234+ if (in_array ('* ' , $ hook ->getGroups ())) {
235+ $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
236+ \call_user_func_array ($ hook ->getAction (), $ arguments );
237+ }
238+ }
239+ }
240+
241+ foreach ($ this ->job ->getGroups () as $ group ) {
242+ foreach ($ this ->initHooks as $ hook ) { // Group init hooks
243+ if (in_array ($ group , $ hook ->getGroups ())) {
244+ $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
245+ \call_user_func_array ($ hook ->getAction (), $ arguments );
246+ }
247+ }
248+ }
249+
250+ return \call_user_func_array ($ this ->job ->getAction (), $ this ->getArguments ($ this ->job , $ message ->getPayload ()));
251+ } finally {
252+ $ processDuration = microtime (true ) - $ receivedAtTimestamp ;
253+ $ this ->processDuration ->record ($ processDuration );
254+ }
255+ },
256+ function (Message $ message ) {
231257 if ($ this ->job ->getHook ()) {
232- foreach ($ this ->initHooks as $ hook ) { // Global init hooks
258+ foreach ($ this ->shutdownHooks as $ hook ) { // Global init hooks
233259 if (in_array ('* ' , $ hook ->getGroups ())) {
234260 $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
235261 \call_user_func_array ($ hook ->getAction (), $ arguments );
@@ -238,55 +264,29 @@ function (Message $message) {
238264 }
239265
240266 foreach ($ this ->job ->getGroups () as $ group ) {
241- foreach ($ this ->initHooks as $ hook ) { // Group init hooks
267+ foreach ($ this ->shutdownHooks as $ hook ) { // Group init hooks
242268 if (in_array ($ group , $ hook ->getGroups ())) {
243269 $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
244270 \call_user_func_array ($ hook ->getAction (), $ arguments );
245271 }
246272 }
247273 }
274+ Console::success ("[Job] ( {$ message ->getPid ()}) successfully run. " );
275+ },
276+ function (?Message $ message , Throwable $ th ) {
277+ Console::error ("[Job] ( {$ message ?->getPid()}) failed to run. " );
278+ Console::error ("[Job] ( {$ message ?->getPid()}) {$ th ->getMessage ()}" );
248279
249- return \call_user_func_array ($ this ->job ->getAction (), $ this ->getArguments ($ this ->job , $ message ->getPayload ()));
250- } finally {
251- $ processDuration = microtime (true ) - $ receivedAtTimestamp ;
252- $ this ->processDuration ->record ($ processDuration );
253- }
254- },
255- function (Message $ message ) {
256- if ($ this ->job ->getHook ()) {
257- foreach ($ this ->shutdownHooks as $ hook ) { // Global init hooks
258- if (in_array ('* ' , $ hook ->getGroups ())) {
259- $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
260- \call_user_func_array ($ hook ->getAction (), $ arguments );
261- }
262- }
263- }
280+ self ::setResource ('error ' , fn () => $ th );
264281
265- foreach ($ this ->job ->getGroups () as $ group ) {
266- foreach ($ this ->shutdownHooks as $ hook ) { // Group init hooks
267- if (in_array ($ group , $ hook ->getGroups ())) {
268- $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
269- \call_user_func_array ($ hook ->getAction (), $ arguments );
270- }
282+ foreach ($ this ->errorHooks as $ hook ) {
283+ ($ hook ->getAction ())(...$ this ->getArguments ($ hook ));
271284 }
272- }
273- Console::success ("[Job] ( {$ message ->getPid ()}) successfully run. " );
274- },
275- function (?Message $ message , Throwable $ th ) {
276- Console::error ("[Job] ( {$ message ?->getPid()}) failed to run. " );
277- Console::error ("[Job] ( {$ message ?->getPid()}) {$ th ->getMessage ()}" );
278-
279- self ::setResource ('error ' , fn () => $ th );
280-
281- foreach ($ this ->errorHooks as $ hook ) {
282- ($ hook ->getAction ())(...$ this ->getArguments ($ hook ));
283- }
284- },
285- );
285+ },
286+ );
287+ }
286288 });
287289
288- $ this ->adapter ->workerStop (fn () => $ this ->adapter ->consumer ->close ());
289-
290290 $ this ->adapter ->start ();
291291 } catch (Throwable $ error ) {
292292 self ::setResource ('error ' , fn () => $ error );
@@ -318,6 +318,31 @@ public function getWorkerStart(): Hook
318318 return $ this ->workerStartHook ;
319319 }
320320
321+ /**
322+ * Is called when a Worker stops.
323+ * @param callable|null $callback
324+ * @return self
325+ * @throws Exception
326+ */
327+ public function workerStop (?callable $ callback = null ): self
328+ {
329+ try {
330+ $ this ->adapter ->workerStop (function (string $ workerId ) use ($ callback ) {
331+ Console::success ("[Worker] Worker {$ workerId } is ready! " );
332+ if (!is_null ($ callback )) {
333+ call_user_func ($ callback );
334+ }
335+ });
336+ } catch (Throwable $ error ) {
337+ self ::setResource ('error ' , fn () => $ error );
338+ foreach ($ this ->errorHooks as $ hook ) {
339+ call_user_func_array ($ hook ->getAction (), $ this ->getArguments ($ hook ));
340+ }
341+ }
342+
343+ return $ this ;
344+ }
345+
321346 /**
322347 * Get Arguments
323348 *
0 commit comments