Where should I put background processes in rails?
Asked Answered
P

1

2

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:

  1. Where's the best place to put the job's code?
  2. Where should I place the Rufus code to schedule the job? How should I kick it off?
Pallid answered 13/1, 2010 at 18:15 Comment(0)
T
2

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

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.

Tour answered 13/1, 2010 at 18:53 Comment(1)
Though this solution is simple one, there will be issues when you deploy your rails app in multiprocess server like unicorn or passenger, your job might be executed multiple times.Dirndl

© 2022 - 2024 — McMap. All rights reserved.