在Rails中组织工作进程的最佳方法是什么? [英] What's the best way to organize worker processes in Rails?

查看:53
本文介绍了在Rails中组织工作进程的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常有一些代码应该按计划运行,或者作为带有某些参数的后台进程运行.共同点是它们在调度过程之外运行,但是需要访问Rails环境(以及可能传入的参数).

I frequently have some code that should be run either on a schedule or as a background process with some parameters. The common element is that they are run outside the dispatch process, but need access to the Rails environment (and possibly the parameters passed in).

什么是组织此的好方法,为什么?如果您想使用特定的插件或gem,请解释为什么会觉得方便-不仅要列出您使用的插件.

What's a good way to organize this and why? If you like to use a particular plugin or gem, explain why you find it convenient--don't just list a plugin you use.

推荐答案

对我来说,不想维护很多额外的基础架构是一个关键优先事项,因此我使用了在Rails之外运行的数据库支持的队列.

For me, not wanting to maintain a lot of extra infrastructure is a key priority, so I have used database-backed queues that are run outside of Rails.

就我而言,我使用了 background_job delayed_job .使用background_job,该工作程序通过cron保持运行,因此没有守护程序管理.通过delayed_job,我正在使用Heroku,让他们对此感到担忧.

In my case, I've used background_job and delayed_job. With background_job, the worker was kept running via cron, so there was no daemon management. With delayed_job, I'm using Heroku and letting them worry about that.

使用delay_job,您可以传递后台运行程序需要运行的尽可能多的参数.

With delayed_job you can pass in as many arguments as your background worker needs to run.

Delayed::Job.enqueue(MyJob.new(param[:one], param[:two], param[:three])

除了通过cron使用script/runner之外,我还没有找到一种按计划运行内容的好的解决方案(我更喜欢在Rake任务中使用script/runner,因为我发现它更容易测试代码).

I have not found a good solution to running stuff on a schedule, aside from using script/runner via cron (I prefer to use script/runner over a Rake task because I find it easier to test the code).

我从不需要有一个定期计划的后台进程,该进程需要访问特定的Rails请求,所以这并不是一个太大的问题.

I've never had to have a regularly scheduled background process that needed access to a particular Rails request so that hasn't been too much of a problem.

我知道还有其他具有更多功能的更凉爽的系统,但这对我来说行之有效,并帮助我避免设置许多新服务来进行管理.

I know there are other, cooler systems with more features but this has worked OK for me, and helps me avoid dealing with setting up a lot of new services to manage.

这篇关于在Rails中组织工作进程的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆