Skip to content

Instantly share code, notes, and snippets.

@robdimarco
Created August 5, 2017 12:48
Show Gist options
  • Save robdimarco/e6b499b2492c8580feb072413ba6a8a1 to your computer and use it in GitHub Desktop.
Save robdimarco/e6b499b2492c8580feb072413ba6a8a1 to your computer and use it in GitHub Desktop.
Sidekiq Memory Killer
#!/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