cron is the system process which will automatically perform tasks for you according to a set schedule. The schedule is called the crontab, which is also the name of the program used to edit that schedule. For example, let’s say you have a rake task which you want to run every hour.
namespace :send_update_mail do desc "send_product_update_mails" task :send_mail => :environment do UserMailer.notify_product_updates end endTo edit the crontab, use this command:
crontab -eNow let’s add our job to the crontab. Each job you add should take up a single line. The format is very simple: six pieces of information, each separated by a space; the first five pieces of information tell cron when to run the job, and the last piece of information tells cron what the job is.
m h dom mon dow commandm, representing the minute of the hour, h, representing the hour of the day, dom, representing the day of the month, mon, representing the month of the year, dow, representing the day of the week and command, which is the command to be run. For example in our case
0 * * * * /home/myname/myapp/lib/tasks/send_mail.rbThe asterisks (“*“) will tell cron that for that unit of time, the job should run ‘every’. Save and close the file. And that’s it. But sometimes fiddling with crontab on the server can be very hectic and it would be much better if we can configure cron job in our rails application so we can keep the configuration in our source control. We have a gem called whenever that allows us to set up cron jobs from within our Rails apps using Ruby code. Let’s see how you can schedule our background jobs in Rails using Whenever to set up your schedule. Add in your Gemfile.
gem 'whenever', :require => falseRun bundle install to install the gem. Run the
wheneverize
command in your app’s root folder to set up an initial configuration.
wheneverize .The wheneverize command will create an initial
config/schedule.rb
file.
Now add following to schedule.rb
every 1.hour do rake 'send_update_mail:send_mail' end
A whenever plugin for mina
The gemmina-whenever
is a whenever plugin for mina.
Add this line to your application’s Gemfile and run bundle install
gem 'mina-whenever'Modify your deploy.rb
require 'mina/whenever'
desc "Deploys the current version to the server."
task :deploy do
deploy do
.........
on :launch do
invoke :'sidekiq:restart'
.....
end
end
end
I don’t think RAM is an issue these days. For just $20/month I can get a 4 GB Linode instance.
Agree, but it also depends on the resource constraints and the number of tasks to run. In most of the cases this memory consumption won’t make any impact.
Or you can use something designed for background jobs like sidekiq. It’s run only one extra instance of application and also provide error handling.
We have mentioned that. Already