Class: WorkerKiller::PumaPluginNg

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/worker_killer/puma_plugin_ng.rb

Defined Under Namespace

Classes: PumaLogWrapper

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePumaPluginNg



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

def initialize
  @killer = ::WorkerKiller::Killer::Puma.new(worker_num: nil, puma_plugin: self)
  @worker_num = nil
  @debug = false

  @puma_server = nil

  @force_restart = %w[t 1].include?(ENV.fetch('WORKER_KILLER_PUMA_AGGRESSIVE', 'false').to_s.downcase[0].to_s)
  @kill_queue ||= Set.new
  @last_restarted_at = 0.0
end

Instance Attribute Details

#kill_queueObject

Returns the value of attribute kill_queue.



25
26
27
# File 'lib/worker_killer/puma_plugin_ng.rb', line 25

def kill_queue
  @kill_queue
end

#killerObject

Returns the value of attribute killer.



25
26
27
# File 'lib/worker_killer/puma_plugin_ng.rb', line 25

def killer
  @killer
end

#puma_serverObject

Returns the value of attribute puma_server.



25
26
27
# File 'lib/worker_killer/puma_plugin_ng.rb', line 25

def puma_server
  @puma_server
end

Instance Method Details

#config(dsl) ⇒ Object

Этот метод зовётся при ИНИЦИАЛИЗАЦИИ плагина внути master-процесса, в самомо начале тут можно выполнить конфигурацию чегоднибудь нужного



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/worker_killer/puma_plugin_ng.rb', line 41

def config(dsl)
  if %w[t 1].include?(ENV.fetch('WORKER_KILLER_DEBUG', 'false').to_s.downcase[0].to_s)
    @debug = true
  end

  cb = if dsl.respond_to?(:before_worker_boot)
    :before_worker_boot
  else
    # DEPRECATED
    :on_worker_boot
  end

  puts "SEND:#{cb}"

  dsl.send(cb) do |num|
    @killer.worker_num = num
    @worker_num = num
    @tag = nil
    log "Set worker_num: #{num}"
  end

  dsl.out_of_band do
    do_kill('OOB') unless @force_restart
  end
end

#debug(msg) ⇒ Object



118
119
120
121
122
123
124
125
126
# File 'lib/worker_killer/puma_plugin_ng.rb', line 118

def debug(msg)
  return unless @debug

  if @logger
    @logger.warn("#{tag} (DEBUG) #{msg}")
  else
    warn("[#{Process.pid}] #{tag} (DEBUG) #{msg}")
  end
end

#do_kill(name) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/worker_killer/puma_plugin_ng.rb', line 91

def do_kill(name)
  return if kill_queue.empty?

  now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  return if now - @last_restarted_at < 10

  @last_restarted_at = now
  log "Killing workers by #{name}: #{kill_queue}"
  kill_queue.each do |worker_num|
    kill_queue.delete(worker_num)
    Thread.current.puma_server.options[:force_shutdown_after] = nil
    Thread.current.puma_server.begin_restart
  end
end

#force_restart!(force = true) ⇒ Object

Завершать процесс сразу после окончания inhibited метода. Иначе завершение будет происзодть в Out Of Band методе



78
79
80
# File 'lib/worker_killer/puma_plugin_ng.rb', line 78

def force_restart!(force = true)
  @force_restart = force
end

#log(msg) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/worker_killer/puma_plugin_ng.rb', line 106

def log(msg)
  if @logger
    @logger.warn("#{tag} #{msg}")
  else
    warn("[#{Process.pid}] #{tag} #{msg}")
  end
end

#request_restart_server(worker_num) ⇒ Object

Этот метод зовётся из Middleware внтури воркера



83
84
85
86
87
88
89
# File 'lib/worker_killer/puma_plugin_ng.rb', line 83

def request_restart_server(worker_num)
  return if @worker_num != worker_num

  log("Enqueue worker #{worker_num} for restarting...")
  kill_queue << worker_num
  do_kill('FORCE') if @force_restart
end

#set_logger!(logger) ⇒ Object



73
74
75
# File 'lib/worker_killer/puma_plugin_ng.rb', line 73

def set_logger!(logger)
  @logger = logger
end

#start(launcher) ⇒ Object

Этот метод зовётся при ИНИЦИАЛИЗАЦИИ плагина внути master-процесса, контролирующего кластер Puma псле форка данные сохранённые тут также доступны (например logger)



69
70
71
# File 'lib/worker_killer/puma_plugin_ng.rb', line 69

def start(launcher)
  set_logger!(PumaLogWrapper.new(launcher.log_writer))
end

#tagObject



114
115
116
# File 'lib/worker_killer/puma_plugin_ng.rb', line 114

def tag
  @tag ||= "[#{self.class}] #{@worker_num.nil? ? '[M]' : "[W#{@worker_num}]"}"
end