Skip to content

Commit

Permalink
Add rolling_pre_term config
Browse files Browse the repository at this point in the history
  • Loading branch information
adelnabiullin committed Jun 11, 2020
1 parent 7dc7683 commit 9313c1d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
7 changes: 5 additions & 2 deletions lib/puma_worker_killer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,16 @@
module PumaWorkerKiller
extend self

attr_accessor :ram, :frequency, :percent_usage, :rolling_restart_frequency, :reaper_status_logs, :pre_term, :on_calculation
attr_accessor :ram, :frequency, :percent_usage, :rolling_restart_frequency,
:reaper_status_logs, :pre_term, :rolling_pre_term, :on_calculation

self.ram = 512 # mb
self.frequency = 10 # seconds
self.percent_usage = 0.99 # percent of RAM to use
self.rolling_restart_frequency = 6 * 3600 # 6 hours in seconds
self.reaper_status_logs = true
self.pre_term = nil
self.rolling_pre_term = nil
self.on_calculation = nil

def config
Expand All @@ -27,7 +30,7 @@ def start(frequency = self.frequency, reaper = self.reaper)

def enable_rolling_restart(frequency = self.rolling_restart_frequency)
frequency = frequency + rand(0..10.0) # so all workers don't restart at the exact same time across multiple machines
AutoReap.new(frequency, RollingRestart.new).start
AutoReap.new(frequency, RollingRestart.new(nil, self.rolling_pre_term)).start
end
end

Expand Down
4 changes: 3 additions & 1 deletion lib/puma_worker_killer/rolling_restart.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module PumaWorkerKiller
class RollingRestart
def initialize(master = nil)
def initialize(master = nil, rolling_pre_term = nil)
@cluster = PumaWorkerKiller::PumaMemory.new(master)
@rolling_pre_term = rolling_pre_term
end

# used for tes
Expand All @@ -16,6 +17,7 @@ def reap(wait_between_worker_kill = 60) # seconds

@cluster.workers.each do |worker, ram|
@cluster.master.log "PumaWorkerKiller: Rolling Restart. #{@cluster.workers.count} workers consuming total: #{ total_memory } mb. Sending TERM to pid #{worker.pid}."
@rolling_pre_term.call(worker) unless @rolling_pre_term.nil?
worker.term
sleep wait_between_worker_kill
end
Expand Down

0 comments on commit 9313c1d

Please sign in to comment.