MooseSaid's avatar

Check if queued jobs finished

I'm out of ideas, I'm using Spatie Media Library to upload and convert images. The package fires a job for each image in the form to do conversions and stuff. I want the user to have some kind of a feedback once all images has been uploaded successfully.

I thought of showing a loading icon and use Inertia manual visits to fire a get request every 10 seconds to a route that checks only the jobs related to this request and if completed I remove the loading icon. But how can I check only the queued jobs related to this request?

0 likes
9 replies
Nakov's avatar
Nakov
Best Answer
Level 73

You can check the size of the queue like this:

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

of course the uniqueQueueName should be replaced with the queue name, or leave it empty if you are adding them to the default queue.

2 likes
MooseSaid's avatar

@Nakov So, the package adds jobs to a queue named media

How can I track jobs completion in this queue with size?

Nakov's avatar

@MooseSaid

$queue = App::make('queue.connection');
$size = $queue->size('media');
2 likes
Nakov's avatar

@MooseSaid omg, you are asking for checking if there are still jobs to be processed.. so as long as the size is bigger than 0, that means that there are still images that are being processed.. if the size is 0, the queue is empty and all the images have been processed. Makes sense?

1 like
MooseSaid's avatar

@Nakov Makes perfect since but what if I have many jobs across my app? I don't want the user to wait for ALL jobs running across the app to be completed so that he get a feedback that his own has been completed. Or you think that's fine from you experience?

Nakov's avatar

@MooseSaid yeah, that's a good point if you have lots of users and all of them uploaded images at the same time, then you will have your user wait until all jobs were processed.

Maybe similar to what @cosmeoes suggested, but that will work for single job, while you have multiple jobs per request. Pass to the job the total number of jobs and the current index, and then Queue::after event which each job dispatches compare if that's the last one and notify the user by dispatching another event. You won't need to make the 10 seconds visit this way. But also nothing guarantees that the job will be taken in the right order.

Maybe job chaining will be good for you here: https://laravel.com/docs/9.x/queues#job-chaining

But this will process the images one after the other, not in the same time if you have multiple workers. So I don't know, chose what best works for you.

cosmeoes's avatar

The way I've done this before is by adding something to the cache when you add the job to the queue and in the job deleting that cache when it finishes.

2 likes

Please or to participate in this conversation.