Background processing is an important tool in web development. Some
things just can't optimized enough for the normal request/response cycle
of a web site and need to be run asynchronously. Ruby on Rails doesn't provide
you with any real out of the box solution. There are a lot of external
options available and this part of Rails is lacking any real convention
or standard.
In this presentation, we will talk about how to choose and implement a
solution that fits your needs. We will start with some basic options
using built in Rails tools and we will cover some of the more popular
solutions solutions out there such as BackgrounDRb, Background Job,
Delayed Job, Workling and more.
Rob Mack has been working with Rails professionally on and off since
2005. Rob currently works for VitalSource Technologies.
8. Do Some Work
$running = true
Signal.trap(quot;TERMquot;) do
$running = false
end
while($running) do
# Do some work
sleep 10
end
lib/daemons/my_worker.rb
Thursday, April 30, 2009
9. Kick It Off
lib/daemons/my_worker_ctl start
lib/daemons/my_worker_ctl stop
Thursday, April 30, 2009
10. Spawn
http://github/tra/spawn
Thursday, April 30, 2009
11. Spawn - Do Some Work
def my_action
flash[:notice] = quot;Processing ... quot;
spawn do
# do some processing
end
end
Thursday, April 30, 2009
21. Do Some Work
class MyWorker < BackgrounDRb::MetaWorker
set_worker_name :my_worker
def create(args = nil)
# initialization goes here
end
def do_work(user_id = nil)
# do some work
end
end
def my_action
flash[:notice] = quot;Processing ...quot;
MiddleMan.worker(:my_worker).async_do_work(:arg => @user.id)
end
Thursday, April 30, 2009
22. Persistent Work
def my_action
MiddleMan(:my_worker).enq_do_work(:job_key => quot;unique_keyquot;)
end
Thursday, April 30, 2009
23. Poll Status
class MyWorker < BackgrounDRb::MetaWorker
set_worker_name :my_worker
def create(args = nil)
# initialization goes here
end
def do_work(user_id = nil)
# do some work
register_status quot;almost donequot;
end
end
Thursday, April 30, 2009
24. Poll Status
def my_action
@job_key = MiddleMan.new_worker(
:worker => :my_worker,
:job_key => quot;unique_keyquot;
)
MiddleMan.worker(:my_worker, @job_key).
do_work(@user.id)
end
def check_status
@status = MiddleMan.worker(:my_worker, @job_key).
ask_status
end
Thursday, April 30, 2009
35. Do Some Work
class MyWorker
attr_accessor :user_id
def initialize(user)
self.user_id = user_id
end
def perform
User.find user_id
# do some work
end
end
Thursday, April 30, 2009
36. Do Some Work
def my_action
flash[:notice] = quot;Processing ...quot;
Delayed::Job.enqueue MyWorker.new(@user.id)
end
def my_action
flash[:notice] = quot;Processing ...quot;
MyMailer.new(@user.id).send_later(:deliver_newsletter)
end
Thursday, April 30, 2009
40. Do Some Work
class MyWorker < Workling::Base
def do_something(options)
# do some work
end
end
def my_action
flash[:notice] = quot;Processing ...quot;
MyWorker.async_do_something :user_id => @user.id
end
Thursday, April 30, 2009
41. The Worker Process
with spawn:
script/workling_client start
with starling:
sudo starling -d
script/workling_client start
Thursday, April 30, 2009
42. Starling
http://github.com/starling/starling/
Thursday, April 30, 2009
43. Even More Options
• Background Fu - http://github.com/ncr/
background-fu
• Roofus Scheduler - http://github.com/
jmettraux/rufus-scheduler
• AP4R - http://rubyforge.org/projects/
ap4r/
Thursday, April 30, 2009
45. Kestrel
http://github.com/robey/kestrel/
Thursday, April 30, 2009
46. Rabbit MQ
http://www.rabbitmq.com/
Thursday, April 30, 2009
47. Active MQ
http://activemq.apache.org/
Thursday, April 30, 2009
48. Amazon SQS
http://aws.amazon.com/sqs/
Thursday, April 30, 2009
49. Monitoring
• god - http://github.com/mojombo/
god/
• launchd - OS X
• monit - http://mmonit.com/monit/
Thursday, April 30, 2009
50. Persistant?
Yes
No
Scaling concerns or fear
I enjoy
of commitment?
managing a
process?
No Yes
No
Yes
Delayed Job
Background Job
Spawn Workling
Daemons
Are you the Kestrel
Starling Probably Not Yes
next Twitter? Rabbit MQ
Thursday, April 30, 2009
51. Thanks
rob@robmack.com
twitter.com/robmack
Thursday, April 30, 2009