【问题标题】:Strange Timeout::Error with render_to_string and HTTParty in Controller Action控制器动作中的render_to_string和HTTParty出现奇怪的超时::错误
【发布时间】:2013-05-26 22:13:38
【问题描述】:

在我的控制器操作中,我使用带有 jbuilder 模板的 render_to_string 来为 API 的 http post 操作渲染对象,此 API 调用的结果应该由控制器渲染。

但是我得到一个奇怪的超时,然后我在通过 render_to_string 渲染模型后调用 HTTParty。

日志:

[23:43:52.262] [18033] [info]   Rendered admin/companies/companies.json.jbuilder (42.8ms)
... nothing happens here (60s timeout)
[23:44:52.314] [18033] [info] Completed 500 Internal Server Error in 60338ms
[23:44:52.342] [18033] [fatal] 
Timeout::Error (Timeout::Error):
  app/controllers/admin/companies_controller.rb:135:in `copy_to_environment'
  lib/middleware/x_domain_request_polyfill.rb:46:in `_call'
  lib/middleware/x_domain_request_polyfill.rb:16:in `call'
[23:44:52.422] [18033] [info] Started POST "/api/v1/internal/company/create_company_copy.json" for 127.0.0.1 at 2013-05-26 23:44:52 +0200
[23:44:52.465] [18033] [info] Processing by Api::V1::Internal::CompaniesController#create_company_copy as JSON
[23:44:52.465] [18033] [info] User Agent: 
[23:44:52.466] [18033] [info] Completed 200 OK in 1ms (Views: 0.2ms | Models: 0.0ms)
[23:44:52.466] [18033] [info] Response is {"status":"success"}

控制器动作:

json_string = render_to_string(:template => '/admin/companies/companies', :formats => [:json],locals: { company: @company })
api_action = "/api/v1/internal/company/create_company_copy.json"
urlstring_to_post = 'http://localhost:3000'+ api_action
@result = HTTParty.post(urlstring_to_post.to_str, :body => JSON.parse(json_string))
render(:show, :formats => [:html])

对我来说奇怪的是,正如您在日志中看到的那样,实际的控制器操作由于超时而失败,因此错误页面被呈现给用户,但 API 的帖子已成功执行,但仅在超时。

任何建议表示赞赏!

【问题讨论】:

    标签: ruby-on-rails httparty


    【解决方案1】:

    常见问题,但如果离监控太近则难以跟踪。

    我猜你运行rails server 通常会启动一个单一的WEBrick 服务器进程。

    不幸的是,你会在本地测试时屏蔽自己。

    修复:启动另一个具有不同端口号的 rails 服务器。或者启动独角兽之类的东西,并将工人数量设置为 2 或更多。

    【讨论】:

    • 太好了,非常感谢,现在第二台服务器正在运行,我不会再阻止自己了;)
    猜你喜欢
    • 2019-03-10
    • 2013-07-28
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 2017-03-04
    • 1970-01-01
    • 2011-01-08
    相关资源
    最近更新 更多