Class: Guard::Resque

Inherits:
Guard
  • Object
show all
Defined in:
lib/guard/resque.rb

Constant Summary collapse

DEFAULT_TERM_CHILD =
1
DEFAULT_SIGNAL =
:SIGTERM
DEFAULT_QUEUE =
'*'.freeze
DEFAULT_COUNT =
1
DEFAULT_TASK_SINGLE =
'resque:work'.freeze
DEFAULT_TASK_MULTIPLE =
'resque:workers'.freeze

Instance Method Summary collapse

Constructor Details

#initialize(watchers = [], options = {}) ⇒ Resque

Allowable options are:

  • :environment e.g. 'test'
  • :task .e.g 'resque:work'
  • :queue e.g. '*'
  • :count e.g. 3
  • :interval e.g. 5
  • :verbose e.g. true
  • :vverbose e.g. true
  • :trace e.g. true
  • :stop_signal e.g. :QUIT or :SIGQUIT
  • :stop_timeout in seconds. Defaults to 5 seconds (one more than resque)
  • :term_child defaults to 1 to use the new signal handling in 2.x


27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/guard/resque.rb', line 27

def initialize watchers = [], options = {}
  @options = options
  @pid = nil
  @stop_signal = options[:stop_signal] || DEFAULT_SIGNAL
  @stop_timeout = options[:stop_timeout] || 5

  @options[:term_child] ||= DEFAULT_TERM_CHILD
  @options[:queue] ||= DEFAULT_QUEUE
  @options[:count] ||= DEFAULT_COUNT
  @options[:task] ||= (@options[:count].to_i == 1) ? DEFAULT_TASK_SINGLE : DEFAULT_TASK_MULTIPLE
  super
end

Instance Method Details

#reloadObject

Called on Ctrl-Z signal



71
72
73
74
# File 'lib/guard/resque.rb', line 71

def reload
  UI.info 'Resque: Restarting...'
  restart
end

#restartObject



86
87
88
89
# File 'lib/guard/resque.rb', line 86

def restart
  stop
  start
end

#run_allObject

Called on Ctrl-/ signal



77
78
79
# File 'lib/guard/resque.rb', line 77

def run_all
  true
end

#run_on_changes(paths) ⇒ Object

Called on file(s) modifications



82
83
84
# File 'lib/guard/resque.rb', line 82

def run_on_changes paths
  restart
end

#startObject



40
41
42
43
44
45
46
47
# File 'lib/guard/resque.rb', line 40

def start
  stop
  UI.info 'Resque: Starting...'
  UI.info 'Resque: ' + [ cmd, env.map{|v| v.join('=')} ].join(' ')

  # launch Resque worker
  @pid = spawn(env, cmd)
end

#stopObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/guard/resque.rb', line 49

def stop
  if @pid
    UI.info 'Resque: Stopping...'
    ::Process.kill @stop_signal, @pid
    begin
      Timeout.timeout(@stop_timeout) do
        ::Process.wait @pid
      end
    rescue Timeout::Error
      UI.info "Resque: Sending SIGKILL as it has taken longer than #{@stop_timeout} to exit. Customize this with the stop_timeout option."
      ::Process.kill :KILL, @pid
      ::Process.wait @pid
    end
    UI.info 'Resque: Stopped'
  end
rescue Errno::ESRCH
  UI.info 'Resque: lost the worker subprocess!'
ensure
  @pid = nil
end