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

查看:18
本文介绍了在 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_jobdelayed_job.使用 background_job,worker 通过 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.

使用delayed_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(我更喜欢使用 script/runner 而不是 Rake任务,因为我发现测试代码更容易).

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天全站免登陆