-
Notifications
You must be signed in to change notification settings - Fork 4.8k
[12.x] Improve Queues and Horizon documentation #10596
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 12.x
Are you sure you want to change the base?
Changes from all commits
a7cc30e
52a1620
4c43417
96a4084
a57d763
db11c73
5f32368
0471e2c
cd779cc
17caa35
982ad73
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -577,6 +577,8 @@ public function middleware(): array | |
} | ||
``` | ||
|
||
Releasing an overlapping job back onto the queue will still increment the job's total number of attempts. You may wish to tune your `tries` and `maxExceptions` properties on your job class accordingly. For exemple, leaving the `tries` property to 1 as it is by default would prevent any overlapping job to be retried later. | ||
|
||
Any overlapping jobs of the same type will be released back to the queue. You may also specify the number of seconds that must elapse before the released job will be attempted again: | ||
|
||
```php | ||
|
@@ -1213,7 +1215,22 @@ class ProcessPodcast implements ShouldQueue | |
<a name="max-attempts"></a> | ||
#### Max Attempts | ||
|
||
If one of your queued jobs is encountering an error, you likely do not want it to keep retrying indefinitely. Therefore, Laravel provides various ways to specify how many times or for how long a job may be attempted. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This section was confusing to me, as it mixed errors and attempts, so I tried to explain as clearly as possible what an ‘attempt’ is. |
||
Job attempts are a core concept of Laravel's queue system and power many advanced features. While they may seem confusing at first, it's important to understand how they work before modifying the default configuration. | ||
|
||
When a job is dispatched, it is pushed onto the queue. A worker then picks it up and attempts to execute it, this is known as a job attempt. | ||
|
||
However, an attempt does not necessarily mean the job’s handle method was executed. Attempts can be consumed in several ways: | ||
|
||
- The job's `handle` method runs and completes without throwing an exception. | ||
- The job encounters an unhandled exception during execution. | ||
- The job timed out. | ||
- The job is manually released back to the queue using `$this->release()`. | ||
- Middleware such as `WithoutOverlapping` or `RateLimited` fails to acquire a lock and releases the job. | ||
|
||
You likely do not want it to keep retrying indefinitely a job. Therefore, Laravel provides various ways to specify how many times or for how long a job may be attempted. | ||
|
||
> [!NOTE] | ||
> By default, Laravel will only attempt a job once. If your job uses middleware like `WithoutOverlapping` or `RateLimited`, or if you're manually releasing jobs, you’ll likely need to increase the number of allowed attempts. | ||
One approach to specifying the maximum number of times a job may be attempted is via the `--tries` switch on the Artisan command line. This will apply to all jobs processed by the worker unless the job being processed specifies the number of times it may be attempted: | ||
|
||
|
@@ -1370,6 +1387,10 @@ If you would like to indicate that a job should be marked as [failed](#dealing-w | |
public $failOnTimeout = true; | ||
``` | ||
|
||
> [!NOTE] | ||
> By default, when a job times out, it consumes one attempt and is released back to the queue (if retries are allowed). | ||
> However, if you configure the job to fail on timeout, it will not be retried, regardless of the value set for tries. | ||
<a name="error-handling"></a> | ||
### Error Handling | ||
|
||
|
@@ -1417,6 +1438,9 @@ public function handle(): void | |
} | ||
``` | ||
|
||
> [!NOTE] | ||
> A job marked failed will never be retried. | ||
Comment on lines
+1441
to
+1443
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This might be redundant, but I added it for people who might not read the entire documentation as they should |
||
If you would like to mark your job as failed because of an exception that you have caught, you may pass the exception to the `fail` method. Or, for convenience, you may pass a string error message which will be converted to an exception for you: | ||
|
||
```php | ||
|
@@ -2249,6 +2273,18 @@ class ProcessPodcast implements ShouldQueue | |
> [!WARNING] | ||
> A new instance of the job is instantiated before invoking the `failed` method; therefore, any class property modifications that may have occurred within the `handle` method will be lost. | ||
A failed job is not necessarily one that encountered an unhandled exception. A job is considered failed when it has exhausted all of its allowed attempts. These attempts can be consumed in several ways: | ||
|
||
- The job encounters an unhandled exception during execution. | ||
- The job timed out. | ||
- The job is released back to the queue either manually or by a middleware. | ||
|
||
If the final attempt fails due to an exception thrown during job execution, that exception will be passed to the job’s failed method. | ||
|
||
However, if the job fails because it has reached the maximum number of allowed attempts, the `$exception` will be an instance of `Illuminate\Queue\MaxAttemptsExceededException`. | ||
|
||
Similarly, if the job fails due to exceeding the configured timeout, the `$exception` will be an instance of `Illuminate\Queue\TimeoutExceededException`. | ||
|
||
<a name="retrying-failed-jobs"></a> | ||
### Retrying Failed Jobs | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my testing these work with any balacing mode or at least also with
balance=false
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, that's true, but these options do not have the same meaning. To make more sense of it, I've added the
processes
key, which is an alias formaxProcesses