44
55namespace Larawelders \QueueEventLogger ;
66
7- use Closure ;
8- use Illuminate \Contracts \Queue \Job ;
9- use Illuminate \Queue \Events \JobExceptionOccurred ;
10- use Illuminate \Queue \Events \JobFailed ;
11- use Illuminate \Queue \Events \JobProcessed ;
12- use Illuminate \Queue \Events \JobProcessing ;
13- use Illuminate \Queue \Events \JobQueued ;
14- use Illuminate \Queue \Events \JobReleasedAfterException ;
15- use Illuminate \Queue \Events \JobTimedOut ;
16- use Illuminate \Queue \Events \Looping ;
17- use Illuminate \Queue \Events \QueueBusy ;
18- use Illuminate \Queue \Events \QueueFailedOver ;
19- use Illuminate \Queue \Events \QueuePaused ;
20- use Illuminate \Queue \Events \QueueResumed ;
21- use Illuminate \Queue \Events \WorkerStarting ;
22- use Illuminate \Queue \Events \WorkerStopping ;
237use Illuminate \Support \Facades \Event ;
24- use Illuminate \Support \Facades \Log ;
25- use Illuminate \Support \Facades \Queue ;
268use Illuminate \Support \ServiceProvider ;
279
2810class QueueEventLoggerServiceProvider extends ServiceProvider
@@ -38,126 +20,9 @@ public function boot(): void
3820 __DIR__ .'/../config/queue-event-logger.php ' => config_path ('queue-event-logger.php ' ),
3921 ], 'queue-event-logger-config ' );
4022
41- $ channelName = $ this ->registerDefaultLogChannel ();
23+ $ this ->registerDefaultLogChannel ();
4224
43- Queue::starting (static function (WorkerStarting $ event ) use ($ channelName ): void {
44- Log::channel ($ channelName )->info (
45- sprintf (
46- '[worker] Starting connection %s on queue %s ' ,
47- $ event ->connectionName ,
48- $ event ->queue
49- )
50- );
51- });
52-
53- Queue::before (static function (JobProcessing $ event ) use ($ channelName ): void {
54- Log::channel ($ channelName )->info (
55- self ::formatJobMessage ('Processing job ' , $ event ->job )
56- );
57- });
58-
59- Queue::after (static function (JobProcessed $ event ) use ($ channelName ): void {
60- Log::channel ($ channelName )->info (
61- self ::formatJobMessage ('Processed job ' , $ event ->job )
62- );
63- });
64-
65- Queue::exceptionOccurred (static function (JobExceptionOccurred $ event ) use ($ channelName ): void {
66- Log::channel ($ channelName )->error (
67- self ::formatExceptionMessage ('Uncaught exception ' , $ event ->job , $ event ->exception )
68- );
69- });
70-
71- Queue::looping (static function (?Looping $ event = null ) use ($ channelName ): void {
72- Log::channel ($ channelName )->debug ('[worker] Looping ' );
73- });
74-
75- Queue::failing (static function (JobFailed $ event ) use ($ channelName ): void {
76- Log::channel ($ channelName )->error (
77- self ::formatExceptionMessage ('Job failed ' , $ event ->job , $ event ->exception )
78- );
79- });
80-
81- Queue::stopping (static function (?WorkerStopping $ event = null ) use ($ channelName ): void {
82- Log::channel ($ channelName )->info (
83- sprintf (
84- '[worker] Stopping%s ' ,
85- $ event instanceof WorkerStopping ? sprintf (' with status %d ' , $ event ->status ) : ''
86- )
87- );
88- });
89-
90- Event::listen (JobQueued::class, static function (JobQueued $ event ) use ($ channelName ): void {
91- Log::channel ($ channelName )->info (
92- sprintf (
93- '[%s] Queued job %s on queue %s%s ' ,
94- self ::formatQueuedJobId ($ event ->id ),
95- self ::formatQueuedJobName ($ event ->job ),
96- is_string ($ event ->queue ) ? $ event ->queue : 'default ' ,
97- is_int ($ event ->delay ) ? sprintf (' with delay %d ' , $ event ->delay ) : ''
98- )
99- );
100- });
101-
102- Event::listen (JobReleasedAfterException::class, static function (JobReleasedAfterException $ event ) use ($ channelName ): void {
103- Log::channel ($ channelName )->warning (
104- sprintf (
105- '%s%s ' ,
106- self ::formatJobMessage ('Released job after exception ' , $ event ->job ),
107- is_int ($ event ->backoff ) ? sprintf (' with backoff %d ' , $ event ->backoff ) : ''
108- )
109- );
110- });
111-
112- Event::listen (JobTimedOut::class, static function (JobTimedOut $ event ) use ($ channelName ): void {
113- Log::channel ($ channelName )->error (
114- self ::formatJobMessage ('Timed out job ' , $ event ->job )
115- );
116- });
117-
118- Event::listen (QueueBusy::class, static function (QueueBusy $ event ) use ($ channelName ): void {
119- Log::channel ($ channelName )->warning (
120- sprintf (
121- '[worker] Queue %s on connection %s is busy with %d pending jobs ' ,
122- $ event ->queue ,
123- self ::formatQueueConnection ($ event , 'connectionName ' , 'connection ' ),
124- $ event ->size
125- )
126- );
127- });
128-
129- Event::listen (QueueFailedOver::class, static function (QueueFailedOver $ event ) use ($ channelName ): void {
130- Log::channel ($ channelName )->error (
131- sprintf (
132- '[worker] Queue failover from connection %s for job %s after %s: %s ' ,
133- is_string ($ event ->connectionName ) ? $ event ->connectionName : 'unknown ' ,
134- self ::formatQueuedJobName ($ event ->command ),
135- get_class ($ event ->exception ),
136- $ event ->exception ->getMessage ()
137- )
138- );
139- });
140-
141- Event::listen (QueuePaused::class, static function (QueuePaused $ event ) use ($ channelName ): void {
142- Log::channel ($ channelName )->info (
143- sprintf (
144- '[worker] Paused queue %s on connection %s%s ' ,
145- $ event ->queue ,
146- $ event ->connection ,
147- self ::formatOptionalPauseTtl ($ event ->ttl )
148- )
149- );
150- });
151-
152- Event::listen (QueueResumed::class, static function (QueueResumed $ event ) use ($ channelName ): void {
153- Log::channel ($ channelName )->info (
154- sprintf (
155- '[worker] Resumed queue %s on connection %s ' ,
156- $ event ->queue ,
157- $ event ->connection
158- )
159- );
160- });
25+ Event::subscribe (QueueEventLoggerSubscriber::class);
16126 }
16227
16328 private function registerDefaultLogChannel (): string
@@ -173,77 +38,4 @@ private function registerDefaultLogChannel(): string
17338
17439 return $ channelName ;
17540 }
176-
177- private static function formatJobMessage (string $ action , Job $ job ): string
178- {
179- return sprintf (
180- '[%s] %s %s ' ,
181- $ job ->getJobId (),
182- $ action ,
183- $ job ->resolveName ()
184- );
185- }
186-
187- private static function formatExceptionMessage (string $ action , Job $ job , \Throwable $ exception ): string
188- {
189- return sprintf (
190- '[%s] %s %s in job %s: %s ' ,
191- $ job ->getJobId (),
192- $ action ,
193- get_class ($ exception ),
194- $ job ->resolveName (),
195- $ exception ->getMessage ()
196- );
197- }
198-
199- private static function formatQueuedJobName (mixed $ job ): string
200- {
201- if ($ job instanceof Closure) {
202- return Closure::class;
203- }
204-
205- if (is_object ($ job )) {
206- return get_class ($ job );
207- }
208-
209- return (string ) $ job ;
210- }
211-
212- private static function formatQueuedJobId (mixed $ id ): string
213- {
214- return is_string ($ id ) || is_int ($ id ) ? (string ) $ id : 'pending ' ;
215- }
216-
217- private static function formatOptionalPauseTtl (mixed $ ttl ): string
218- {
219- if (! is_int ($ ttl ) && ! $ ttl instanceof \DateInterval && ! $ ttl instanceof \DateTimeInterface) {
220- return '' ;
221- }
222-
223- return sprintf (' for %s ' , self ::formatPauseTtl ($ ttl ));
224- }
225-
226- private static function formatPauseTtl (mixed $ ttl ): string
227- {
228- if ($ ttl instanceof \DateInterval) {
229- return $ ttl ->format ('P%yY%mM%dDT%hH%iM%sS ' );
230- }
231-
232- if ($ ttl instanceof \DateTimeInterface) {
233- return $ ttl ->format (DATE_ATOM );
234- }
235-
236- return (string ) $ ttl ;
237- }
238-
239- private static function formatQueueConnection (object $ event , string ...$ propertyNames ): string
240- {
241- foreach ($ propertyNames as $ propertyName ) {
242- if (property_exists ($ event , $ propertyName ) && is_string ($ event ->{$ propertyName })) {
243- return $ event ->{$ propertyName };
244- }
245- }
246-
247- return 'unknown ' ;
248- }
24941}
0 commit comments