Class: Refresh::Daemon
- Inherits:
-
Object
- Object
- Refresh::Daemon
- Defined in:
- lib/refresh/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(argv) ⇒ 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(argv) ⇒ Daemon
Returns a new instance of Daemon.
24 25 26 27 28 29 30 31 32 |
# File 'lib/refresh/daemon.rb', line 24 def initialize argv @unicorn_args = 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.
21 22 23 |
# File 'lib/refresh/daemon.rb', line 21 def @options end |
#unicorn_args ⇒ Object
Returns the value of attribute unicorn_args.
21 22 23 |
# File 'lib/refresh/daemon.rb', line 21 def unicorn_args @unicorn_args end |
#unicorn_pid ⇒ Object
Returns the value of attribute unicorn_pid.
22 23 24 |
# File 'lib/refresh/daemon.rb', line 22 def unicorn_pid @unicorn_pid end |
Instance Method Details
#handle_change(modified, added, removed) ⇒ Object
66 67 68 69 70 71 72 73 74 |
# 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 =~ [:full]} reload_everything else hup_unicorn end end |
#hup_unicorn ⇒ Object
tell unicorn to gracefully shut down workers
61 62 63 64 |
# File 'lib/refresh/daemon.rb', line 61 def hup_unicorn log "hupping #{unicorn_pid}" Process.kill(:HUP, unicorn_pid) end |
#listener ⇒ Object
76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/refresh/daemon.rb', line 76 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
34 35 36 |
# File 'lib/refresh/daemon.rb', line 34 def log(msg) $stderr.puts msg end |
#reload_everything ⇒ Object
TODO maybe consider doing like: unicorn.bogomips.org/SIGNALS.html
47 48 49 50 51 |
# File 'lib/refresh/daemon.rb', line 47 def reload_everything Process.kill(:QUIT, unicorn_pid) Process.wait(unicorn_pid) start_unicorn end |
#run ⇒ Object
88 89 90 91 92 93 94 95 96 97 |
# 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 ⇒ Object
53 54 55 56 57 58 |
# File 'lib/refresh/daemon.rb', line 53 def shutdown listener.stop Process.kill(:TERM, unicorn_pid) Process.wait(unicorn_pid) exit end |
#start_unicorn ⇒ Object
38 39 40 |
# File 'lib/refresh/daemon.rb', line 38 def start_unicorn @unicorn_pid = Kernel.spawn('unicorn', '-c', unicorn_config, *unicorn_args) end |
#unicorn_config ⇒ Object
42 43 44 |
# File 'lib/refresh/daemon.rb', line 42 def unicorn_config File. 'unicorn.conf.rb', File.dirname(__FILE__) end |