ruby daemons

21
Ruby Daemons Creston Jamison Ruby Tree Software, Inc

Upload: creston-jamison

Post on 20-Jun-2015

103 views

Category:

Software


4 download

DESCRIPTION

Learn how to use daemons with your ruby based application like Ruby on Rails. See how it compares to other queueing systems like Queue Classic, Sidekiq, or Delayed Job.

TRANSCRIPT

Page 1: Ruby Daemons

Ruby DaemonsCreston JamisonRuby Tree Software, Inc.

Page 2: Ruby Daemons
Page 3: Ruby Daemons

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!

Page 4: Ruby Daemons

1. Reliability2. Speed

Page 5: Ruby Daemons

Background Processing Options Delayed Job Beanstalkd Resque Sidekiq Queue Classic Daemons

Page 6: Ruby Daemons

Delayed Job

Uses rake Uses full rails environment Best for long running and infrequent

jobs

Page 7: Ruby Daemons

Beanstalkd

Fast queue Does not poll Good for raw speed Authentication and queue

management ?

Page 8: Ruby Daemons

Resque

Redis based queue system Requires Redis Comes with an admin interface Uses polling

Page 9: Ruby Daemons

Sidekiq

Redis based queue system Requires Redis Comes with an admin interface Uses polling Uses threads Code needs to be thread safe

Page 10: Ruby Daemons

Queue Classic

Postgres based queue system Requires postgresql Avoids separate queue process Can avoid polling

Page 11: Ruby Daemons

Queue vs. Worker

Page 12: Ruby Daemons

Daemons

Converts a script into a daemon Not a queue system A way to create a worker process

Page 13: Ruby Daemons

What is a daemon?

A computer program that runs as a background process

Daemon processes typically end in ‘d’

For example: sshd

Page 14: Ruby Daemons

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

Page 15: Ruby Daemons

Create a control file

require 'rubygems'require 'bundler/setup' require 'daemons'

Daemons.run('lib/daemons/event_worker.rb')

Page 16: Ruby Daemons

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

Page 17: Ruby Daemons

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

Page 18: Ruby Daemons

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

Page 19: Ruby Daemons

Gotchas

Need rock solid error handling Otherwise your process dies Need a way to skip over errors and

come back to them

Page 20: Ruby Daemons

Extra Creditjstorimer.com/2012/04/19/daemon-processes-in-ruby.html

labs.headlondon.com/2010/07/skinny-daemons/

Page 21: Ruby Daemons

Questions?Creston Jamison@crestonjamison

[email protected]