forked from diaspora/diaspora
/
unicorn_killer.rb
53 lines (45 loc) · 1.08 KB
/
unicorn_killer.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# # your config.ru
# require 'unicorn_killer'
# use UnicornKiller::MaxRequests, 1000
# use UnicornKiller::Oom, 400 * 1024
module UnicornKiller
module Kill
def quit
sec = (Time.now - @process_start).to_i
warn "#{self.class} send SIGQUIT (pid: #{Process.pid})\talive: #{sec} sec"
Process.kill :QUIT, Process.pid
end
end
class Oom
include Kill
def initialize(app, memory_size= 512 * 1024, check_cycle = 10)
@app = app
@memory_size = memory_size
@check_cycle = check_cycle
@check_count = 0
end
def rss
`ps -o rss= -p #{Process.pid}`.to_i
end
def call(env)
@process_start ||= Time.now
if (@check_count += 1) % @check_cycle == 0
@check_count = 0
quit if rss > @memory_size
end
@app.call env
end
end
class MaxRequests
include Kill
def initialize(app, max_requests = 1000)
@app = app
@max_requests = max_requests
end
def call(env)
@process_start ||= Time.now
quit if (@max_requests -= 1) == 0
@app.call env
end
end
end