karateka's avatar

Queue delay not working

Hi, I have stupid problem with queue. I dispatch job using this code:

dispatch((new \App\Jobs\TestJob())->delay(\Carbon\Carbon::now()->addMinutes(5)));

Job is working but dispatch working immediate, not waiting 5 minutes. I was trying other method e.g Queue::dispach but every time delay not working.

At delay time queue:work give me log:

[2021-01-07 17:00:49][114] Processing: App\Jobs\TestJob [2021-01-07 17:00:49][114] Processed: App\Jobs\TestJob

QUEUE_CONNECTION is beanstalkd

0 likes
11 replies
karateka's avatar

Still no working. I was trying use database queue driver and delay was working but job dispatching give me SQL errors about deadlock when select in jobs table

sr57's avatar

I use db driver without pb, can you show (log) errors about deadlock?

karateka's avatar

It's crazy. Later, errors was like:

Deadlock found when trying to get lock; try restarting transaction (SQL: select * from 'jobs' where 'reserved_at' = 7405457) OR ...

Now, I switch QUEUE_CONNECTION to database and I don't have any errors but jobs work immediate. New rows are added to the jobs table and disappear when delay time. I can't understand this.

rossque's avatar

I've had similar issues with ->delay() method running straight away, instead of delayed. To resolve the issue, I first declare the Carbon instance as a variable (without the ->addMinutes(5))

This doesn't work for me (fires jobs straight away)

TestJob::dispatch()->delay(now()->addMinutes(5));

This works ....

$dateTime = Carbon::now();
TestJob::dispatch()->delay($dateTime->addMinutes(5));
Osahady's avatar

Me too delay on the dispatched job didn't work instead it processed the jobs immediately without any delays even though I changed the connection from sync to in .env file: QUEUE_CONNECTION=database

<?php

use App\Jobs\MyJob;
use App\Models\User;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    $user = User::first();
    MyJob::dispatch($user)->delay(5); // does not take effects
// MyJob::dispatch($user)->delay(now()->addSeconds(5)); // even this
    return view('welcome');
});

when I run job worker by calling php artisan queue:work all the jobs are processed immediately without delays but if I used: php artisan queue:work --rest=5 everything is fine can anyone know what's going on?

Snapey's avatar

@Osahady if your delay is 5 and your rest is also 5, how do you know which takes effect?

Osahady's avatar

@Snapey Normally I use: php artisan queue:work with the code provided but I noticed that the jobs are processed immediately with no delays even though I chained the delay method with the dispatch. Actually, the chained delay method does not take effect (exist or not = the same) the result I would like to get is the same as if you run:

php artisan queue:work --rest=5

which means to take a period let it be 5 seconds before processing the next job https://github.com/laravel/framework/issues/46568 and see also: https://github.com/laravel/framework/pull/46582#issuecomment-1483803976

friedaritter's avatar

Since you mentioned QUEUE_CONNECTION is set to beanstalkd, make sure you are running the worker with: php artisan queue:work --queue=your-queue-name

Replace your-queue-name with the actual queue name you are using.

Snapey's avatar

@friedaritter that question was raised 3 years ago. Someone just tagged on their own question today

Please or to participate in this conversation.