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

╄→尐↘猪︶ㄣ 提交于 2019-12-07 00:12:12

问题


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:


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

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



来源:https://stackoverflow.com/questions/513491/whats-the-fastest-way-for-a-true-sinatraruby-rack-after-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!