rails 流控制器中的意外过滤器行为
unexpected filter behavior in rails streaming controlle
如 Ruby on Rails 3: Streaming data through Rails to client 所示进行流式传输时,我 运行 遇到了 after_filter 和 around_filter 在响应之前完成执行的问题。我需要在执行 after_filter 之前流式传输响应,因为我们依赖于在这些过滤器中清理的数据。我正在使用乘客。
示例控制器:
class Asdf
def each
(1..5).each do |num|
sleep(1)
Rails.logger.info "STREAMING LINE #{num}"
yield "test#{num}\n"
end
end
end
class WelcomeController < ApplicationController
before_filter :before
after_filter :after
around_filter do |controller, action|
logger.debug "around_filter prior to action"
action.call
logger.debug "around_filter after action"
end
def index
self.response.headers['Last-Modified'] = Time.now.to_s
self.response_body = Asdf.new
end
def before
Rails.logger.info "before_filter called"
end
def after
Rails.logger.info "after_filter called"
end
end
示例日志输出(时间反映它正在流式传输)
Started GET "/welcome/index" for 192.168.74.64 at 2016-01-25 17:28:17 -0600
Processing by WelcomeController#index as HTML
before_filter called
around_filter prior to action
around_filter after action
after_filter called
Completed 200 OK in 0.7ms (ActiveRecord: 0.0ms)
STREAMING LINE 1
STREAMING LINE 2
STREAMING LINE 3
STREAMING LINE 4
STREAMING LINE 5
您似乎可以在 class 的 each
方法中调用您的数据 clean-up 例程,而不是依赖于 around_filter
迭代器已用完:
class Asdf
def each
(1..5).each do |num|
sleep(1)
Rails.logger.info "STREAMING LINE #{num}"
yield "test#{num}\n"
end
Rails.logger.info "ALL DONE; READY TO CLEAN UP"
clean_up
end
def clean_up
Rails.logger.info "CLEANING UP"
end
end
在 Rails 3.2 应用程序中点击 welcome#index
操作在日志中产生以下内容:
Started GET "/welcome" for 127.0.0.1 at 2016-01-25 18:55:49 -0800
Processing by WelcomeController#index as HTML
before_filter called
around_filter prior to action
around_filter after action
after_filter called
Completed 200 OK in 0.5ms (ActiveRecord: 0.0ms)
STREAMING LINE 1
STREAMING LINE 2
STREAMING LINE 3
STREAMING LINE 4
STREAMING LINE 5
ALL DONE; READY TO CLEAN UP
CLEANING UP
如 Ruby on Rails 3: Streaming data through Rails to client 所示进行流式传输时,我 运行 遇到了 after_filter 和 around_filter 在响应之前完成执行的问题。我需要在执行 after_filter 之前流式传输响应,因为我们依赖于在这些过滤器中清理的数据。我正在使用乘客。
示例控制器:
class Asdf
def each
(1..5).each do |num|
sleep(1)
Rails.logger.info "STREAMING LINE #{num}"
yield "test#{num}\n"
end
end
end
class WelcomeController < ApplicationController
before_filter :before
after_filter :after
around_filter do |controller, action|
logger.debug "around_filter prior to action"
action.call
logger.debug "around_filter after action"
end
def index
self.response.headers['Last-Modified'] = Time.now.to_s
self.response_body = Asdf.new
end
def before
Rails.logger.info "before_filter called"
end
def after
Rails.logger.info "after_filter called"
end
end
示例日志输出(时间反映它正在流式传输)
Started GET "/welcome/index" for 192.168.74.64 at 2016-01-25 17:28:17 -0600
Processing by WelcomeController#index as HTML
before_filter called
around_filter prior to action
around_filter after action
after_filter called
Completed 200 OK in 0.7ms (ActiveRecord: 0.0ms)
STREAMING LINE 1
STREAMING LINE 2
STREAMING LINE 3
STREAMING LINE 4
STREAMING LINE 5
您似乎可以在 class 的 each
方法中调用您的数据 clean-up 例程,而不是依赖于 around_filter
迭代器已用完:
class Asdf
def each
(1..5).each do |num|
sleep(1)
Rails.logger.info "STREAMING LINE #{num}"
yield "test#{num}\n"
end
Rails.logger.info "ALL DONE; READY TO CLEAN UP"
clean_up
end
def clean_up
Rails.logger.info "CLEANING UP"
end
end
在 Rails 3.2 应用程序中点击 welcome#index
操作在日志中产生以下内容:
Started GET "/welcome" for 127.0.0.1 at 2016-01-25 18:55:49 -0800
Processing by WelcomeController#index as HTML
before_filter called
around_filter prior to action
around_filter after action
after_filter called
Completed 200 OK in 0.5ms (ActiveRecord: 0.0ms)
STREAMING LINE 1
STREAMING LINE 2
STREAMING LINE 3
STREAMING LINE 4
STREAMING LINE 5
ALL DONE; READY TO CLEAN UP
CLEANING UP