77
88abstract class Job implements JobInterface
99{
10+ use InteractsWithQueueableAttributes;
11+
1012 /**
1113 * The number of times the job may be attempted.
1214 *
@@ -150,11 +152,19 @@ public function delayFor(int $delay): self
150152 /**
151153 * Dispatch the job to the queue.
152154 *
153- * @return string Job ID
155+ * @return string|null
154156 */
155- public function dispatch (): string
157+ public function dispatch (): ? string
156158 {
157- return Queue::push ($ this );
159+ $ this ->applyQueueableAttributes ();
160+
161+ if ($ this ->shouldQueue ()) {
162+ return Queue::push ($ this );
163+ }
164+
165+ $ this ->handle ();
166+
167+ return null ;
158168 }
159169
160170 /**
@@ -167,6 +177,8 @@ public function dispatchAfter(int $delay): string
167177 {
168178 $ this ->delayFor ($ delay );
169179
180+ $ this ->applyQueueableAttributes ();
181+
170182 return $ this ->dispatch ();
171183 }
172184
@@ -180,6 +192,8 @@ public function dispatchOn(string $queue): string
180192 {
181193 $ this ->onQueue ($ queue );
182194
195+ $ this ->applyQueueableAttributes ();
196+
183197 return $ this ->dispatch ();
184198 }
185199
@@ -195,4 +209,29 @@ public static function dispatchNow(...$args): string
195209
196210 return $ job ->dispatch ();
197211 }
212+
213+ /**
214+ * Dispatch the job synchronously
215+ *
216+ * @param mixed ...$args
217+ * @return void
218+ */
219+ public static function dispatchSync (...$ args ): void
220+ {
221+ $ job = new static (...$ args );
222+
223+ $ job ->handle ();
224+ }
225+
226+ /**
227+ * Force the job to be queued even without Queueable attribute.
228+ *
229+ * @return string Job ID
230+ */
231+ public function forceQueue (): string
232+ {
233+ $ this ->applyQueueableAttributes ();
234+
235+ return Queue::push ($ this );
236+ }
198237}
0 commit comments