Created
August 5, 2017 12:48
-
-
Save robdimarco/e6b499b2492c8580feb072413ba6a8a1 to your computer and use it in GitHub Desktop.
Sidekiq Memory Killer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
class SidekiqMemoryKiller | |
attr_accessor :restart_threshold | |
def initialize(restart_threshold: 90) | |
@restart_threshold = restart_threshold | |
end | |
def perform | |
log "!! No sidekiq process found!!" and return false \ | |
if sidekiq_pid == '' | |
log "Currently have memory usage of #{get_ram_pct}" | |
signal_restart \ | |
if get_ram_pct.to_f >= restart_threshold | |
end | |
def signal_restart | |
log "Sending restart signal for Sidekiq" | |
execute 'sudo service elocal_web_sidekiq restart' | |
end | |
def get_ram_pct | |
@get_ram_pct ||= execute("ps -o %mem= -p #{sidekiq_pid}") | |
end | |
def sidekiq_pid | |
@sidekiq_pid ||= execute("ps -u app -f | grep sidekiq | awk '{ print $2 }'") | |
end | |
def execute(cmd) | |
log "Running > #{cmd}" | |
%x(#{cmd}) | |
end | |
def log(msg) | |
%x(logger "SidekiqMemoryKiller: #{msg}") | |
puts msg | |
end | |
end | |
SidekiqMemoryKiller.new.perform \ | |
if __FILE__ == $PROGRAM_NAME |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment