Found this gem Rack After Reply that says:
A hook for Rack apps which fires after the response has been sent, and the socket to the client has been closed.
This is the ideal time to perform delayable, non-backgroundable tasks, such as garbage collection, stats gathering, flushing logs, etc. without affecting response times at all.
Usage
Simply add your callbacks to env['rack_after_reply.callbacks'].
use Rack::ContentLength
use Rack::ContentType, 'text/plain'
run lambda { |env|
env['rack_after_reply.callbacks'] << lambda { ... }
[200, {}, ['hi']]
}
But I couldn’t fit it into the Rails3 app.
Can anyone say on how to use this with a Rails3 app?
You need to add the callbacks to the Rack env during the actual request, i.e. in your controller.
You can get at the Rack env with the
requestobject.In your controller:
The callback will be run after any request that gets routed to this method.