Ruby on rails scheduled tasks
Asked Answered
E

4

6

This is my first time scheduling a task and I am not sure of the best implementation (or the proper implementation).

My Goal: I have a ruby on rails 4 app setup with twilio and deployed on Heroku. I want the app to automatically text all of my users once a week with a customized text message (which is written and created by information in the database).

From research I have come down to the following Gems: Whenever and Rufus-Scheduler.

I believe that both these gems can get the Job done, but upon reading on the Rufus' docs: "please note: rufus-scheduler is not a cron replacement" I got stuck trying to understand if what I want is indeed a cron job or a "Rufus-Scheduler".

I am left with the following questions: What is a cron job and when is the appropriate time to use it? Why is Rufus-Scheduler not a cron replacement and what does it do differently? Which one should I use?

Ecumenicalism answered 30/4, 2014 at 2:57 Comment(0)
S
1

About Cron:

Cron is name of program which does scheduled tasks on nix systems. what Scheduled Tasks are in Windows, Cron does something similar for Linux at the conceptual level.Cron is one of the most useful tool in Linux or UNIX like operating systems. The cron service (daemon) runs in the background and constantly checks the /etc/crontab file, and /etc/cron./ directories. It also checks the /var/spool/cron/ directory.

For Scheduling tasks on Heroku

Good news is that on Heroku there is a thing called Scheduler which is an add-on for running jobs on your app at scheduled time intervals, much like cron in a traditional server environment. so you really don't need to fiddle/player with cron or gems like whenever. just use the Scheduler addon on Heroku.

For More info see: https://devcenter.heroku.com/articles/scheduler

Stoffel answered 30/4, 2014 at 3:6 Comment(3)
Yes, the heroku Scheduler does seem like a great option, I was a little turned off by it when I kept reading in its docs "Scheduler has a known issue whereby scheduled processes are occasionally skipped"Ecumenicalism
I believe this would be the right answer if the Heroku task Scheduler actually allowed for scheduling tasks once a week as opposed to once a day.Ecumenicalism
stecd: nothing prevents you from having a schedule triggered every day but that only does something on sundays. if day != sunday then do nothing and return.Single
H
1

A cron job is a program run on an automated time schedule, using the cron software.

Rufus-Scheduler is different from cron because it runs inside of Ruby processes.

For what you're describing I believe either would be fine.

Hermelindahermeneutic answered 30/4, 2014 at 3:9 Comment(0)
S
1

rufus-scheduler is not a cron replacement

can be expanded to "rufus-scheduler was never written as a drop-in replacement for cron". The bigger message is "you are a developer and as a developer you should know the environment you inherit and the tools it comes with. You should know when to use them, when to imitate them, when to replace them."

Rufus-scheduler understands the "* * * * *" syntax of Cron. This has led some people to say "rufus-scheduler tries to be a Cron replacement". It could better be formulated as "some people have abused rufus-scheduler instead of thinking (knowing) that old faithful Cron would have been better in that situation".

To become a good developer you should think seriously about some *nix sysadmin skills, else you'll be a pain to work with. It can be as easy as "install, run and manage a linux box in a VirtualBox in your system".

A rufus-scheduler schedule runs in a Ruby process. In a vanilla world, one runs rufus-scheduler in the same Ruby process that services http requests (Rails or Sinatra web application), and, oh, oops, the schedules don't run when the application doesn't run.

Cron is a service provided by your *nix operating system. Other applications and services on your host rely on it. Cron reads its crontab and runs the script indicated in it at the given times. Thanks to the excellent Whenever, Cron can be told to run scripts in your Rails application.

This might interest you as well: https://devcenter.heroku.com/articles/clock-processes-ruby

Single answered 30/4, 2014 at 6:25 Comment(0)
H
0

whenever allows you to write ruby code that would be transformed in a crontab file, which is a file specifying a set of commands and a frequency for each command. This file is used by cron.

rufus-scheduler is pure ruby, you write ruby and jobs are running in ruby, for example inside your application loop calling scheduler.join, or in another thread calling ruby your/rufus_scheduler_script.rb.

In my opinion they do the same things, with cron you are using a linux command, but I don't see any other difference.

There are other options too, in my experience I have had problems with rufus-scheduler, and whenever (they did not find classes in my Rails app, but maybe was just a quirk), on the other side clockwork worked for me.

Harvestman answered 30/4, 2014 at 3:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.