Ruby-on-rails 如何在生产服务器中运行 sidekiq?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22958188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to run sidekiq in production server?
提问by Debadatt
I have a server with apache + passenger.
我有一台带有 apache + 乘客的服务器。
How will I run sidekiqin production? Any configuration needed to run the
我将如何sidekiq在生产中运行?运行所需的任何配置
bundle exec sidekiq
Thanks
谢谢
回答by Yersin
bundle exec sidekiq -d -L log/sidekiq.log -C config/sidekiq.yml -e production
bundle exec sidekiq -d -L log/sidekiq.log -C config/sidekiq.yml -e production
-d, Daemonize process
-d, 守护进程
-L, path to writable logfile
-L, 可写日志文件的路径
-C, path to YAML config file
-C, YAML 配置文件的路径
-e, Application environment
-e, 应用环境
回答by Brian Warren
A better solution than using the daemonization -d flag is to leverage the process supervisor provided by your OS. This is also the recommendation given by the sidekiq gem's wiki:
比使用 daemonization -d 标志更好的解决方案是利用操作系统提供的进程管理器。这也是sidekiq gem 的 wiki 给出的建议:
I strongly recommend people not to use the -d flag but instead use a process supervisor like systemd or upstart to manage Sidekiq (or any other server daemon). This ensures Sidekiq will immediately restart if it crashes for some reason.
我强烈建议人们不要使用 -d 标志,而是使用像 systemd 或 upstart 这样的进程主管来管理 Sidekiq(或任何其他服务器守护程序)。这确保了 Sidekiq 在它由于某种原因崩溃时会立即重新启动。
The wiki provides a example config files for both upstart and systemd found in the "examples" directory of the repo.
wiki 为repo的“examples”目录中的upstart 和 systemd 提供了一个示例配置文件。
NOTEOn my CentOS 7 server I use rvm (Ruby Version Manger). I had to perform an extra step to ensure that my systemd script (/etc/systemd/system/sidekiq.service) could reliably start and stop sidekiq, even in the case where my ruby and/or gemset paths change in the future. The most important directive is "ExecStart", which looks like the following in my script:
注意在我的 CentOS 7 服务器上,我使用 rvm(Ruby 版本管理器)。我必须执行一个额外的步骤来确保我的 systemd 脚本 (/etc/systemd/system/sidekiq.service) 能够可靠地启动和停止 sidekiq,即使我的 ruby 和/或 gemset 路径在未来发生变化。最重要的指令是“ExecStart”,它在我的脚本中如下所示:
ExecStart=/usr/local/rvm/wrappers/surveil/bundler exec sidekiq -e production -L log/sidekiq.log -C config/sidekiq.yml
The part of the path "/usr/local/rvm/wrappers/surveil", is actually a symlink, which I recreate with the assistance of 'rvm alias'during deployment to ensure that it always points to the app's ruby version and gemset, both of which could feasibly change from one deployment to another. This is achieved by creating a rake task that runs during deployment and does the equivalent of the following:
路径“/usr/local/rvm/wrappers/surveil”的部分实际上是一个符号链接,我在部署期间在“rvm alias”的帮助下重新创建,以确保它始终指向应用程序的 ruby 版本和 gemset,这两种部署都可以从一种部署转变为另一种部署。这是通过创建一个在部署期间运行的 rake 任务来实现的,并执行以下等效操作:
rvm alias delete surveil
rvm alias create surveil ruby-#{new_ruby_version}@#{new_gemset_name}
By setting up this alias/symlink during deployment, I can safely leave the systemd script untouched and it will keep working fine. This is because the path "/usr/local/rvm/wrappers/surveil/bundler" always points to the correct version of bundler and thus beneifts from the bundler magic that causes its targets to run in the app's configured ruby/gem environment.
通过在部署期间设置这个别名/符号链接,我可以安全地保持 systemd 脚本不变,它会继续正常工作。这是因为路径“/usr/local/rvm/wrappers/surveil/bundler”总是指向正确版本的bundler,因此受益于bundler 的魔力,使其目标在应用程序配置的ruby/gem 环境中运行。
回答by alexpls
You should be able to start Sidekiq as a background process (daemon) by passing the -d argument when you start it up:
您应该能够通过在启动时传递 -d 参数来将 Sidekiq 作为后台进程(守护进程)启动:
bundle exec sidekiq -d.
bundle exec sidekiq -d.
Although this answer should work for you now, please be aware that if the sidekiq process crashes for any reason the process will have to be manually restarted. A good starting place for finding out about more robust ways to run sidekiq in production is here: https://github.com/mperham/sidekiq/wiki/Deployment
尽管此答案现在应该对您有用,但请注意,如果 sidekiq 进程因任何原因崩溃,则必须手动重新启动该进程。了解在生产中运行 sidekiq 的更强大方法的一个很好的起点是:https: //github.com/mperham/sidekiq/wiki/Deployment

