File tree Expand file tree Collapse file tree 2 files changed +29
-0
lines changed
src/Illuminate/Foundation/Bus Expand file tree Collapse file tree 2 files changed +29
-0
lines changed Original file line number Diff line number Diff line change @@ -27,6 +27,13 @@ class PendingDispatch
27
27
*/
28
28
protected $ afterResponse = false ;
29
29
30
+ /**
31
+ * Indicates if the job should be dispatched to the queue after the current request.
32
+ *
33
+ * @var bool
34
+ */
35
+ protected $ defer = true ;
36
+
30
37
/**
31
38
* Create a new pending job dispatch.
32
39
*
@@ -163,6 +170,18 @@ public function afterResponse()
163
170
return $ this ;
164
171
}
165
172
173
+ /**
174
+ * Indicate that the job should be dispatched to the queue after the current request.
175
+ *
176
+ * @return $this
177
+ */
178
+ public function defer (): static
179
+ {
180
+ $ this ->defer = true ;
181
+
182
+ return $ this ;
183
+ }
184
+
166
185
/**
167
186
* Determine if the job should be dispatched.
168
187
*
@@ -217,6 +236,8 @@ public function __destruct()
217
236
return ;
218
237
} elseif ($ this ->afterResponse ) {
219
238
app (Dispatcher::class)->dispatchAfterResponse ($ this ->job );
239
+ } elseif ($ this ->defer ) {
240
+ defer (fn () => app (Dispatcher::class)->dispatch ($ this ->job ));
220
241
} else {
221
242
app (Dispatcher::class)->dispatch ($ this ->job );
222
243
}
Original file line number Diff line number Diff line change @@ -103,6 +103,14 @@ public function testAfterResponse()
103
103
);
104
104
}
105
105
106
+ public function testDefer ()
107
+ {
108
+ $ this ->pendingDispatch ->defer ();
109
+ $ this ->assertTrue (
110
+ (new ReflectionClass ($ this ->pendingDispatch ))->getProperty ('defer ' )->getValue ($ this ->pendingDispatch )
111
+ );
112
+ }
113
+
106
114
public function testGetJob ()
107
115
{
108
116
$ this ->assertSame ($ this ->job , $ this ->pendingDispatch ->getJob ());
You can’t perform that action at this time.
0 commit comments