What's the fastest way for a true sinatra(ruby

2019-04-29 10:43发布

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! :-)

1条回答
SAY GOODBYE
2楼-- · 2019-04-29 11:27

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

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

查看更多
登录 后发表回答