Created
July 29, 2010 07:46
-
-
Save floere/497539 to your computer and use it in GitHub Desktop.
Unicorn Harakiri Middleware
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
# Simple Rack Middleware to kill Unicorns after X requests. | |
# | |
# Use as follows in e.g. your rackup File: | |
# | |
# Rack::Harakiri.after = 50 | |
# use Rack::Harakiri | |
# | |
module Rack | |
class Harakiri | |
# Set the amount of requests before the Unicorn commits Harakiri. | |
# | |
cattr_accessor :after | |
def initialize app | |
@app = app | |
@requests = 0 | |
@quit_after_requests = @@after || 50 | |
end | |
def call env | |
harakiri | |
@app.call env | |
end | |
# Checks to see if it is time to honorably retire. | |
# | |
# If yes, kills itself (Unicorn will answer the request, honorably). | |
# | |
def harakiri | |
@requests = @requests + 1 | |
Process.kill(:QUIT, Process.pid) if @requests >= @quit_after_requests | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment