Class: Rack::Harakiri

Inherits:
Object show all
Defined in:
lib/picky/rack/harakiri.rb

Overview

Simple Rack Middleware to kill Unicorns after X requests.

Use as follows in e.g. your rackup File:

Rack::Harakiri.after = 100
use Rack::Harakiri

Then the Unicorn will commit suicide after 100 requests (50 is the default).

The Master Unicorn process forks a new child Unicorn to replace the old one.

Class Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(app) ⇒ Harakiri

Returns a new instance of Harakiri.



22
23
24
25
26
27
# File 'lib/picky/rack/harakiri.rb', line 22

def initialize app
  @app = app

  @requests            = 0
  @quit_after_requests = self.class.after || 50
end

Class Attribute Details

.afterObject

Returns the value of attribute after.



19
20
21
# File 'lib/picky/rack/harakiri.rb', line 19

def after
  @after
end

Instance Method Details

#call(env) ⇒ Object

#call interface method.

Harakiri is a middleware, so it forwards the the app or the next middleware after checking if it is time to honorably retire.



34
35
36
37
# File 'lib/picky/rack/harakiri.rb', line 34

def call env
  harakiri
  @app.call env
end

#harakiriObject

Checks to see if it is time to honorably retire.

If yes, kills itself (Unicorn will answer the request, honorably).

Note: Sends its process a QUIT signal if it is time.



45
46
47
48
# File 'lib/picky/rack/harakiri.rb', line 45

def harakiri
  @requests = @requests + 1
  Process.kill(:QUIT, Process.pid) if harakiri?
end

#harakiri?Boolean

Is it time to honorably retire?

Returns:

  • (Boolean)


52
53
54
# File 'lib/picky/rack/harakiri.rb', line 52

def harakiri?
  @requests >= @quit_after_requests
end