Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails delayed job and docker: adding more workers

I run my rails app using a Docker. Delayed jobs are processed by a single worker that runs in a separate container called worker and inside it worker runs with a command bundle exec rake jobs:work.

I have several types of jobs that I would like to move to a separate queue and create a separate worker for that. Or at least have two workers for process tasks.

I tried to run my worker container with env QUEUE=default_queue bundle exec rake job:work && env QUEUE=another_queue bundle exec rake job:work but that does not make any sense. It does not fails, is starts but jobs aren't processed.

Is there any way to have separate workers in one container? And is it correct? Or should I create separate container for each worker I would ever want to make?

Thanx in advance!

like image 650
Ngoral Avatar asked Nov 25 '25 14:11

Ngoral


1 Answers

Running the command command1 && command2 results in command2 being executed only when command1 completes. rake jobs:work never terminates, even when it has finished executing all the jobs in the queue, so the second command will never execute.

A single "&" is probably what you're looking for: command1 & command2. This will run the commands independently in their own processes.

You should use the delayed_job script on production, and it's a good idea to put workers of different queues into different containers in case one of the queues contains jobs that use up a lot of resources.

This will start a delayed job worker for the default_queue:
bundle exec script/delayed_job --queue=default_queue start
For Rails 4, it is: bundle exec bin/delayed_job --queue=default_queue start
Check out this answer on the topic: https://stackoverflow.com/a/6814591/6006050

You can also start multiple workers in separate processes using the -n option. This will start 3 workers in separate processes, all picking jobs from the default_queue:
bundle exec script/delayed_job --queue=default_queue -n 3 start

Differences between rake jobs:work and the delayed_job script:
It appears that the only difference is that rake jobs:work starts processing jobs in the foreground, while the delayed_job script creates a daemon which processes jobs in the background. You can use whichever is more suited to your use case.
Check this github issue: https://github.com/collectiveidea/delayed_job/issues/659

like image 68
Reub Avatar answered Nov 28 '25 11:11

Reub