Class: Resqued::QuitAndWait

Inherits:
Object
  • Object
show all
Defined in:
lib/resqued/quit-and-wait.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(grace_seconds:, pidfile:, quiet: false) ⇒ QuitAndWait

Returns a new instance of QuitAndWait.



48
49
50
51
52
# File 'lib/resqued/quit-and-wait.rb', line 48

def initialize(grace_seconds:, pidfile:, quiet: false)
  @grace_seconds = grace_seconds
  @pidfile = pidfile
  @quiet = quiet
end

Instance Attribute Details

#grace_secondsObject (readonly)

Returns the value of attribute grace_seconds.



54
55
56
# File 'lib/resqued/quit-and-wait.rb', line 54

def grace_seconds
  @grace_seconds
end

#pidfileObject (readonly)

Returns the value of attribute pidfile.



54
55
56
# File 'lib/resqued/quit-and-wait.rb', line 54

def pidfile
  @pidfile
end

#quietObject (readonly)

Returns the value of attribute quiet.



54
55
56
# File 'lib/resqued/quit-and-wait.rb', line 54

def quiet
  @quiet
end

Class Method Details

.exec!(argv) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/resqued/quit-and-wait.rb', line 5

def self.exec!(argv)
  options = { grace_seconds: 30 }

  opts = OptionParser.new do |opts| # rubocop: disable Lint/ShadowingOuterLocalVariable
    opts.banner = "      Usage: resqued quit-and-wait PIDFILE [--grace-period SECONDS]\n\n      Use this as a preStop script in kubernetes. This script will send a SIGQUIT to\n      resqued immediately, and then sleep until either resqued exits or until the\n      grace period is nearing expiration. This script exits 0 if resqued exited and\n      99 otherwise.\n    USAGE\n\n    opts.on \"-h\", \"--help\", \"Show this message\" do\n      puts opts\n      exit\n    end\n\n    opts.on \"-v\", \"--version\", \"Show the version\" do\n      require \"resqued/version\"\n      puts Resqued::VERSION\n      exit\n    end\n\n    opts.on \"--grace-period SECONDS\", Numeric, \"Grace period provided to container runtime (default 30)\" do |v|\n      options[:grace_seconds] = v\n    end\n\n    opts.on \"--quiet\" do\n      options[:quiet] = true\n    end\n  end\n\n  argv = opts.parse(argv)\n  if argv.size != 1\n    puts opts\n    exit 1\n  end\n  options[:pidfile] = argv.shift\n\n  new(**options).exec!\nend\n"

Instance Method Details

#exec!Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/resqued/quit-and-wait.rb', line 56

def exec!
  start = Time.now
  stop_at = start + grace_seconds - 5
  pid = File.read(pidfile).to_i

  log "kill -QUIT #{pid} (resqued-master)"
  Process.kill(:QUIT, pid)

  while Time.now < stop_at
    begin
      # check if pid is still alive.
      Process.kill(0, pid)
    rescue Errno::ESRCH # no such process, it exited!
      log "ok: resqued-master with pid #{pid} exited"
      exit 0
    end
  end

  log "giving up, resqued-master with pid #{pid} is still running."
  exit 99
end

#log(message) ⇒ Object



78
79
80
81
82
# File 'lib/resqued/quit-and-wait.rb', line 78

def log(message)
  return if quiet

  puts "#{Time.now.strftime('%H:%M:%S.%L')} #{message}"
end