Dispatching/Delaying Jobs
You can use the below commands to dispatch the jobs from the controller methods.
/** Option 1 */
Queue::push(new MatchSendEmail($options));/** Option 2 */
dispatch(new MatchSendEmail($options));/** Option 3 */
(new MatchSendEmail($options))->dispatch();/** Option 4 */
\App\Jobs\MatchSendEmail::dispatch($options);
If you would like to dispatch a job conditionally, you may use the dispatchIf and dispatch unless methods.
MatchSendEmail::dispatchIf($accountActive === true, $options);
MatchSendEmail::dispatchUnless($accountSuspended === false, $options);
Jobs can be dispatched at specific queues also.
MatchSendEmail::dispatch($options)->onQueue(‘Email’);
You can also delay your Jobs to a given time.
MatchSendEmail::dispatch($options)->delay(now()->addMinutes(10));
We can also set tries and timeout properties to the job class, and the queue driver will use these values.
After Dispatching a job, you need to process this queue; for this, you have to start employment by using a straightforward command.
php artisan queue:work
You can also specify queue connection, the queue’s priorities, length of time a job can run, sleep duration between two job executions, process all posts, or a single job.
Laravel also provides facilities to deal with failed jobs. You can specify the number of times a job can be attempted before a job will be added in the failed job table using the “–tries=5” (number defines the attempts) command with the “queue:work” command when you run the queue worker. You can also specify the delay time for every effort after the job will be failed using “–delay=5” (number defines the seconds).
For example:
php artisan queue:work –tries=5 –delay=5
Comments
Post a Comment