How to use 'Whenever' gem?

user192249 picture user192249 · Sep 26, 2012 · Viewed 8.4k times · Source

I'm currently trying to use the whenever gem to schedule my tasks but I do not know how it works. I've tried following the steps at https://github.com/javan/whenever but I got stuck at schedule.rb file. What am I supposed to write inside here? I want my app to call a method every minute using this gem. How am I supposed to do it? Can anyone give me a clue on how to do so?

UPDATE


I did the following to my application whereby it's supposed to send out an email every minute. I tried running the method without the scheduling and it works but it doesn't work if i schedule it, like the codes below. Is there something wrong with my code?

1) schedule.rb

every 1.minute do
  runner "Newsletter.schedule_email"
end

2) newsletter.rb

def schedule_email
    ...*codes*...
end

Answer

MrTheWalrus picture MrTheWalrus · Sep 26, 2012

Well, the basic form would be:

every 1.minute do
  runner "Class.method_name"
end

If your stuff isn't running, this question might have some useful info: Whenever cron job is not working in rails 3

There's also a railscast about cron jobs in general and Whenever in particular: http://railscasts.com/episodes/164-cron-in-ruby