class Cuba::Bin::Daemon

Constants

DEFAULT_FULL_RELOAD_PATTERN
DEFAULT_RELOAD_PATTERN
IGNORE_PATTERNS

todo> make configurable

Attributes

options[RW]
unicorn_args[RW]
unicorn_pid[RW]

Public Class Methods

new() click to toggle source
# File lib/cuba/bin/daemon.rb, line 25
def initialize
  @unicorn_args = Bin.argv
  # @options, @unicorn_args = options, unicorn_args
  @options = {}
  options[:pattern]       ||= DEFAULT_RELOAD_PATTERN
  options[:full]          ||= DEFAULT_FULL_RELOAD_PATTERN
  options[:force_polling] ||= false
  self
end

Public Instance Methods

handle_change(modified, added, removed) click to toggle source
# File lib/cuba/bin/daemon.rb, line 67
def handle_change(modified, added, removed)
  log "File change event detected: #{{modified: modified, added: added, removed: removed}.inspect}"

  if (modified + added + removed).index {|f| f =~ options[:full]}
    reload_everything
  else
    hup_unicorn
  end
end
hup_unicorn() click to toggle source

tell unicorn to gracefully shut down workers

# File lib/cuba/bin/daemon.rb, line 62
def hup_unicorn
  log "hupping #{unicorn_pid}"
  Process.kill(:HUP, unicorn_pid)
end
listener() click to toggle source
# File lib/cuba/bin/daemon.rb, line 77
def listener
  @listener ||= begin
    x = Listen.to(Dir.pwd, :relative_paths=>true, :force_polling=> options[:force_polling]) do |modified, added, removed|
      handle_change(modified, added, removed)
    end

    x.only([ options[:pattern], options[:full] ])
    IGNORE_PATTERNS.map{|ptrn| x.ignore(ptrn) }
    x
  end
end
log(msg) click to toggle source
# File lib/cuba/bin/daemon.rb, line 35
def log(msg)
  $stderr.puts msg
end
reload_everything() click to toggle source

TODO maybe consider doing like: unicorn.bogomips.org/SIGNALS.html

# File lib/cuba/bin/daemon.rb, line 48
def reload_everything
  Process.kill(:QUIT, unicorn_pid)
  Process.wait(unicorn_pid)
  start_unicorn
end
run() click to toggle source
# File lib/cuba/bin/daemon.rb, line 89
def run
  that = self
  Signal.trap("INT") { |signo| that.shutdown }
  Signal.trap("EXIT") { |signo| that.shutdown }
  listener.start
  start_unicorn

  # And now we just want to keep the thread alive--we're just waiting around to get interrupted at this point.
  sleep(99999) while true
end
shutdown() click to toggle source
# File lib/cuba/bin/daemon.rb, line 54
def shutdown
  listener.stop
  Process.kill(:TERM, unicorn_pid)
  Process.wait(unicorn_pid)
  exit
end
start_unicorn() click to toggle source
# File lib/cuba/bin/daemon.rb, line 39
def start_unicorn
  @unicorn_pid = Kernel.spawn('unicorn', '-c', unicorn_config, *unicorn_args)
end
unicorn_config() click to toggle source
# File lib/cuba/bin/daemon.rb, line 43
def unicorn_config
  File.expand_path 'unicorn.conf.rb', File.dirname(__FILE__)
end