简体   繁体   中英

How to run a rake task with cron just twice?

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.

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.

尝试执行每当cron并创建一个rake任务,该任务正在做您的工作..并且在rake任务何时以及如何执行其操作时在数据库中创建一些触发器字段。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM