-
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
Open
QuentinGab
wants to merge
20
commits into
laravel:12.x
Choose a base branch
from
QuentinGab:queues-horizon
base: 12.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+263
−9
Open
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a7cc30e
qg/queues
QuentinGab 52a1620
qg/horizon
QuentinGab 4c43417
format
QuentinGab 96a4084
add documentation about TimeoutExceededException
QuentinGab a57d763
Clarify timeout doc
QuentinGab db11c73
rewrite strategies
QuentinGab 5f32368
typo
QuentinGab 0471e2c
format
QuentinGab cd779cc
format
QuentinGab 17caa35
simplify
QuentinGab 982ad73
simplify
QuentinGab a9aeeea
Correct False Strategy
QuentinGab f2eb5ff
use min/maxProcesses instead of processes
QuentinGab 433ec8b
rewrite simple strategy
QuentinGab 61476ee
typo
QuentinGab 4784ecc
simplify false
QuentinGab 2a739d1
reformulate
QuentinGab 5f34adb
reformulate
QuentinGab 8ce5cda
typo
QuentinGab 488f88f
remove useless comments
QuentinGab File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
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 | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
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.