Search code examples
ruby-on-railstimerruby-on-rails-3observer-pattern

Performing an action in Rails at a specific time


Is there a way to setup a callback in ROR that would trigger at a specific time?

Lets say I'm running a contest that expries at a certain time. Lets say Monday July 28th at 9:00. I'd like to set up an observer that ran a function at Monday July 28th at 9:00. Does rails have a method to do this?


Solution

  • There's a run_at field in Delayed Job. You have to have a worker process in the background always running and looking for jobs that a set to run, but if your application is doing this a lot, it might be easier than always writing new cron jobs.

    So, you could have a method in your Contest model that gets called in a after_create callback that sets up a delayed job to send out an email to a random winner at the date that's specified.

    If it's a one time, or very infrequent deal, though, I'll agree about using whenever