TheDelChop
TheDelChop

Reputation: 7998

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?

Upvotes: 3

Views: 4181

Answers (3)

Dan McNevin
Dan McNevin

Reputation: 22336

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

Upvotes: 2

Geoff Lanotte
Geoff Lanotte

Reputation: 7500

I agree about crontab, but I like whenever. It has a nice integration into cron that you can store with your repo and it integrates nicely with capistrano.

Upvotes: 0

Eimantas
Eimantas

Reputation: 49344

You'd be better off writing a ruby script that runs via crontab at the exact time you need it to.

Upvotes: 0

Related Questions