rcravens's avatar

Queue Time Out Per Job

Is there a way to set or override the default timeout of a Job as part of the Job. What I'd like to do is something like this:

class MyJob extends Job implements SelfHandling, ShouldQueue
{
    protected $timeout = 300;
}

Most of my jobs are short (sub minute) but I have a few longer jobs. I would like to have the default (app/queues.php) expire used but change it on the long running jobs.

Any one know if this is possible?

Thanks,

Bob

0 likes
5 replies
marin246's avatar

Hi, i know this is an old post, but i am having a similar issue.

In fact, all you need to do to set a CONSTANT timeout per job is to declare that variable public in your job class (https://laravel.com/docs/5.6/queues#max-job-attempts-and-timeout) :

public $timeout = 300; // Only works with artisan queue:work (NOT with queue:listen)

However, is there a way to make this variable dynamic?

E.g if you want to sync all your users to some server via an API, the job will take longer depending on how many users you have. As far as I can tell, defining a timeout method on the job does not work like so:

public function timeout() {
    return 2*User::count(); // 2 seconds per user
}

Is there any other (working) way? Thanks in advance!

rin4ik's avatar
php artisan queue:listen --timeout=0
marin246's avatar

Could you elaborate? Does --timeout=0 disable the timeout feature alltogether? If so, I don't want that..

Reppair's avatar

I know the post is quite old, just for future reference.

Using Laravel 8.26.1, redis queue and horizon v5.6.5, I can run php artisan horizon and it will respect the public int $timeout = 240; I've defined, but only when I push a new job instance in the queue.

Please or to participate in this conversation.