Class: Cuba::Bin::Daemon
- Inherits:
-
Object
- Object
- Cuba::Bin::Daemon
- 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
-
#options ⇒ Object
Returns the value of attribute options.
-
#unicorn_args ⇒ Object
Returns the value of attribute unicorn_args.
-
#unicorn_pid ⇒ Object
Returns the value of attribute unicorn_pid.
Instance Method Summary collapse
- #handle_change(modified, added, removed) ⇒ Object
-
#hup_unicorn ⇒ Object
tell unicorn to gracefully shut down workers.
-
#initialize ⇒ Daemon
constructor
A new instance of Daemon.
- #listener ⇒ Object
- #log(msg) ⇒ Object
-
#reload_everything ⇒ Object
TODO maybe consider doing like: unicorn.bogomips.org/SIGNALS.html.
- #run ⇒ Object
- #shutdown ⇒ Object
- #start_unicorn ⇒ Object
- #unicorn_config ⇒ Object
Constructor Details
#initialize ⇒ Daemon
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 = {} [:pattern] ||= DEFAULT_RELOAD_PATTERN [:full] ||= DEFAULT_FULL_RELOAD_PATTERN [:force_polling] ||= false self end |
Instance Attribute Details
#options ⇒ Object
Returns the value of attribute options.
22 23 24 |
# File 'lib/cuba/bin/daemon.rb', line 22 def @options end |
#unicorn_args ⇒ Object
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_pid ⇒ Object
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 =~ [:full]} reload_everything else hup_unicorn end end |
#hup_unicorn ⇒ Object
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 |
#listener ⇒ Object
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=> [:force_polling]) do |modified, added, removed| handle_change(modified, added, removed) end x.only([ [:pattern], [: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_everything ⇒ Object
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 |
#run ⇒ Object
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 |
#shutdown ⇒ Object
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_unicorn ⇒ Object
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_config ⇒ Object
43 44 45 |
# File 'lib/cuba/bin/daemon.rb', line 43 def unicorn_config File. 'unicorn.conf.rb', File.dirname(__FILE__) end |