How to run a rake task with cron just twice?

252 views Asked by At

I want to know if there is a way in RoR to run a rake task, or a ruby code, twice in different times. For example, when a user registers, run the task after three days, and then run the same task one week later, but no more('stop the process'). I was looking at crontab and gems like Resque, Stalker, Starling, etc. but I don't have a clear idea who this gems can help me. I'm thinking in run a daemon for each user and count the task executions.The problem is that the daemon would be active all that time "eating" resources. Is there a way to use as least resources as possible?. I want to run this in Heroku later.

3

There are 3 answers

0
Mark Swardstrom On BEST ANSWER

I usually solve this type of thing with date columns on the record (eg. first_reminder_sent_at, user_responed_at, week_reminder_sent_at). In this case, a date for the first item, the user's response, and for the week-later item.

Create a cron task to call a rake task - look at all users that are > 3 days old, < 1 week, user hasn't responded, date not set. Queue up the Background Job. The Job will send the mail (or whatever the task is) and then set the date field on the record.

Cron task to call a rake task - looks at all users that are > 1 week old where user hasn't responded and date not set. Send the reminder and set the date in a Background Job.

0
BvuRVKyUVlViVIc7 On

Try the whenever cron and create a rake task, which is doing your stuff.. and in the database make some trigger fields when and how the rake task should do his actions..

0
dbenhur On

Posix systems have the at command which is a dead simple way to schedule a job to run once at a later time. Though there's nothing wrong with using an application based queueing mechanism to schedule later work as outlined in the other answers. at is host specific so probably unsuitable if you're running a cluster, especially if the hosts may be transient.