Class: Cuba::Bin::Daemon

Inherits:
Object
  • Object
show all
Defined in:
lib/cuba/bin/daemon.rb

Constant Summary collapse

DEFAULT_RELOAD_PATTERN =
%r(\.(?:builder #{extensions.join('|')})$)
DEFAULT_FULL_RELOAD_PATTERN =
/^Gemfile(?:\.lock)?$/
IGNORE_PATTERNS =

todo> make configurable

[/\.direnv/, /\.sass-cache/, /^tmp/]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeDaemon

Returns a new instance of Daemon.



25
26
27
28
29
30
31
32
33
# 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

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



22
23
24
# File 'lib/cuba/bin/daemon.rb', line 22

def options
  @options
end

#unicorn_argsObject

Returns the value of attribute unicorn_args.



22
23
24
# File 'lib/cuba/bin/daemon.rb', line 22

def unicorn_args
  @unicorn_args
end

#unicorn_pidObject

Returns the value of attribute unicorn_pid.



23
24
25
# File 'lib/cuba/bin/daemon.rb', line 23

def unicorn_pid
  @unicorn_pid
end

Instance Method Details

#handle_change(modified, added, removed) ⇒ Object



67
68
69
70
71
72
73
74
75
# 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_unicornObject

tell unicorn to gracefully shut down workers



62
63
64
65
# File 'lib/cuba/bin/daemon.rb', line 62

def hup_unicorn
  log "hupping #{unicorn_pid}"
  Process.kill(:HUP, unicorn_pid)
end

#listenerObject



77
78
79
80
81
82
83
84
85
86
87
# 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) ⇒ Object



35
36
37
# File 'lib/cuba/bin/daemon.rb', line 35

def log(msg)
  $stderr.puts msg
end

#reload_everythingObject

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



48
49
50
51
52
# File 'lib/cuba/bin/daemon.rb', line 48

def reload_everything
  Process.kill(:QUIT, unicorn_pid)
  Process.wait(unicorn_pid)
  start_unicorn
end

#runObject



89
90
91
92
93
94
95
96
97
98
# 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

#shutdownObject



54
55
56
57
58
59
# File 'lib/cuba/bin/daemon.rb', line 54

def shutdown
  listener.stop
  Process.kill(:TERM, unicorn_pid)
  Process.wait(unicorn_pid)
  exit
end

#start_unicornObject



39
40
41
# File 'lib/cuba/bin/daemon.rb', line 39

def start_unicorn
  @unicorn_pid = Kernel.spawn('unicorn', '-c', unicorn_config, *unicorn_args)
end

#unicorn_configObject



43
44
45
# File 'lib/cuba/bin/daemon.rb', line 43

def unicorn_config
  File.expand_path 'unicorn.conf.rb', File.dirname(__FILE__)
end