更新时间:2023-11-30 17:54:22
要控制调度程序,我将创建一个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.