laravellaravel-5queuejobsbeanstalkd

Get the number of jobs before specific job in laravel queue


I supply some time costing calculating service on my server for users. I put these calculating work in the job queue. So I need to show how many jobs to wait for the user. While I cannot find a way to get how many jobs before specific job in the laravel queue. I used beanstalkd as the Queue.


Solution

  • As in the 'How can I get a list of all jobs in a beanstalk tube?' answer, Beanstalkd is not an array. It is not designed to be searched or counted within, just to get the next job to work on.

    If you want to see how many jobs are in the queue, and where they are in the order, make a note elsewhere, in another datastore - Redis or memcached for example. Make an entry when you put the job into the queue, and remove it when the job has been deleted from Beanstalkd. Note - this 'order' or 'sequence' would only be valid if all the jobs have the same priority, and you keep both systems in perfect sync.

    Some things you could more reasonably do is take a count of the total number of jobs in the queue (or a particular tube/queue), and if the jobs are broadly similar in the amount of time they should take, offer an estimate of how long it would be until a new job (or the new job you just put in) would take until it has been completed.

    You can help to calculate an ETA with a special job that has a createdAt time, and when it it run, it updates the time taken in the queue (now-createdTime) to an external store.