Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to add delay in dispatch job in laravel?

I tried to do all of these:

Attempt 1:
DispatchWpAutoNewTaskJob::dispatch($payload)->delay(now()->addSeconds(10));

Attempt 2:
$when = now()->addSeconds(10); dispatch(new DispatchWpAutoNewTaskJob($payload))->later($when);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Attempt 3:
dispatch(new DispatchWpAutoNewTaskJob($payload))->delay(now()->addSeconds(10));

But it all returns an error similar to this:

[2023-10-25 20:13:09] local.ERROR: Call to undefined method App\Jobs\DispatchWpAutoNewTaskJob::delay() {"exception":"[object] (Error(code: 0): Call to undefined method App\\Jobs\\DispatchWpAutoNewTaskJob::delay() at /www/wwwroot/zz.com.test/vendor/laravel/framework/src/Illuminate/Foundation/Bus/PendingDispatch.php:98) [stacktrace] #0 /www/wwwroot/zz.com.test/app/Http/Controllers/Api/WpAutoWebhookController.php(63): Illuminate\\Foundation\\Bus\\PendingDispatch->delay() #1 /www/wwwroot/zz.com.test/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(54): App\\Http\\Controllers\\Api\\WpAutoWebhookController->handleWebhook()

Laravel version:
Laravel Framework 9.52.10

Attempt 1:
DispatchWpAutoNewTaskJob::dispatch($payload)->delay(now()->addSeconds(10));

Attempt 2:
$when = now()->addSeconds(10); dispatch(new DispatchWpAutoNewTaskJob($payload))->later($when);

Attempt 3:
dispatch(new DispatchWpAutoNewTaskJob($payload))->delay(now()->addSeconds(10));

I am expecting that the job will be dispatch with a delay and it should be non-blocking. I already setup the redis and it is working. My only problem is the delay.

Note that the codes inside the job are actually working, but without any delays. I wanna add non-blocking delay

>Solution :

Make sure that your job class uses the Illuminate\Bus\Queueable class.

Take a more in-depth look at the class structure of jobs from the Laravel 9.x documentation.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading