Clockwork and delayed job and PostgreSQL

Clockwork is simple and very useful tool for scheduling tasks instead of using cron jobs in ruby.

First things first, add clockwork to your Gemfile:

gem 'clockwork'

Configuration is kept in config/clock.rb. It uses plain ruby DSL. Most amazing thing is to use custom handler which allows you to skip loading Rails environment and insert scheduled jobs directly into delyed job database table.

# Clockfile
require 'pg'
require 'yaml'
db = YAML.load_file(File.dirname(__FILE__) + "/database.yml")[ENV['RAILS_ENV'] || "development"]
conn = PGconn.connect(db['host'], nil, nil, nil, db['database'], db['username'], db['password'])

require 'clockwork'
include Clockwork


handler do |job|
  db_time = Time.now.utc.strftime("%Y-%m-%d %H:%M:%S")
  handler = "--- !ruby/object:ScheduledJob \njob: #{job}\n"
  conn.exec("INSERT INTO \"delayed_jobs\" (\"failed_at\", \"locked_by\", \"created_at\", \"handler\", \"updated_at\", \"priority\", \"run_at\", \"attempts\", \"locked_at\", \"last_error\") VALUES(NULL, NULL, '#{db_time}', '#{handler}', '#{db_time}', 0, '#{db_time}', 0, NULL, NULL)")
end

every(30.minutes, 'reactivate_paid_accounts')
# and more every blocks with definitions

And finally example of ScheduledJob:

# app/workers/scheduled_job.rb
class ScheduledJob
  attr_accessor :job
  SUPPORTED_JOBS = { 
                     :reactivate_paid_accounts    => 'Account.reactivate_paid'           
  }

  def initialize(job)
    self.job = job
    unless SUPPORTED_JOBS.has_key?(job.to_sym)
      raise "Unsupported scheduled action - #{job}"
    end
  end

  def perform
    klass, method = SUPPORTED_JOBS[job.to_sym].split('.')
    Kernel.const_get(klass).send(method.to_sym)
  end

  def display_name
    "Job/#{job}"
  end
end
Filed under  //   cron   delayed_job   postgresql   ruby  

Comments [0]

Cron tasks with style: Whenever

Whenever is simple and very useful tool for defining cron jobs in ruby.

First things first, add whenever to your Gemfile (you use bundler, right?):

gem 'whenever', :require => false

Configuration is kept in config/schedule.rb. It uses plain ruby DSL. Periodically executed code is wrapper in every blocks, in which you specify your command or rake task to be run.

It is good idea to set output redirection and disable default cron behaviour of emailing standard output (otherwise you end up with thousands local emails).

set :output, '/home/web/fakturoid/shared/log/cron.log'
env :MAILTO, "''"

Basic example can be running rake task for cleaning stale database sessions:

every 2.hours do
  rake "db:sessions:clean_nonactive"
end

I prefer application related tasks to be grouped under app namespace:

every 1.day, :at => '0:30 am' do
  rake "app:mark_as_overdue"
  rake "app:invoices_from_generators"
end

Backups should be also scheduled to run nightly:

every 1.day, :at => '2:30 am' do
  REMOTE = "someaccount@someserver.com:backups"
  rake "backup BACKUP_REMOTE=#{REMOTE}"
end

As you can see, I use rake tasks to perform scheduled jobs. But if you have background job queue system in place, it would be wise to use cron only for placing task in queue.

To generate cron configuration from ruby DSL run whenever command (via bundle exec whenever). But real fun starts with capistrano integration to regenerate cron settings after deploy. All your cron tasks will run under user specified in capistrano application variable.

# whenever.rb capistrano task
namespace :deploy do
  desc "Update the crontab file"
  task :update_crontab, :roles => :db do
    run "cd #{current_path} && /opt/ruby-enterprise/bin/bundle exec whenever --update-crontab #{application}"
  end
end
after "deploy:restart", "deploy:update_crontab"
Filed under  //   capistrano   cron   ruby   whenever  

Comments [0]

How to start with Rails

Lots of my friends are starting with Ruby on Rails development and all of them have similar questions. They often have previous PHP experience. This post is about directions and sources which helped me. You may also read my previous article about setting Mac for RoR development.

1) Ruby

You can learn Ruby (the language) as you go by learning primary Rails, it worked for me.

2) Rails

3) Git and GitHub

Install git and get familiar with it.

  • Book: Pro Git – free html book
  • Screeencasts: GitCasts
  • GitHub: Grab account, you will use GitHub a lot. Almost all Rails related code is hosted there (including plugins, gems and RoR itself).

4) Asking questions

5) Czech sources

Bonus sources

  • RubyFlow – community links
  • Planet – rails related blog aggregator
  • RubyShow – podcast with Dan Benjamin and Jason Seifer

Now go and develop some great Rails application. Remember, it is not about tools (Rails in this case), but about what you can do with them.

Filed under  //   git   rails   ruby  

Comments [3]

About

Web developer. Visit my links. Follow me on twitter.