Parent

Rack::Signals

Installs signal handlers that are safely processed after a request

NOTE: This middleware should not be used in a threaded environment

use Rack::Signals.new do

trap 'INT', lambda {
  puts "Exiting now"
  exit
}

trap_when_ready 'USR1', lambda {
  puts "Exiting when ready"
  exit
}

end

Public Class Methods

new(app, &block) click to toggle source
# File lib/rack/contrib/signals.rb, line 29
def initialize(app, &block)
  @app = app
  @processing = false
  @when_ready = nil
  instance_eval(&block)
end

Public Instance Methods

call(env) click to toggle source
# File lib/rack/contrib/signals.rb, line 36
def call(env)
  begin
    @processing, @when_ready = true, nil
    status, headers, body = @app.call(env)

    if handler = @when_ready
      body = BodyWithCallback.new(body, handler)
      @when_ready = nil
    end
  ensure
    @processing = false
  end

  [status, headers, body]
end
trap_when_ready(signal, handler) click to toggle source
# File lib/rack/contrib/signals.rb, line 52
def trap_when_ready(signal, handler)
  when_ready_handler = lambda { |signal|
    if @processing
      @when_ready = lambda { handler.call(signal) }
    else
      handler.call(signal)
    end
  }
  trap(signal, when_ready_handler)
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.