Ruby on rails scheduled tasks - ruby-on-rails

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?

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

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.

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

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.

Related

Rails acceptance tests - run Foreman

My application runs properly using Faye, Redis, Resque and other services, that I am starting every time with Foreman. Now I am writing acceptance tests with Capybara+RSpec, and I wonder how I could start Foreman (or even Faye alone) with Capybara in spec_helper to test live features.
One option is to not depend on these services in your tests. I know redis and resque have "mock" counterparts that behave like these services without actually needing to run them. Some services might not have a "mock" version (I can't find one for Faye), so you may just need to run them in the background.
if you are using jenkins as the CI server, you can start the foreman before rspec started.

how can I speed up ruby on rails tests on windows?

I've started learning to use Ruby on Rails running on Windows 7, and the time to run tests is painful I'm wondering if I can speed it up.
Right now I am using the default test framework (inheriting from ActionController), with a SQLite database and the webrick web server.
While unit and functional tests report a runtime of less than 2 seconds, from the time I run the tests from the command-line to completion is actually 40 secounds.
A friend recommended I use guard. That looks like it will help start the tests as soon as I save, but it seems like the real cost is starting up the webserver or database. I wonder if it might be better to set up apache or mysql and use those locally instead.
Anyhow, what tips do people have for speeding up ruby on rails tests on windows? I tried running the tests on Amazon EC2 linux micro instance (again with webrick and sqlite) and there was significant startup time (though I did not time it).
I tried "rake test --trace". There was a significant pause:
Immediately before the first line of output
Between outputing "Execute environment" and "Execute db:abort_if_pending_migrations"
Between "Execute test:units" and "Run options:"
The first pause seems worse.
Your best bet is to use spork which now works in Windows. It runs on windows by pre-populating a pool of ready processes (referred to as the “magazine” strategy). The result is that webserver startup time is dramatically reduced.
If running rails on Windows is really the bottleneck then you can run a virtual machine using VirtualBox and run an instance of Ubuntu and work on your rails projects in the VM.

Which schedulers for rails 3.1 or above can stay on all the time on ubuntu 12.04?

We are looking for a scheduler which can stay on all the time for our rails 3.1 app on ubuntu 12.04 server. Currently we are using rufus scheduler which could be killed by Passenger by accident and can not stay on all the time. There are quite a few of schedulers available out there. We are looking for a simple and easy to use one for our rails app. The primary purpose of the scheduler is to fire off periodically to check the session timestamp and reset if necessary. Any recommendation? Thanks.
Have you looked at clockwork?
https://rubygems.org/gems/clockwork
For rufus-scheduler vs Passenger, you might want to look at Debugging Rufus scheduler
Else if you don't want to tune your Passenger, go the crond way and use Whenever: https://github.com/javan/whenever

Any background process gems that work with JRuby deployed on Windows?

Does anyone know a working background job solution for JRuby deployed on a windows server? (via warbler and tomcat)
I'm looking for a way to schedule background jobs from my Rails 3.2 app, so that the web app can respond immediately rather than hang up while a long running job runs.
I tried the delayed_job gem, except it doesn't seem to work with JRuby and Windows. (If I'm wrong, please enlighten me) Resque depends on redis which evidently doesn't support Windows. I don't have experience with Beanstalkd or Starling, but the documentation for them doesn't mention windows.
I'ld love to dump windows, believe me, but the background job is a windows executable that did not come with source. And I need to use JRuby to be able to call some Java code too.
Solved the problem using the jruby-rack-worker gem, http://github.com/kares/jruby-rack-worker, which let's you use delayed_job to schedule jobs, just provides a different way to kickoff worker processes that is more JRuby/Warbler/Tomcat friendly.
We use Rufus Scheduler for that purpose. The scheduler configuration sits in an initializer file like so:
scheduler = Rufus::Scheduler.start_new
scheduler.every('1d') do
puts "I run once every day"
end
scheduler.every '3h' do
puts "I run every 3 hours"
end

How to make/monitor/deploy daemon processes in JRuby

I'm currently porting a Rails App currently using REE to JRuby so I can offer an easy-to-install JRuby alternative.
I've bundled the app into a WAR file using Bundler which I'm currently deploying to GlassFish. However, this app has a couple of daemon processes and it would be ideal if these could be part of the WAR file, and potentially monitored by Glassfish (if possible).
I've looked at QuartzScheduler, and while meets my needs for a couple of things, I have a daemon process that must execute every 20 seconds as it's polling the database for any delayed mail to send.
If anyone can provide any insight as to how best to set up daemon processes in a JRuby/Java/Glassfish environment any help will be greatly appreciated! :)
One way to daemonize a JRuby process is to use akuma framework (on *nix) or others.
I would rather use cronjobs (schedulling) rather than daemons as they are less error-proned, daemons can leak memory, can stop on errors etc. Check jruby-quartz and quartz_scheduler
EDIT
If one uses Torquebox it offers support for services and scheduling.

Resources