Commit a047f017 authored by Ahmad Sherif's avatar Ahmad Sherif
Browse files

Implement a quick-n-dirty memory killer

parent dc8dabf2
Pipeline #82738 passed with stage
in 31 seconds
......@@ -8,6 +8,8 @@ db_common: &db_common
server:
listen_address: 0.0.0.0
listen_port: 9168
# Maximum amount of memory to use in megabytes, after which the process is killed
memory_threshold: 1024
# Probes config
probes:
......
require "sinatra/base"
require "English"
module GitLab
module Monitor
# Metrics web exporter
class WebExporter < Sinatra::Base
# A middleware to kill the process if we exceeded a certain threshold
class MemoryKillerMiddleware
def initialize(app, memory_threshold)
@app = app
@memory_threshold = memory_threshold.to_i * 1024
end
def call(env)
if memory_usage > @memory_threshold
puts "Memory usage of #{memory_usage} exceeded threshold of #{@memory_threshold}, signalling KILL"
Process.kill("KILL", $PID)
end
@app.call(env)
end
private
def memory_usage
io = IO.popen(%W(ps -o rss= -p #{$PID}))
mem = io.read
io.close
return 0 unless $CHILD_STATUS.to_i.zero?
mem.to_i
end
end
class << self
def setup(config)
setup_server(config[:server])
setup_probes(config[:probes])
use MemoryKillerMiddleware, config[:server][:memory_threshold] || 1024
end
def setup_server(config)
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment