File tree 2 files changed +69
-0
lines changed 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change @@ -51,6 +51,13 @@ class Dispatcher implements QueueingDispatcher
51
51
*/
52
52
protected $ queueResolver ;
53
53
54
+ /**
55
+ * Indicates if dispatching after response is disabled.
56
+ *
57
+ * @var bool
58
+ */
59
+ protected $ allowsDispatchingAfterResponses = true ;
60
+
54
61
/**
55
62
* Create a new command dispatcher instance.
56
63
*
@@ -252,6 +259,12 @@ protected function pushCommandToQueue($queue, $command)
252
259
*/
253
260
public function dispatchAfterResponse ($ command , $ handler = null )
254
261
{
262
+ if (! $ this ->allowsDispatchingAfterResponses ) {
263
+ $ this ->dispatchSync ($ command );
264
+
265
+ return ;
266
+ }
267
+
255
268
$ this ->container ->terminating (function () use ($ command , $ handler ) {
256
269
$ this ->dispatchSync ($ command , $ handler );
257
270
});
@@ -282,4 +295,28 @@ public function map(array $map)
282
295
283
296
return $ this ;
284
297
}
298
+
299
+ /**
300
+ * Allow dispatching after responses.
301
+ *
302
+ * @return $this
303
+ */
304
+ public function withDispatchingAfterResponses ()
305
+ {
306
+ $ this ->allowsDispatchingAfterResponses = true ;
307
+
308
+ return $ this ;
309
+ }
310
+
311
+ /**
312
+ * Disable dispatching after responses.
313
+ *
314
+ * @return $this
315
+ */
316
+ public function withoutDispatchingAfterResponses ()
317
+ {
318
+ $ this ->allowsDispatchingAfterResponses = false ;
319
+
320
+ return $ this ;
321
+ }
285
322
}
Original file line number Diff line number Diff line change 10
10
use Illuminate \Queue \Events \JobQueued ;
11
11
use Illuminate \Queue \Events \JobQueueing ;
12
12
use Illuminate \Queue \InteractsWithQueue ;
13
+ use Illuminate \Support \Facades \Bus ;
13
14
use Illuminate \Support \Facades \Config ;
14
15
use Orchestra \Testbench \Attributes \WithMigration ;
15
16
@@ -165,6 +166,37 @@ public function testQueueMayBeNullForJobQueueingAndJobQueuedEvent()
165
166
$ this ->assertNull ($ events [3 ]->queue );
166
167
}
167
168
169
+ public function testCanDisableDispatchingAfterResponse ()
170
+ {
171
+ Job::dispatchAfterResponse ('test ' );
172
+
173
+ $ this ->assertFalse (Job::$ ran );
174
+
175
+ $ this ->app ->terminate ();
176
+
177
+ $ this ->assertTrue (Job::$ ran );
178
+
179
+ Bus::withoutDispatchingAfterResponses ();
180
+
181
+ Job::$ ran = false ;
182
+ Job::dispatchAfterResponse ('test ' );
183
+
184
+ $ this ->assertTrue (Job::$ ran );
185
+
186
+ $ this ->app ->terminate ();
187
+
188
+ Bus::withDispatchingAfterResponses ();
189
+
190
+ Job::$ ran = false ;
191
+ Job::dispatchAfterResponse ('test ' );
192
+
193
+ $ this ->assertFalse (Job::$ ran );
194
+
195
+ $ this ->app ->terminate ();
196
+
197
+ $ this ->assertTrue (Job::$ ran );
198
+ }
199
+
168
200
/**
169
201
* Helpers.
170
202
*/
You can’t perform that action at this time.
0 commit comments