Schedule a method to run once a day in Rails 3

I have a method that checks if the user has provided a schedule for the previous month, and if not, sends an email reminder. This method should work every day. The user takes no action to actually run this method, it just runs at a set time every day.

Any idea on the best way to implement this in Rails 3?

+3


source to share


4 answers


Create a rake task to run the method + deliver emails and look at every gem that can automate cron generation.

https://github.com/javan/whenever



Then you can create each config file in config / schedule.rb as follows:

every 1.day, :at => '12:00 pm' do
  rake "timesheet:check"                 
end

      

+3


source


Do you definitely need to run this as part of your rails app? Can it be counted to be your own service?

If you can bring this functionality from your rails app and run it yourself, you can just write a ruby ​​script and execute it at the scheduled time using a cron job.



It can get unnecessarily messy when it comes to your rails application because you need something to constantly check the time and see if it's time to call this method. A cron job is always an easier way to do tasks like yours.

I would be happy to point you to resources to help you set up your cron job if you tell me which operating system you are using.

0


source


Have a look at Resque Scheduler . You can set cron to 0 0 * * *

run your script once every time. Here is the railscasts at Resque .

0


source


I am developing Mac OS X with our production machine on CentOS, but this also applies to Ubuntu.

I have a rake task defined in report.rake

namespace :report do
  desc "Email stats"
  task :email_global_stats, [] => :environment do |t, args|
    StatsMailer.global_stats_email.deliver
  end
end

      

And the bulk of the logic for checking new features and then sending them by email is defined in StatsMailer

. Then on a production machine, I have this in my crontab (accessed via crontab -e

):

30 7 * * * RAILS_ENV=production /usr/local/bin/rake -f /home/user/code/stats/current/Rakefile report:email_global_stats

      

It is set at 7:30 am, so not everything works for me at midnight. And then I want to test locally, I just run:

rake report:email_global_stats

      

from my development directory. You also need to make sure it's postfix

running by typing this on Mac OS X:

sudo postfix start

      

0


source







All Articles