class Refresh::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(argv)
click to toggle source
# File lib/refresh/daemon.rb, line 24 def initialize argv @unicorn_args = 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/refresh/daemon.rb, line 66 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/refresh/daemon.rb, line 61 def hup_unicorn log "hupping #{unicorn_pid}" Process.kill(:HUP, unicorn_pid) end
listener()
click to toggle source
# File lib/refresh/daemon.rb, line 76 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/refresh/daemon.rb, line 34 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/refresh/daemon.rb, line 47 def reload_everything Process.kill(:QUIT, unicorn_pid) Process.wait(unicorn_pid) start_unicorn end
run()
click to toggle source
# File lib/refresh/daemon.rb, line 88 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/refresh/daemon.rb, line 53 def shutdown listener.stop Process.kill(:TERM, unicorn_pid) Process.wait(unicorn_pid) exit end
start_unicorn()
click to toggle source
# File lib/refresh/daemon.rb, line 38 def start_unicorn @unicorn_pid = Kernel.spawn('unicorn', '-c', unicorn_config, *unicorn_args) end
unicorn_config()
click to toggle source
# File lib/refresh/daemon.rb, line 42 def unicorn_config File.expand_path 'unicorn.conf.rb', File.dirname(__FILE__) end