Brendan White
Brendan White

Reputation: 453

How can I check the status of a queue in Laravel?

I need to submit a number of jobs to a Laravel queue to process some uploaded CSV files. These jobs could be finished in one second if the files are small, or a few seconds if they're bigger, or possibly up to a minute if the CSV files are very big. And I can't tell in advance how big the files will be.

When the user goes to the "results" page, I need to display the results - but only if the queue has finished the jobs. If the queue is still processing, I need to display a "try again later" message.

So - is there a way to check, from a controller, whether the queue has finished?

I'm currently using Laravel 5.1 but would happily upgrade if that helps. And I'm currently using the database queue driver. Ideally I'd love to find a general technique that works for all queue drivers, but if the only way to do it is to check a database table then I guess that's what I have to do.

Thanks!

Upvotes: 13

Views: 32150

Answers (3)

woens
woens

Reputation: 1078

since version 8, laravel provides the concept of batches, which you can use to monitor progress of jobs.

Upvotes: 0

Brendan White
Brendan White

Reputation: 453

I expect if I was trying to do this today, I'd use a Laravel Job Event to update a status field on the database record, to log when the job has started and when it's finished.

Then I could see whether a record has been fully processed or not by just looking at the status field on the record itself.

Upvotes: 2

Robert Norman
Robert Norman

Reputation: 191

I know this is a year old, but why not create a new queue per upload with a unique key based on that request.

$job = (new ProcessCSVJob($data))->onQueue($uniqueQueueName);

You can then simply either do a count in the database on the queue name field if you want a DB only solution.

To work across all queue types you can use the Queue size method to return the queue size.

$queue = App::make('queue.connection');
$size = $queue->size($uniqueQueueName);

This is in Laravel 5.4. Not sure how backwards compatible this is.

Upvotes: 10

Related Questions