@@ -219,45 +219,19 @@ public function start(): self
219219 call_user_func_array ($ this ->workerStartHook ->getAction (), $ this ->getArguments ($ this ->workerStartHook ));
220220 }
221221
222- while (true ) {
223- $ this ->adapter ->consumer ->consume (
224- $ this ->adapter ->queue ,
225- function (Message $ message ) {
226- $ receivedAtTimestamp = microtime (true );
227- Console::info ("[Job] Received Job ( {$ message ->getPid ()}). " );
228- try {
229- $ waitDuration = microtime (true ) - $ message ->getTimestamp ();
230- $ this ->jobWaitTime ->record ($ waitDuration );
231-
232- $ this ->resources = [];
233- self ::setResource ('message ' , fn () => $ message );
234- if ($ this ->job ->getHook ()) {
235- foreach ($ this ->initHooks as $ hook ) { // Global init hooks
236- if (in_array ('* ' , $ hook ->getGroups ())) {
237- $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
238- \call_user_func_array ($ hook ->getAction (), $ arguments );
239- }
240- }
241- }
242-
243- foreach ($ this ->job ->getGroups () as $ group ) {
244- foreach ($ this ->initHooks as $ hook ) { // Group init hooks
245- if (in_array ($ group , $ hook ->getGroups ())) {
246- $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
247- \call_user_func_array ($ hook ->getAction (), $ arguments );
248- }
249- }
250- }
251-
252- \call_user_func_array ($ this ->job ->getAction (), $ this ->getArguments ($ this ->job , $ message ->getPayload ()));
253- } finally {
254- $ processDuration = microtime (true ) - $ receivedAtTimestamp ;
255- $ this ->processDuration ->record ($ processDuration );
256- }
257- },
258- function (Message $ message ) {
222+ $ this ->adapter ->consumer ->consume (
223+ $ this ->adapter ->queue ,
224+ function (Message $ message ) {
225+ $ receivedAtTimestamp = microtime (true );
226+ Console::info ("[Job] Received Job ( {$ message ->getPid ()}). " );
227+ try {
228+ $ waitDuration = microtime (true ) - $ message ->getTimestamp ();
229+ $ this ->jobWaitTime ->record ($ waitDuration );
230+
231+ $ this ->resources = [];
232+ self ::setResource ('message ' , fn () => $ message );
259233 if ($ this ->job ->getHook ()) {
260- foreach ($ this ->shutdownHooks as $ hook ) { // Global init hooks
234+ foreach ($ this ->initHooks as $ hook ) { // Global init hooks
261235 if (in_array ('* ' , $ hook ->getGroups ())) {
262236 $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
263237 \call_user_func_array ($ hook ->getAction (), $ arguments );
@@ -266,27 +240,51 @@ function (Message $message) {
266240 }
267241
268242 foreach ($ this ->job ->getGroups () as $ group ) {
269- foreach ($ this ->shutdownHooks as $ hook ) { // Group init hooks
243+ foreach ($ this ->initHooks as $ hook ) { // Group init hooks
270244 if (in_array ($ group , $ hook ->getGroups ())) {
271245 $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
272246 \call_user_func_array ($ hook ->getAction (), $ arguments );
273247 }
274248 }
275249 }
276250
277- Console::success ("[Job] ( {$ message ->getPid ()}) successfully run. " );
278- },
279- function (Message $ message , Throwable $ th ) {
280- Console::error ("[Job] ( {$ message ->getPid ()}) failed to run. " );
281- Console::error ("[Job] ( {$ message ->getPid ()}) {$ th ->getMessage ()}" );
251+ \call_user_func_array ($ this ->job ->getAction (), $ this ->getArguments ($ this ->job , $ message ->getPayload ()));
252+ } finally {
253+ $ processDuration = microtime (true ) - $ receivedAtTimestamp ;
254+ $ this ->processDuration ->record ($ processDuration );
255+ }
256+ },
257+ function (Message $ message ) {
258+ if ($ this ->job ->getHook ()) {
259+ foreach ($ this ->shutdownHooks as $ hook ) { // Global init hooks
260+ if (in_array ('* ' , $ hook ->getGroups ())) {
261+ $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
262+ \call_user_func_array ($ hook ->getAction (), $ arguments );
263+ }
264+ }
265+ }
282266
283- self ::setResource ('error ' , fn () => $ th );
284- foreach ($ this ->errorHooks as $ hook ) {
285- call_user_func_array ($ hook ->getAction (), $ this ->getArguments ($ hook ));
267+ foreach ($ this ->job ->getGroups () as $ group ) {
268+ foreach ($ this ->shutdownHooks as $ hook ) { // Group init hooks
269+ if (in_array ($ group , $ hook ->getGroups ())) {
270+ $ arguments = $ this ->getArguments ($ hook , $ message ->getPayload ());
271+ \call_user_func_array ($ hook ->getAction (), $ arguments );
272+ }
286273 }
287- },
288- );
289- }
274+ }
275+
276+ Console::success ("[Job] ( {$ message ->getPid ()}) successfully run. " );
277+ },
278+ function (Message $ message , Throwable $ th ) {
279+ Console::error ("[Job] ( {$ message ->getPid ()}) failed to run. " );
280+ Console::error ("[Job] ( {$ message ->getPid ()}) {$ th ->getMessage ()}" );
281+
282+ self ::setResource ('error ' , fn () => $ th );
283+ foreach ($ this ->errorHooks as $ hook ) {
284+ call_user_func_array ($ hook ->getAction (), $ this ->getArguments ($ hook ));
285+ }
286+ },
287+ );
290288 });
291289
292290 $ this ->adapter ->start ();
0 commit comments