77use Google \Protobuf \Duration ;
88use Illuminate \Queue \Events \JobProcessed ;
99use Illuminate \Queue \Events \JobProcessing ;
10+ use Illuminate \Queue \Events \JobReleasedAfterException ;
1011use Illuminate \Support \Facades \Event ;
1112use Illuminate \Support \Facades \Log ;
1213use Illuminate \Validation \ValidationException ;
@@ -239,13 +240,13 @@ public function after_max_attempts_it_will_log_to_failed_table()
239240 // Act & Assert
240241 $ this ->assertDatabaseCount ('failed_jobs ' , 0 );
241242
242- $ job ->run ();
243+ $ releasedJob = $ job ->runAndGetReleasedJob ();
243244 $ this ->assertDatabaseCount ('failed_jobs ' , 0 );
244245
245- $ job -> run ();
246+ $ releasedJob = $ releasedJob -> runAndGetReleasedJob ();
246247 $ this ->assertDatabaseCount ('failed_jobs ' , 0 );
247248
248- $ job ->run ();
249+ $ releasedJob ->run ();
249250 $ this ->assertDatabaseCount ('failed_jobs ' , 1 );
250251 }
251252
@@ -264,17 +265,17 @@ public function after_max_attempts_it_will_delete_the_task()
264265 $ job = $ this ->dispatch (new FailingJob ());
265266
266267 // Act & Assert
267- $ job ->run ();
268+ $ releasedJob = $ job ->runAndGetReleasedJob ();
268269 CloudTasksApi::assertDeletedTaskCount (1 );
269270 CloudTasksApi::assertTaskDeleted ($ job ->task ->getName ());
270271 $ this ->assertDatabaseCount ('failed_jobs ' , 0 );
271272
272- $ job -> run ();
273+ $ releasedJob = $ releasedJob -> runAndGetReleasedJob ();
273274 CloudTasksApi::assertDeletedTaskCount (2 );
274275 CloudTasksApi::assertTaskDeleted ($ job ->task ->getName ());
275276 $ this ->assertDatabaseCount ('failed_jobs ' , 0 );
276277
277- $ job ->run ();
278+ $ releasedJob ->run ();
278279 CloudTasksApi::assertDeletedTaskCount (3 );
279280 CloudTasksApi::assertTaskDeleted ($ job ->task ->getName ());
280281 $ this ->assertDatabaseCount ('failed_jobs ' , 1 );
@@ -294,7 +295,7 @@ public function after_max_retry_until_it_will_log_to_failed_table_and_delete_the
294295 $ job = $ this ->dispatch (new FailingJob ());
295296
296297 // Act
297- $ job ->run ();
298+ $ releasedJob = $ job ->runAndGetReleasedJob ();
298299
299300 // Assert
300301 CloudTasksApi::assertDeletedTaskCount (1 );
@@ -303,7 +304,7 @@ public function after_max_retry_until_it_will_log_to_failed_table_and_delete_the
303304
304305 // Act
305306 CloudTasksApi::partialMock ()->shouldReceive ('getRetryUntilTimestamp ' )->andReturn (1 );
306- $ job ->run ();
307+ $ releasedJob ->run ();
307308
308309 // Assert
309310 CloudTasksApi::assertDeletedTaskCount (2 );
@@ -353,15 +354,15 @@ public function test_max_attempts_in_combination_with_retry_until()
353354 $ job = $ this ->dispatch (new FailingJob ());
354355
355356 // Act & Assert
356- $ job ->run ();
357- $ job -> run ();
357+ $ releasedJob = $ job ->runAndGetReleasedJob ();
358+ $ releasedJob = $ releasedJob -> runAndGetReleasedJob ();
358359
359360 # After 2 attempts both Laravel versions should report the same: 2 errors and 0 failures.
360- $ task = StackkitCloudTask::whereTaskUuid ($ job ->payloadAsArray [ 'uuid ' ] )->firstOrFail ();
361+ $ task = StackkitCloudTask::whereTaskUuid ($ job ->payloadAsArray ( 'uuid ' ) )->firstOrFail ();
361362 $ this ->assertEquals (2 , $ task ->getNumberOfAttempts ());
362363 $ this ->assertEquals ('error ' , $ task ->status );
363364
364- $ job ->run ();
365+ $ releasedJob ->run ();
365366
366367 # Max attempts was reached
367368 # Laravel 5, 6, 7: fail because max attempts was reached
@@ -375,7 +376,7 @@ public function test_max_attempts_in_combination_with_retry_until()
375376 }
376377
377378 CloudTasksApi::shouldReceive ('getRetryUntilTimestamp ' )->andReturn (time () - 1 );
378- $ job ->run ();
379+ $ releasedJob ->run ();
379380
380381 $ this ->assertEquals ('failed ' , $ task ->fresh ()->status );
381382 }
@@ -400,19 +401,23 @@ public function it_can_handle_encrypted_jobs()
400401 // Assert
401402 $ this ->assertStringContainsString (
402403 'O:26:"Tests\Support\EncryptedJob" ' ,
403- decrypt ($ job ->payloadAsArray [ 'data ' ][ ' command '] ),
404+ decrypt ($ job ->payloadAsArray ( 'data. command ' ) ),
404405 );
405406
406407 Log::assertLogged ('EncryptedJob:success ' );
407408 }
408409
410+ /**
411+ * @test
412+ */
409413 public function failing_jobs_are_released ()
410414 {
411415 // Arrange
412416 OpenIdVerificator::fake ();
413417 CloudTasksApi::partialMock ()->shouldReceive ('getRetryConfig ' )->andReturn (
414418 (new RetryConfig ())->setMaxAttempts (3 )
415419 );
420+ Event::fake ([JobReleasedAfterException::class]);
416421
417422 // Act
418423 $ job = $ this ->dispatch (new FailingJob ());
@@ -426,5 +431,35 @@ public function failing_jobs_are_released()
426431 CloudTasksApi::assertDeletedTaskCount (1 );
427432 CloudTasksApi::assertCreatedTaskCount (2 );
428433 CloudTasksApi::assertTaskDeleted ($ job ->task ->getName ());
434+ Event::assertDispatched (JobReleasedAfterException::class, function ($ event ) {
435+ return $ event ->job ->attempts () === 1 ;
436+ });
437+ }
438+
439+ /**
440+ * @test
441+ */
442+ public function attempts_are_tracked_internally ()
443+ {
444+ // Arrange
445+ CloudTasksApi::fake ();
446+ OpenIdVerificator::fake ();
447+ Event::fake ([JobReleasedAfterException::class]);
448+
449+ // Act & Assert
450+ $ job = $ this ->dispatch (new FailingJob ());
451+ $ job ->run ();
452+ $ releasedJob = null ;
453+
454+ Event::assertDispatched (JobReleasedAfterException::class, function ($ event ) use (&$ releasedJob ) {
455+ $ releasedJob = $ event ->job ->getRawBody ();
456+ return $ event ->job ->attempts () === 1 ;
457+ });
458+
459+ $ this ->runFromPayload ($ releasedJob );
460+
461+ Event::assertDispatched (JobReleasedAfterException::class, function ($ event ) {
462+ return $ event ->job ->attempts () === 2 ;
463+ });
429464 }
430465}
0 commit comments