我应该将后台进程放在哪里? [英] Where should I put background processes in rails?
问题描述
我正在构建一个Rails项目,该项目具有由Rufus Scheduler管理的cron型作业.关于如何在Rails中进行适当设置,我有两个问题:
I'm building a Rails project that has a cron-type job that I'm managing with Rufus Scheduler. I have two questions about how to set this up appropriately in Rails:
- 放置工作代码的最佳位置在哪里?
- 我应该在哪里放置Rufus代码以安排工作?我应该如何开始呢?
推荐答案
要控制调度程序,我将创建一个config/initializers/task_scheduler.rb
:
To control the scheduler I would create a config/initializers/task_scheduler.rb
:
task_scheduler = Rufus::Scheduler.start_new
task_scheduler.every("1m") do
Something.to_do! # Do something every minute!
end
现在使用Something.to_do
代码,这种类型取决于它的功能.也许它是一个数据模型,应该放在app/models
目录中,但是如果它更通用,则可能需要将其放在lib/
中.如果您要完成许多计划内的任务,则可能要创建一个app/scheduled_tasks
目录,但是对于一个文件来说,这可能会显得有些过分.
Now for the Something.to_do
code, that sort of depends on what it does. Perhaps it is a data model and it should go in the app/models
directory, but if it is something more generic you might want to place it in lib/
. If you wind up with a number of scheduled tasks you might want to create a app/scheduled_tasks
directory, but that might be overkill for just one file.
这篇关于我应该将后台进程放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!