1
votes

I've built a system based on Laravel where users are able to begin a "task" which repeats a number of times, with a delay between each repetition. I've accomplished this by queueing a job with an amount argument, which then recursively queues an additional job until the count is up.

For example, I start my task with 3 repetitions:

  1. A job is queued with an amount argument of 3. It is ran, the amount is decremented to 2. The same job is queued again with a delay of 5 seconds specified.
  2. When the job runs again, the process repeats with an amount of 1.
  3. The last job executes, and now that the amount has reached 0, it is not queued again and the tasks have been completed.

This is working as expected, but I need to know whether a user currently has any tasks being processed. I need to be able to do the following:

  1. Check if a particular queue has any jobs started by a particular user.
  2. Check the value that was set for amount on that job.

I'm using the database driver for a queue named tasks. Is there any existing method to accomplish my goals here?

Thanks!

1
Did you finally find a solution for this?user5832543
@Bahman Nothing elegant. I couldn't find any way to associate a queued job with a specific user. I took a slightly different approach. I made a tasks table, which stores the remaining count and a reference to the user. Then, on the queued job, I passed the task_id in. That job runs on repea and checks the tasks table. If it doesn't find the task record it's looking for, it stops re-queueing. So instead of checking if a user has a queued job, I check the tasks table. It isn't perfect but it's close enough for the time being. Happy to explain in more detail.Tyler

1 Answers

0
votes

You shoudln't be using delay to queue multiple repetitions of the same job over and over. That functionality is meant for something like retrying a failed network request. Keeping jobs in the queue for multiple hours at a time can lead to memory issues with your queues if the count gets too high.

I would suggest you use the php artisan schedule:run functionality to run a command every 1-5 minutes to check the database if it is time to run a user's job. If so, kick off that job and add a status flag to the user table (or whatever table you want to keep track of these things). When finished you mark that same row as completed and wait for the next time the cron runs to do it again.