Class: Guard::Sidekiq

Inherits:
Plugin
  • Object
show all
Defined in:
lib/guard/sidekiq.rb

Constant Summary collapse

DEFAULT_SIGNAL =
:TERM
DEFAULT_CONCURRENCY =
1

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Sidekiq

Allowable options are:

- :environment  e.g. 'test'
- :queue e.g. 'default'
- :timeout e.g. 5
- :config e.g. config/sidekiq.yml
- :concurrency, e.g. 20
- :verbose e.g. true
- :stop_signal e.g. :TERM, :QUIT or :SIGQUIT
- :logfile e.g. log/sidekiq.log (defaults to STDOUT)
- :require e.g. ./sidekiq_helper.rb


20
21
22
23
24
25
26
27
# File 'lib/guard/sidekiq.rb', line 20

def initialize(options = {})
  @options = options
  @pid = nil
  @stop_signal = options[:stop_signal] || DEFAULT_SIGNAL
  @options[:concurrency] ||= DEFAULT_CONCURRENCY
  @options[:verbose] = @options.fetch(:verbose, true)
  super
end

Instance Method Details

#reloadObject

Called on Ctrl-Z signal



60
61
62
63
# File 'lib/guard/sidekiq.rb', line 60

def reload
  UI.info 'Restarting sidekiq...'
  restart
end

#restartObject



75
76
77
78
# File 'lib/guard/sidekiq.rb', line 75

def restart
  stop
  start
end

#run_allObject

Called on Ctrl-/ signal



66
67
68
# File 'lib/guard/sidekiq.rb', line 66

def run_all
  true
end

#run_on_changes(paths) ⇒ Object

Called on file(s) modifications



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

def run_on_changes(paths)
  restart
end

#startObject



29
30
31
32
33
34
35
36
# File 'lib/guard/sidekiq.rb', line 29

def start
  stop
  UI.info 'Starting up sidekiq...'
  UI.info cmd

  # launch Sidekiq worker
  @pid = spawn({}, cmd)
end

#stopObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/guard/sidekiq.rb', line 38

def stop
  if @pid
    UI.info 'Stopping sidekiq...'
    ::Process.kill @stop_signal, @pid
    begin
      Timeout.timeout(15) do
        ::Process.wait @pid
      end
    rescue Timeout::Error
      UI.info 'Sending SIGKILL to sidekiq, as it\'s taking too long to shutdown.'
      ::Process.kill :KILL, @pid
      ::Process.wait @pid
    end
    UI.info 'Stopped process sidekiq'
  end
rescue Errno::ESRCH
  UI.info 'Guard::Sidekiq lost the Sidekiq worker subprocess!'
ensure
  @pid = nil
end