gpt4 book ai didi

ruby-on-rails - rails activesupport 通知 - 错误的数据库运行时值

转载 作者:行者123 更新时间:2023-12-04 02:26:33 24 4
gpt4 key购买 nike

我正在尝试记录我的 REST API 应用程序的请求。我为此使用 Rails 通知,例如这里 http://railscasts.com/episodes/249-notifications-in-rails-3

我不明白如何用 rails 通知解决一个问题。

我的初始化代码

ActiveSupport::Notifications.subscribe "process_action.action_controller" do |name, start, finish, id, payload|
p name
p start
p finish
p id
p payload
end



Controller respond section

class PostsController < ApplicationController
# GET /posts
# GET /posts.json

respond_to :json, :html
....
end

Controller 创建 Action
  def create
@post = Post.new(params[:post])
@post.save!
respond_with(@post, :location => nil)
end

控制台输出
"process_action.action_controller"
2013-02-02 20:13:11 +0200
2013-02-02 20:13:11 +0200
"951b8999e9b71d4a8949"
{:controller=>"PostsController", :action=>"create", :params=>{"utf8"=>"✓", "authenticity_token"=>"1WugY9gh6ZCRXjfBTuckye3c9XDvtCqMQ2JdBpCo88s=", "post"=>{"name"=>"post3", "title"=>"post3", "content"=>"post3"}, "commit"=>"Create Post", "action"=>"create", "controller"=>"posts"}, :format=>:html, :method=>"POST", :path=>"/posts", :status=>302, :view_runtime=>nil, :db_runtime=>0}

如您所见 :db_runtime=>0

但是,如果我将 Controller 操作代码更改为默认脚手架
  def create
@post = Post.new(params[:post])
#@post.save!
#respond_with(@post)
respond_to do |format|
if @post.save
format.html { redirect_to @post, notice: 'Post was successfully created.' }
format.json { render json: @post, status: :created, location: @post }
else
format.html { render action: "new" }
format.json { render json: @post.errors, status: :unprocessable_entity }
end
end
end

我可以看到
"process_action.action_controller"
2013-02-02 20:22:51 +0200
2013-02-02 20:22:51 +0200
"bf2a3173c08a0fd9008e"
{:controller=>"PostsController", :action=>"create", :params=>{"utf8"=>"✓", "authenticity_token"=>"1WugY9gh6ZCRXjfBTuckye3c9XDvtCqMQ2JdBpCo88s=", "post"=>{"name"=>"post3", "title"=>"post3", "content"=>"post3"}, "commit"=>"Create Post", "action"=>"create", "controller"=>"posts"}, :format=>:html, :method=>"POST", :path=>"/posts", :status=>302, :view_runtime=>nil, :db_runtime=>4.727}

:db_runtime=>4.727

这是什么原因以及如何修复它以使其在第一个示例中工作?
谢谢 !

UPD
 bundle show rails
/Users/admin/.rvm/gems/ruby-1.9.3-p125/gems/rails-3.2.11
rvm current
ruby-1.9.3-p125

UPD2

当我使用respond_with时,它似乎不起作用!有人能告诉我为什么吗?
谢谢

最佳答案

好吧,这似乎是一个错误。让我们看看,发生了什么:

首先,我们有用于 Controller 操作的 AR railtie 及其使用 cleanup_view_runtime 钩子(Hook)设置 db_runtime 的实现

def cleanup_view_runtime
if ActiveRecord::Base.connected?
db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
runtime = super
db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime
self.db_runtime = db_rt_before_render + db_rt_after_render
runtime - db_rt_after_render
else
super
end
end

应用程序调用 Controller 操作 -> 操作执行一些数据库查询并渲染一些东西 -> 渲染前后 AR Logger 保存运行时数据。好的。

让我们看看 respond_with 是如何工作的
def respond_with(*resources, &block)
raise "In order to use respond_with, first you need to declare the formats your " <<
"controller responds to in the class level" if self.class.mimes_for_respond_to.empty?

if collector = retrieve_collector_from_mimes(&block)
options = resources.size == 1 ? {} : resources.extract_options!
options[:default_response] = collector.response
(options.delete(:responder) || self.class.responder).call(self, resources, options)
end
end

def self.call(*args)
new(*args).respond
end

def to_format
if get? || !has_errors? || response_overridden?
default_render
else
display_errors
end
rescue ActionView::MissingTemplate => e
api_behavior(e)
end

这里的代码似乎太多了,但你应该看到这个问题的调用堆栈:respond_with -> self.class.responder.respond -> self.class.responder.to_format -> default_render -> default_renderer raise ActionView::MissingTemplate(因为我们没有)。此时我们可以通过捕获 ActionView::MissingTemplate 看到渲染 :json 和 :xml(api_behaviour) 的实现。

现在我们知道 respond_with 是如何工作的,但是 AR Logger 不知道..
cleanup_view_runtime 钩子(Hook)被调用了两次:对于 default_renderer(当时模板数据已准备好并调用了一些数据库查询,但我们在渲染过程中捕获了 ActionView::MissingTemplate)
db_rt_before_render = ActiveRecord::LogSubscriber.reset_runtime
runtime = super # <-- here
db_rt_after_render = ActiveRecord::LogSubscriber.reset_runtime

对于 api_behavour(当时所有模板数据都已准备好渲染,没有数据库查询)

一些困惑的解释,但我希望它会有所帮助:)

关于ruby-on-rails - rails activesupport 通知 - 错误的数据库运行时值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14665016/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com