Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the fastest way for a true sinatra(ruby/rack) after_filter?

Okay it's a simple task. After I render html to the client I want to execute a db call with information from the request.

I am using sinatra because it's a lightweight microframework, but really i up for anything in ruby, if it's faster/easier(Rack?). I just want to get the url and redirect the client somewhere else based on the url.

So how does one go about with rack/sinatra a real after_filter. And by after_filter I mean after response is sent to the client. Or is that just not dooable without threads?

I forked sinatra and added after filters, but there is no way to flush the response, even send_data which is suppose to stream files(which is obviously for binary) waits for the after_filters.

I've seen this question: Multipart-response-in-ruby but the answer is for rails. And i am not sure if it really flushes the response to the client and then allows for processing afterwards.

Rack::Callbacks has some before and after callbacks but even those look like they would run before the response is ever sent to the client here's Rack::Callbacks implementation(added comment):

def call(env)
  @before.each {|c| c.call(env) }
  response = @app.call(env)
  @after.each {|c| c.call(env) }
  response 
  #i am guessing when this method returns then the response is sent to the client.
end

So i know I could call a background task through the shell with rake. But it would be nice not to have too... Also there is NeverBlock but is that good for executing a separate process without delaying the response or would it still make the app wait as whole(i think it would)?

I know this is a lot, but in short it's simple after_filter that really runs after the response is sent in ruby/sinatra/rack.

Thanks for reading or answering my question! :-)

like image 470
jaredmdobson Avatar asked Feb 04 '09 21:02

jaredmdobson


1 Answers

Modified run_later port to rails to do the trick the file is available here:

http://github.com/pmamediagroup/sinatra_run_later/tree/master

like image 73
jaredmdobson Avatar answered Sep 28 '22 19:09

jaredmdobson