简体   繁体   English

Rails 延迟作业和 docker:添加更多工人

[英]Rails delayed job and docker: adding more workers

I run my rails app using a Docker.我使用 Docker 运行我的 rails 应用程序。 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 .延迟作业由一个单独的 worker 处理,该 worker 运行在一个名为worker的单独容器中,在其中 worker 使用命令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.我尝试使用env QUEUE=default_queue bundle exec rake job:work && env QUEUE=another_queue bundle exec rake job:work运行我的工作容器,但这没有任何意义。 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!提前谢谢!

Running the command command1 && command2 results in command2 being executed only when command1 completes.运行命令command1 && command2导致 command2 仅在 command1 完成时执行。 rake jobs:work never terminates, even when it has finished executing all the jobs in the queue, so the second command will never execute. rake jobs:work 永远不会终止,即使它已经完成了队列中的所有作业,所以第二个命令永远不会执行。

A single "&" is probably what you're looking for: command1 & command2 .单个“&”可能就是您要查找的内容: 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.您应该在生产中使用 delay_job 脚本,最好将不同队列的工作人员放入不同的容器中,以防其中一个队列包含占用大量资源的作业。

This will start a delayed job worker for the default_queue:这将为 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对于 Rails 4,它是: bundle exec bin/delayed_job --queue=default_queue start
Check out this answer on the topic: https://stackoverflow.com/a/6814591/6006050查看有关该主题的答案: https : //stackoverflow.com/a/6814591/6006050

You can also start multiple workers in separate processes using the -n option.您还可以使用 -n 选项在单独的进程中启动多个工作程序。 This will start 3 workers in separate processes, all picking jobs from the default_queue:这将在不同的进程中启动 3 个工人,所有从 default_queue 中挑选作业:
bundle exec script/delayed_job --queue=default_queue -n 3 start

Differences between rake jobs:work and the delayed_job script: rake jobs:work和 delay_job 脚本之间的差异:
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.似乎唯一的区别是rake jobs:work在前台开始处理作业,而 delay_job 脚本创建了一个在后台处理作业的守护进程。 You can use whichever is more suited to your use case.您可以使用更适合您的用例的任何一种。
Check this github issue: https://github.com/collectiveidea/delayed_job/issues/659检查这个 github 问题: https : //github.com/collectiveidea/delayed_job/issues/659

Actually i just came across this problem with scaling delayed_jobs on docker实际上,我刚刚在 docker 上扩展 delay_jobs 时遇到了这个问题

see this gist for a script that starts delayed jobs with arbitrary arguments and listens to SIGTERM and executes a smooth shutdown of the started jobs on container shutdown.请参阅此要点以了解使用任意参数启动延迟作业并侦听 SIGTERM 并在容器关闭时平滑关闭已启动作业的脚本。 This way you can execute as many processes and queues as you want.通过这种方式,您可以根据需要执行任意数量的进程和队列。

https://gist.github.com/jklimke/3fea1e5e7dd7cd8003de7500508364df https://gist.github.com/jklimke/3fea1e5e7dd7cd8003de7500508364df

#!/bin/bash

# Variable DELAYED_JOB_ARGS contains the arguments for delayed jobs for, e.g. defining queues and worker pools.

# function that is called when the docker container should stop. It stops the delayed job processes
_term() {
  echo "Caught SIGTERM signal! Stopping delayed jobs !"
  # unbind traps
  trap - SIGTERM
  trap - TERM
  trap - SIGINT
  trap - INT
  # end delayed jobs 
  bundle exec "./bin/delayed_job ${DELAYED_JOB_ARGS} stop"

  exit
}

# register handler for selected signals
trap _term SIGTERM
trap _term TERM
trap _term INT
trap _term SIGINT

echo "Starting delayed jobs ... with ARGs \"${DELAYED_JOB_ARGS}\""

# restart delayed jobs on script execution
bundle exec "./bin/delayed_job ${DELAYED_JOB_ARGS} restart"

echo "Finished starting delayed jobs... Waiting for SIGTERM / CTRL C"

# sleep forever until exit
while true; do sleep 86400; done

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM