Create a reminder function in Ruby on Rails
How do you create a reminder type function in Ruby on Rails? What I want is basically an infinite thread that calls a database function periodically (every 1 minute). It seems as though most of the gems (God, Whenever, etc...) rely on calling cron jobs.
Are cron jobs the best/only way to do this that is robust and scalable? What are the drawbacks to running a thread in the background of your server?
Is something like Node.js 开发者_运维知识库a better way to go for something like this? Javascript has the very nice setInterval function built-in that seems to be exactly what I need. Sorry for the scattered questions, just looking at the options.
Thanks
You don't need a background job for reminders (as long as the operable dataset is not large). You could use Rufus::Scheduler to run a particular model action every/at/on a certain time.
in Gemfile
gem 'rufus-scheduler', :require => "rufus/scheduler"
in config/initializers/reminder_sheduler.rb
scheduler = Rufus::Scheduler.start_new
scheduler.cron("0 5 * * *") do
Model.send_reminder_email
end
Long running processes (especially in Ruby) often consume quite a bit of memory and become a point of failure, requiring monitoring, etc. Cron is pretty reliable, so it makes a lot of sense to just set an entry in crontab and make it a periodic process.
That said, there are Ruby background jobs that would support this. Take a look at https://github.com/bvandenbos/resque-scheduler , as an example.
精彩评论