Ruby Daemons Creston Jamison Ruby Tree Software, Inc
Jun 20, 2015
Ruby DaemonsCreston JamisonRuby Tree Software, Inc.
GAMEhud
HTTP API accepts event and player based data from game clients.
Incoming data needs to be distributed between 10-20 tables.
Don’t want to do that in the request. Need to do background processing!
1. Reliability2. Speed
Background Processing Options Delayed Job Beanstalkd Resque Sidekiq Queue Classic Daemons
Delayed Job
Uses rake Uses full rails environment Best for long running and infrequent
jobs
Beanstalkd
Fast queue Does not poll Good for raw speed Authentication and queue
management ?
Resque
Redis based queue system Requires Redis Comes with an admin interface Uses polling
Sidekiq
Redis based queue system Requires Redis Comes with an admin interface Uses polling Uses threads Code needs to be thread safe
Queue Classic
Postgres based queue system Requires postgresql Avoids separate queue process Can avoid polling
Queue vs. Worker
Daemons
Converts a script into a daemon Not a queue system A way to create a worker process
What is a daemon?
A computer program that runs as a background process
Daemon processes typically end in ‘d’
For example: sshd
Setting up a daemon
Install daemons gem Create a control file
event_worker_control.rb Create a loop in the script called by the
control fileevent_worker.rb
Create a control file
require 'rubygems'require 'bundler/setup' require 'daemons'
Daemons.run('lib/daemons/event_worker.rb')
Loop in your script
$: << File.expand_path(".", File.dirname(__FILE__))
require ‘event_processor'
event_processor = EventProcessor.new
loop do event_processor.process sleep(5)end
Control your daemon
ruby daemon-name_control.rb start ruby daemon-name_control.rb restart ruby daemon-name_control.rb stop
ruby lib/daemons/event_worker_control.rbstart -- production
PID file
Capistrano
before 'deploy:update_code', 'deploy:stop_workers'
desc "Stop the worker processes“
task :stop_workers, :roles => :app do
run "cd #{current_path};
ruby lib/daemons/event_worker_control.rb
stop -- production“
end
Gotchas
Need rock solid error handling Otherwise your process dies Need a way to skip over errors and
come back to them
Extra Creditjstorimer.com/2012/04/19/daemon-processes-in-ruby.html
labs.headlondon.com/2010/07/skinny-daemons/
Questions?Creston Jamison@crestonjamison