Class: Refresh::Daemon

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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 = {}
  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.



21
22
23
# File 'lib/refresh/daemon.rb', line 21

def options
  @options
end

#unicorn_argsObject

Returns the value of attribute unicorn_args.



21
22
23
# File 'lib/refresh/daemon.rb', line 21

def unicorn_args
  @unicorn_args
end

#unicorn_pidObject

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 =~ options[:full]}
    reload_everything
  else
    hup_unicorn
  end
end

#hup_unicornObject

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

#listenerObject



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=> 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



34
35
36
# File 'lib/refresh/daemon.rb', line 34

def log(msg)
  $stderr.puts msg
end

#reload_everythingObject

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

#runObject



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

#shutdownObject



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_unicornObject



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_configObject



42
43
44
# File 'lib/refresh/daemon.rb', line 42

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