0

I'm wondering how does one implement a cron job like feature in rails.

For instance, you have a member model. My system sends a SMS welcome message each time a new member sign up. However, if this user has been greeted before, don't send it again. Thus, each 10 minutes I'd like to look into the member table, and see who shall I send the message to.

This is simply an example, and I'm aware it's easier to implement otherwise. However I'd like to know if for a cron job like task, which has to look into the active records

1) where shall I put the script? 2) is it a good practice to access the db via active records directly? 3) shall I use a gem like whenever? or in this situation would be easier to implement just using system cron?

Thanks!

2
  • How about create a rake task (i.e., rake greet_new_member) and use cron to run this task every 10 minutes?
    – yihangho
    Commented Jul 21, 2015 at 23:55
  • I agree with @yihangho a rake task will get you going fast with a regular cronjob Commented Jul 22, 2015 at 0:02

2 Answers 2

2

Whenever is a sort of "wrapper" tool around system cron; there's no reason to write a custom batch to setup rails environment and launch tasks, as whenever does exactly this job. It plays nicely with capistrano and mina, both having the ability to update the crontab during deployment.

Take a look at the whenever documentation and at mina documentation to have a clearer idea.

Cheers!

0

This is called an asynchronous job. Resque and Sidekiq are popular solutions for that.

Or you may create a rake task and put it in whenever or directly to cron. Of course, it's totally fine to use ActiveRecord in rake tasks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.