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
@@ -183,12 +181,15 @@ Job classes are very simple, normally containing only a `handle` method that is
183
181
184
182
use App\Models\Podcast;
185
183
use App\Services\AudioProcessor;
184
+
use Illuminate\Bus\Queueable;
186
185
use Illuminate\Contracts\Queue\ShouldQueue;
187
-
use Illuminate\Foundation\Queue\Queueable;
186
+
use Illuminate\Foundation\Bus\Dispatchable;
187
+
use Illuminate\Queue\InteractsWithQueue;
188
+
use Illuminate\Queue\SerializesModels;
188
189
189
190
class ProcessPodcast implements ShouldQueue
190
191
{
191
-
use Queueable;
192
+
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
192
193
193
194
/**
194
195
* Create a new job instance.
@@ -206,7 +207,7 @@ Job classes are very simple, normally containing only a `handle` method that is
206
207
}
207
208
}
208
209
209
-
In this example, note that we were able to pass an [Eloquent model](/docs/{{version}}/eloquent) directly into the queued job's constructor. Because of the `Queueable` trait that the job is using, Eloquent models and their loaded relationships will be gracefully serialized and unserialized when the job is processing.
210
+
In this example, note that we were able to pass an [Eloquent model](/docs/{{version}}/eloquent) directly into the queued job's constructor. Because of the `SerializesModels` trait that the job is using, Eloquent models and their loaded relationships will be gracefully serialized and unserialized when the job is processing.
210
211
211
212
If your queued job accepts an Eloquent model in its constructor, only the identifier for the model will be serialized onto the queue. When the job is actually handled, the queue system will automatically re-retrieve the full model instance and its loaded relationships from the database. This approach to model serialization allows for much smaller job payloads to be sent to your queue driver.
212
213
@@ -238,9 +239,8 @@ Or, to prevent relations from being serialized, you can call the `withoutRelatio
238
239
/**
239
240
* Create a new job instance.
240
241
*/
241
-
public function __construct(
242
-
Podcast $podcast,
243
-
) {
242
+
public function __construct(Podcast $podcast)
243
+
{
244
244
$this->podcast = $podcast->withoutRelations();
245
245
}
246
246
@@ -253,8 +253,9 @@ If you are using PHP constructor property promotion and would like to indicate t
253
253
*/
254
254
public function __construct(
255
255
#[WithoutRelations]
256
-
public Podcast $podcast,
257
-
) {}
256
+
public Podcast $podcast
257
+
) {
258
+
}
258
259
259
260
If a job receives a collection or array of Eloquent models instead of a single model, the models within that collection will not have their relationships restored when the job is deserialized and executed. This is to prevent excessive resource usage on jobs that deal with large numbers of models.
260
261
@@ -574,6 +575,7 @@ class ProviderIsDown
574
575
{
575
576
// ...
576
577
578
+
577
579
public function middleware(): array
578
580
{
579
581
return [
@@ -586,6 +588,7 @@ class ProviderIsUp
586
588
{
587
589
// ...
588
590
591
+
589
592
public function middleware(): array
590
593
{
591
594
return [
@@ -612,18 +615,18 @@ For example, let's imagine a queued job that interacts with a third-party API th
612
615
*/
613
616
public function middleware(): array
614
617
{
615
-
return [new ThrottlesExceptions(10, 5 * 60)];
618
+
return [new ThrottlesExceptions(10, 5)];
616
619
}
617
620
618
621
/**
619
622
* Determine the time at which the job should timeout.
620
623
*/
621
624
public function retryUntil(): DateTime
622
625
{
623
-
return now()->addMinutes(30);
626
+
return now()->addMinutes(5);
624
627
}
625
628
626
-
The first constructor argument accepted by the middleware is the number of exceptions the job can throw before being throttled, while the second constructor argument is the number of seconds that should elapse before the job is attempted again once it has been throttled. In the code example above, if the job throws 10 consecutive exceptions, we will wait 5 minutes before attempting the job again, constrained by the 30-minute time limit.
629
+
The first constructor argument accepted by the middleware is the number of exceptions the job can throw before being throttled, while the second constructor argument is the number of minutes that should elapse before the job is attempted again once it has been throttled. In the code example above, if the job throws 10 exceptions within 5 minutes, we will wait 5 minutes before attempting the job again.
627
630
628
631
When a job throws an exception but the exception threshold has not yet been reached, the job will typically be retried immediately. However, you may specify the number of minutes such a job should be delayed by calling the `backoff` method when attaching the middleware to the job:
629
632
@@ -636,7 +639,7 @@ When a job throws an exception but the exception threshold has not yet been reac
Internally, this middleware uses Laravel's cache system to implement rate limiting, and the job's class name is utilized as the cache "key". You may override this key by calling the `by` method when attaching the middleware to your job. This may be useful if you have multiple jobs interacting with the same third-party service and you would like them to share a common throttling "bucket":
@@ -650,79 +653,12 @@ Internally, this middleware uses Laravel's cache system to implement rate limiti
By default, this middleware will throttle every exception. You can modify this behaviour by invoking the `when` method when attaching the middleware to your job. The exception will then only be throttled if closure provided to the `when` method returns `true`:
657
-
658
-
use Illuminate\Http\Client\HttpClientException;
659
-
use Illuminate\Queue\Middleware\ThrottlesExceptions;
If you would like to have the throttled exceptions reported to your application's exception handler, you can do so by invoking the `report` method when attaching the middleware to your job. Optionally, you may provide a closure to the `report` method and the exception will only be reported if the given closure returns `true`:
674
-
675
-
use Illuminate\Http\Client\HttpClientException;
676
-
use Illuminate\Queue\Middleware\ThrottlesExceptions;
> If you are using Redis, you may use the `Illuminate\Queue\Middleware\ThrottlesExceptionsWithRedis` middleware, which is fine-tuned for Redis and more efficient than the basic exception throttling middleware.
692
661
693
-
<aname="skipping-jobs"></a>
694
-
### Skipping Jobs
695
-
696
-
The `Skip` middleware allows you to specify that a job should be skipped / deleted without needing to modify the job's logic. The `Skip::when` method will delete the job if the given condition evaluates to `true`, while the `Skip::unless` method will delete the job if the condition evaluates to `false`:
697
-
698
-
use Illuminate\Queue\Middleware\Skip;
699
-
700
-
/**
701
-
* Get the middleware the job should pass through.
702
-
*/
703
-
public function middleware(): array
704
-
{
705
-
return [
706
-
Skip::when($someCondition),
707
-
];
708
-
}
709
-
710
-
You can also pass a `Closure` to the `when` and `unless` methods for more complex conditional evaluation:
711
-
712
-
use Illuminate\Queue\Middleware\Skip;
713
-
714
-
/**
715
-
* Get the middleware the job should pass through.
716
-
*/
717
-
public function middleware(): array
718
-
{
719
-
return [
720
-
Skip::when(function (): bool {
721
-
return $this->shouldSkip();
722
-
}),
723
-
];
724
-
}
725
-
726
662
<aname="dispatching-jobs"></a>
727
663
## Dispatching Jobs
728
664
@@ -796,10 +732,6 @@ If you would like to specify that a job should not be immediately available for
796
732
}
797
733
}
798
734
799
-
In some cases, jobs may have a default delay configured. If you need to bypass this delay and dispatch a job for immediate processing, you may use the `withoutDelay` method:
Occasionally, you may need to prepend or append a job to an existing job chain from within another job in that chain. You may accomplish this using the `prependToChain` and `appendToChain` methods:
933
-
934
-
```php
935
-
/**
936
-
* Execute the job.
937
-
*/
938
-
public function handle(): void
939
-
{
940
-
// ...
941
-
942
-
// Prepend to the current chain, run job immediately after current job...
943
-
$this->prependToChain(new TranscribePodcast);
944
-
945
-
// Append to the current chain, run job at end of chain...
946
-
$this->appendToChain(new TranscribePodcast);
947
-
}
948
-
```
949
-
950
861
<aname="chain-failures"></a>
951
862
#### Chain Failures
952
863
@@ -967,7 +878,7 @@ When chaining jobs, you may use the `catch` method to specify a closure that sho
967
878
> Since chain callbacks are serialized and executed at a later time by the Laravel queue, you should not use the `$this` variable within chain callbacks.
@@ -1650,7 +1570,7 @@ If you defined your DynamoDB table with a `ttl` attribute, you may define config
1650
1570
<aname="queueing-closures"></a>
1651
1571
## Queueing Closures
1652
1572
1653
-
Instead of dispatching a job class to the queue, you may also dispatch a closure. This is great for quick, simple tasks that need to be executed outside of the current request cycle. When dispatching closures to the queue, the closure's code content is cryptographically signed so that it cannot be modified in transit:
1573
+
Instead of dispatching a job class to the queue, you may also dispatch a closure. This is great for quick, simple tasks that need to be executed outside of the current request cycle. When dispatching closures to the queue, the closure's code content is cryptographically signed so that it can not be modified in transit:
1654
1574
1655
1575
$podcast = App\Podcast::find(1);
1656
1576
@@ -1957,13 +1877,15 @@ When a particular job fails, you may want to send an alert to your users or reve
1957
1877
1958
1878
use App\Models\Podcast;
1959
1879
use App\Services\AudioProcessor;
1880
+
use Illuminate\Bus\Queueable;
1960
1881
use Illuminate\Contracts\Queue\ShouldQueue;
1961
-
use Illuminate\Foundation\Queue\Queueable;
1882
+
use Illuminate\Queue\InteractsWithQueue;
1883
+
use Illuminate\Queue\SerializesModels;
1962
1884
use Throwable;
1963
1885
1964
1886
class ProcessPodcast implements ShouldQueue
1965
1887
{
1966
-
use Queueable;
1888
+
use InteractsWithQueue, Queueable, SerializesModels;
1967
1889
1968
1890
/**
1969
1891
* Create a new job instance.
@@ -2351,29 +2273,6 @@ You may use the `assertDispatchedWithoutChain` method to assert that a job was p
If a chained job [prepends or appends jobs to an existing chain](#adding-jobs-to-the-chain), you may use the job's `assertHasChain` method to assert that the job has the expected chain of remaining jobs:
2358
-
2359
-
```php
2360
-
$job = new ProcessPodcast;
2361
-
2362
-
$job->handle();
2363
-
2364
-
$job->assertHasChain([
2365
-
new TranscribePodcast,
2366
-
new OptimizePodcast,
2367
-
new ReleasePodcast,
2368
-
]);
2369
-
```
2370
-
2371
-
The `assertDoesntHaveChain` method may be used to assert that the job's remaining chain is empty:
2372
-
2373
-
```php
2374
-
$job->assertDoesntHaveChain();
2375
-
```
2376
-
2377
2276
<aname="testing-chained-batches"></a>
2378
2277
#### Testing Chained Batches
2379
2278
@@ -2434,10 +2333,9 @@ In addition, you may occasionally need to test an individual job's interaction w
2434
2333
2435
2334
Sometimes, you may need to test that a queued job [releases itself back onto the queue](#manually-releasing-a-job). Or, you may need to test that the job deleted itself. You may test these queue interactions by instantiating the job and invoking the `withFakeQueueInteractions` method.
2436
2335
2437
-
Once the job's queue interactions have been faked, you may invoke the `handle` method on the job. After invoking the job, the `assertReleased`, `assertDeleted`, `assertNotDeleted`, `assertFailed`, `assertFailedWith`, and `assertNotFailed` methods may be used to make assertions against the job's queue interactions:
2336
+
Once the job's queue interactions have been faked, you may invoke the `handle` method on the job. After invoking the job, the `assetReleased`, `assertDeleted`, and `assertFailed` methods may be used to make assertions against the job's queue interactions:
2438
2337
2439
2338
```php
2440
-
use App\Exceptions\CorruptedAudioException;
2441
2339
use App\Jobs\ProcessPodcast;
2442
2340
2443
2341
$job = (new ProcessPodcast)->withFakeQueueInteractions();
0 commit comments