Skip to content

Commit 48e782c

Browse files
[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]>
1 parent 3fb881c commit 48e782c

File tree

1 file changed

+149
-2
lines changed

1 file changed

+149
-2
lines changed

events.md

Lines changed: 149 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
- [Queued Event Listeners](#queued-event-listeners)
1212
- [Manually Interacting With the Queue](#manually-interacting-with-the-queue)
1313
- [Queued Event Listeners and Database Transactions](#queued-event-listeners-and-database-transactions)
14+
- [Queued Listener Middleware](#queued-listener-middleware)
15+
- [Encrypted Queued Listeners](#encrypted-queued-listeners)
1416
- [Handling Failed Jobs](#handling-failed-jobs)
1517
- [Dispatching Events](#dispatching-events)
1618
- [Dispatching Events After Database Transactions](#dispatching-events-after-database-transactions)
@@ -455,6 +457,62 @@ class SendShipmentNotification implements ShouldQueueAfterCommit
455457
> [!NOTE]
456458
> 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).
457459
460+
<a name="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+
<a name="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+
458516
<a name="handling-failed-jobs"></a>
459517
### Handling Failed Jobs
460518

@@ -557,7 +615,7 @@ If you require more complex logic for determining the listeners's backoff time,
557615
/**
558616
* Calculate the number of seconds to wait before retrying the queued listener.
559617
*/
560-
public function backoff(): int
618+
public function backoff(OrderShipped $event): int
561619
{
562620
return 3;
563621
}
@@ -571,12 +629,101 @@ You may easily configure "exponential" backoffs by returning an array of backoff
571629
*
572630
* @return list<int>
573631
*/
574-
public function backoff(): array
632+
public function backoff(OrderShipped $event): array
575633
{
576634
return [1, 5, 10];
577635
}
578636
```
579637

638+
<a name="specifying-queued-listener-max-exceptions"></a>
639+
#### Specifying Queued Listener Max Exceptions
640+
641+
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+
<a name="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.
720+
*
721+
* @var bool
722+
*/
723+
public $failOnTimeout = true;
724+
}
725+
```
726+
580727
<a name="dispatching-events"></a>
581728
## Dispatching Events
582729

0 commit comments

Comments
 (0)