You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
[12.x] Add documentation for remaining propagated queued listener options (#10633)
* Add max exceptions docs to queued listener
* Add docs for remaining properties
* Add parameters to inform developers of their availability
* Update events.md
* formatting
---------
Co-authored-by: Taylor Otwell <[email protected]>
-[Dispatching Events After Database Transactions](#dispatching-events-after-database-transactions)
@@ -455,6 +457,62 @@ class SendShipmentNotification implements ShouldQueueAfterCommit
455
457
> [!NOTE]
456
458
> To learn more about working around these issues, please review the documentation regarding [queued jobs and database transactions](/docs/{{version}}/queues#jobs-and-database-transactions).
457
459
460
+
<aname="queued-listener-middleware"></a>
461
+
### Queued Listener Middleware
462
+
463
+
Queued listeners can also utilize [job middleware](/docs/{{version}}/queues#job-middleware). Job middleware allow you to wrap custom logic around the execution of queued listeners, reducing boilerplate in the listeners themselves. After creating job middleware, they may be attached to a listener by returning them from the listener's `middleware` method:
464
+
465
+
```php
466
+
<?php
467
+
468
+
namespace App\Listeners;
469
+
470
+
use App\Events\OrderShipped;
471
+
use App\Jobs\Middleware\RateLimited;
472
+
use Illuminate\Contracts\Queue\ShouldQueue;
473
+
474
+
class SendShipmentNotification implements ShouldQueue
475
+
{
476
+
/**
477
+
* Handle the event.
478
+
*/
479
+
public function handle(OrderShipped $event): void
480
+
{
481
+
// Process the event...
482
+
}
483
+
484
+
/**
485
+
* Get the middleware the listener should pass through.
486
+
*
487
+
* @return array<int,object>
488
+
*/
489
+
public function middleware(OrderShipped $event): array
490
+
{
491
+
return [new RateLimited];
492
+
}
493
+
}
494
+
```
495
+
496
+
<aname="encrypted-queued-listeners"></a>
497
+
#### Encrypted Queued Listeners
498
+
499
+
Laravel allows you to ensure the privacy and integrity of a queued listener's data via [encryption](/docs/{{version}}/encryption). To get started, simply add the `ShouldBeEncrypted` interface to the listener class. Once this interface has been added to the class, Laravel will automatically encrypt your listener before pushing it onto a queue:
500
+
501
+
```php
502
+
<?php
503
+
504
+
namespace App\Listeners;
505
+
506
+
use App\Events\OrderShipped;
507
+
use Illuminate\Contracts\Queue\ShouldBeEncrypted;
508
+
use Illuminate\Contracts\Queue\ShouldQueue;
509
+
510
+
class SendShipmentNotification implements ShouldQueue, ShouldBeEncrypted
511
+
{
512
+
// ...
513
+
}
514
+
```
515
+
458
516
<aname="handling-failed-jobs"></a>
459
517
### Handling Failed Jobs
460
518
@@ -557,7 +615,7 @@ If you require more complex logic for determining the listeners's backoff time,
557
615
/**
558
616
* Calculate the number of seconds to wait before retrying the queued listener.
559
617
*/
560
-
public function backoff(): int
618
+
public function backoff(OrderShipped $event): int
561
619
{
562
620
return 3;
563
621
}
@@ -571,12 +629,101 @@ You may easily configure "exponential" backoffs by returning an array of backoff
571
629
*
572
630
* @return list<int>
573
631
*/
574
-
public function backoff(): array
632
+
public function backoff(OrderShipped $event): array
Sometimes you may wish to specify that a queued listener may be attempted many times, but should fail if the retries are triggered by a given number of unhandled exceptions (as opposed to being released by the `release` method directly). To accomplish this, you may define a `maxExceptions` property on your listener class:
642
+
643
+
```php
644
+
<?php
645
+
646
+
namespace App\Listeners;
647
+
648
+
use App\Events\OrderShipped;
649
+
use Illuminate\Contracts\Queue\ShouldQueue;
650
+
use Illuminate\Queue\InteractsWithQueue;
651
+
652
+
class SendShipmentNotification implements ShouldQueue
653
+
{
654
+
use InteractsWithQueue;
655
+
656
+
/**
657
+
* The number of times the queued listener may be attempted.
658
+
*
659
+
* @var int
660
+
*/
661
+
public $tries = 25;
662
+
663
+
/**
664
+
* The maximum number of unhandled exceptions to allow before failing.
665
+
*
666
+
* @var int
667
+
*/
668
+
public $maxExceptions = 3;
669
+
670
+
/**
671
+
* Handle the event.
672
+
*/
673
+
public function handle(OrderShipped $event): void
674
+
{
675
+
// Process the event...
676
+
}
677
+
}
678
+
```
679
+
680
+
In this example, the listener will be retried up to 25 times. However, the listener will fail if three unhandled exceptions are thrown by the listener.
681
+
682
+
<aname="specifying-queued-listener-timeout"></a>
683
+
#### Specifying Queued Listener Timeout
684
+
685
+
Often, you know roughly how long you expect your queued listeners to take. For this reason, Laravel allows you to specify a "timeout" value. If a listener is processing for longer than the number of seconds specified by the timeout value, the worker processing the listener will exit with an error. You may define the maximum number of seconds a listener should be allowed to run by defining a `$timeout` property on your listener class:
686
+
687
+
```php
688
+
<?php
689
+
690
+
namespace App\Listeners;
691
+
692
+
use App\Events\OrderShipped;
693
+
use Illuminate\Contracts\Queue\ShouldQueue;
694
+
695
+
class SendShipmentNotification implements ShouldQueue
696
+
{
697
+
/**
698
+
* The number of seconds the listener can run before timing out.
699
+
*
700
+
* @var int
701
+
*/
702
+
public $timeout = 120;
703
+
}
704
+
```
705
+
706
+
If you would like to indicate that a listener should be marked as failed on timeout, you may define the `$failOnTimeout` property on the listener class:
707
+
708
+
```php
709
+
<?php
710
+
711
+
namespace App\Listeners;
712
+
713
+
use App\Events\OrderShipped;
714
+
use Illuminate\Contracts\Queue\ShouldQueue;
715
+
716
+
class SendShipmentNotification implements ShouldQueue
717
+
{
718
+
/**
719
+
* Indicate if the listener should be marked as failed on timeout.
0 commit comments