Class: Sidekiq::Ctl

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

Defined Under Namespace

Classes: Status

Constant Summary collapse

DEFAULT_KILL_TIMEOUT =
10
CMD =
File.basename($0)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stage, pidfile, timeout) ⇒ Ctl

Returns a new instance of Ctl.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/sidekiq/ctl.rb', line 32

def initialize(stage, pidfile, timeout)
  @stage = stage
  @pidfile = pidfile
  @kill_timeout = timeout

  done('No pidfile given', :error) if !pidfile
  done("Pidfile #{pidfile} does not exist", :warn) if !File.exist?(pidfile)
  done('Invalid pidfile content', :error) if pid == 0

  fetch_process

  begin
    send(stage)
  rescue NoMethodError
    done "Invalid command: #{stage}", :error
  end
end

Instance Attribute Details

#kill_timeoutObject (readonly)

Returns the value of attribute kill_timeout.



10
11
12
# File 'lib/sidekiq/ctl.rb', line 10

def kill_timeout
  @kill_timeout
end

#pidfileObject (readonly)

Returns the value of attribute pidfile.



10
11
12
# File 'lib/sidekiq/ctl.rb', line 10

def pidfile
  @pidfile
end

#stageObject (readonly)

Returns the value of attribute stage.



10
11
12
# File 'lib/sidekiq/ctl.rb', line 10

def stage
  @stage
end

Class Method Details



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/sidekiq/ctl.rb', line 12

def self.print_usage
  puts "#{CMD} - control Sidekiq from the command line."
  puts
  puts "Usage: #{CMD} quiet <pidfile> <kill_timeout>"
  puts "       #{CMD} stop <pidfile> <kill_timeout>"
  puts "       #{CMD} status <section>"
  puts
  puts "       <pidfile> is path to a pidfile"
  puts "       <kill_timeout> is number of seconds to wait until Sidekiq exits"
  puts "       (default: #{Sidekiq::Ctl::DEFAULT_KILL_TIMEOUT}), after which Sidekiq will be KILL'd"
  puts
  puts "       <section> (optional) view a specific section of the status output"
  puts "       Valid sections are: #{Sidekiq::Ctl::Status::VALID_SECTIONS.join(', ')}"
  puts
  puts "Be sure to set the kill_timeout LONGER than Sidekiq's -t timeout.  If you want"
  puts "to wait 60 seconds for jobs to finish, use `sidekiq -t 60` and `sidekiqctl stop"
  puts " path_to_pidfile 61`"
  puts
end

Instance Method Details

#done(msg, error = nil) ⇒ Object



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

def done(msg, error = nil)
  puts msg
  exit(exit_signal(error))
end

#exit_signal(error) ⇒ Object



65
66
67
# File 'lib/sidekiq/ctl.rb', line 65

def exit_signal(error)
  (error == :error) ? 1 : 0
end

#fetch_processObject



50
51
52
53
54
55
56
57
58
# File 'lib/sidekiq/ctl.rb', line 50

def fetch_process
  Process.kill(0, pid)
rescue Errno::ESRCH
  done "Process doesn't exist", :error
# We were not allowed to send a signal, but the process must have existed
# when Process.kill() was called.
rescue Errno::EPERM
  return pid
end

#pidObject



69
70
71
# File 'lib/sidekiq/ctl.rb', line 69

def pid
  @pid ||= File.read(pidfile).to_i
end

#quietObject



73
74
75
# File 'lib/sidekiq/ctl.rb', line 73

def quiet
  `kill -TSTP #{pid}`
end

#stopObject Also known as: shutdown



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/sidekiq/ctl.rb', line 77

def stop
  `kill -TERM #{pid}`
  kill_timeout.times do
    begin
      Process.kill(0, pid)
    rescue Errno::ESRCH
      FileUtils.rm_f pidfile
      done 'Sidekiq shut down gracefully.'
    rescue Errno::EPERM
      done 'Not permitted to shut down Sidekiq.'
    end
    sleep 1
  end
  `kill -9 #{pid}`
  FileUtils.rm_f pidfile
  done 'Sidekiq shut down forcefully.'
end