Tuesday, January 19, 2010

Navvy Background Job Processor

Navvy adalah background job processor. Navvy dibuat oleh Jeff Kreeftmeijer yang diinspirasi oleh background job processor yang bernama delayed_job.

Perbedaan utama navvy dengan delayed_job adalah penggunaan database. Dalam setiap job yang ada, navvy menyimpannya ke dalam sebuah tabel create_jobs dibandingkan dengan delayed_job yang hanya tersimpan pada memori. Dengan begitu, background job dengan navvy bisa dipastikan tidak hilang jika terjadi mati lampu / blackout. Namun, hal ini juga mengakibatkan masalah di sisi performansi, yaitu karena melakukan akses terhadap database, waktu yang diperlukan lebih besar dibandingkan delayed_job. Berikut ini adalah tutorial dalam penggunaan navvy.


1. Setup

[email protected]:~# gem install navvy
[email protected]:~/lab$ rails app_with_navvy
[email protected]:~/lab$ cd app_with_navvy

2. Konfigurasi

# config/environment.rb
# ----- cut -----
Rails::Initializer.run do |config|
  config.gem 'navvy' # ----> add this line
# ----- cut -----
[email protected]:~/lab/app_with_navvy$ touch config/initializers/navvy.rb
# config/initialiazers/navvy.rb
require 'navvy/job/active_record'
[email protected]:~/lab/app_with_navvy$ ./script/generate navvy
[email protected]:~/lab/app_with_navvy$ rake db:migrate
# Rakefile
require(File.join(File.dirname(__FILE__), 'config', 'boot'))

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'

require 'tasks/rails'
require 'navvy/tasks' # ---> add this line

3. Buat resource
[email protected]:~/lab/app_with_navvy$ ./script/generate scaffold post title:string content:text name:string
[email protected]:~/lab/app_with_navvy$ rake db:migrate
[email protected]:~/lab/app_with_navvy$ touch lib/auto_post.rb

4. Buat script untuk dijalankan oleh navvy
# lib/auto_post.rb
class AutoPost
  def self.auto_create
    puts "creating post..."
    Post.create(:title => "my title", :content => "my content", :name => "my name")
  end
end
# config/initializers/navvy.rb
require 'navvy/job/active_record'

Navvy::Job.enqueue(AutoPost, :auto_create) # ---> add this line

5. Jalankan navvy worker. Untuk menghentikannya, tekan CTRL-C.
[email protected]:~/lab/app_with_navvy$ rake navvy:work
(in /home/aji/lab/app_with_navvy)
*** Starting ***
creating post...
* AutoPost.auto_create() => #<post:0xb755f070>
creating post...
* AutoPost.auto_create() => #<post:0xb751f77c>
creating post...
* AutoPost.auto_create() => #<post:0xb74d3b4c>

6. Selesai. Sweet..


Update:
Navvy dan delayed_job sama-sama menggunakan database dalam melakukan background job processing, perbedaannya adalah delayed_job hanya dapat digunakan pada ORM ActiveRecord, sedangkan navvy dapat digunakan oleh ORM selain ActiveRecord. Oleh karena sama-sama menggunakan database, secara teori navvy performansinya setara dengan delayed_job. Thanks to Jeff Kreeftmeijer for the correction.



2 comments:

  1. Hi,

    I really appreciate you taking the time to write a guide about Navvy. Great work! :)

    My Indonesian is really bad, so I pulled your post through Google translate to be able to read it. Please correct me if I'm wrong here.

    You state that delayed_job stores it's jobs in memory. Actually, this is false. DJ is database backed too, the difference between the two is that Navvy is database agnostic. This way you can use other ORMs with Navvy, while DJ only officially supports ActiveRecord.

    In theory, Navvy shouldn't be any slower than DJ since they both call a database for their jobs. The general idea is the same in both cases and Navvy is heavily inspired by DJ.

    Thanks again for your post!

    - Jeff

    ReplyDelete
  2. Hi Jeff, thanks for visiting my blog and the correction. Sorry for my mistake, I'll update this post.

    ReplyDelete

© Railsmine