skip to Main Content

I have a lot of tasks, which should be processed in proper order. I would like to divide them by “type” to different queues. Queues should be created dynamically. Then I would like to start X workers, which will process the tasks from the queues. But there is one important rule – from each queue, only one task should be processed at once. The reason is – each task of the specified type can change the state of the application, so I can’t start processing a new task of the same type if the last one hasn’t finished.

I would like to use Laravel queue system with Redis driver, but I’m not sure it’s able to do that. How to prevent the queue system from taking more than one job from each queue at once? Any ideas?

Thank you in advance for your help.

2

Answers


  1. If you are using supervisor then this is what you can do in your .conf file.

    [program:laravel-worker]
    process_name=%(program_name)s_%(process_num)02d
    command=php /var/www/html/****/artisan queue:work
    autostart=true
    autorestart=true
    user=root
    numprocs=1  <----- this is what you are looking for
    redirect_stderr=true
    stdout_logfile=/var/www/html/****/storage/logs/supervisord.log
    

    numprocs directive will instruct Supervisor to run 1 queue:work processes and monitor it, automatically restarting it if it fails. (Laravel Queue Supervisor Doc)

    Login or Signup to reply.
  2. Job chaining allows you to specify a list of queued jobs that should be run in sequence after the primary job has executed successfully. If one job in the sequence fails, the rest of the jobs will not be run. To execute a queued job chain, you may use the withChain method on any of your dispatchable jobs.

    If you would like to specify the default connection and queue that should be used for the chained jobs, you may use the allOnConnection and allOnQueue methods.

    ProcessPodcast::withChain([
        new OptimizePodcast,
        new ReleasePodcast
    ])->dispatch()->allOnConnection('redis')->allOnQueue('podcasts');
    

    See Laravel docs for more info.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search