Strange Timeout::Error with render_to_string and HTTParty in Controller Action

给你一囗甜甜゛ 提交于 2019-12-10 17:49:39

问题


Within my controller action i am using render_to_string with a jbuilder template to render an object for a http post action to an API, the results of this API-Call is what should be rendered by the controller.

But i get a strange timeout then i call HTTParty after i rendered the model via render_to_string.

Log:

[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"}

Controller-Action:

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])

The strange thing for me is as you can see in the log that the actual controller action fails because of the timeout and thus the errorpage is rendered to the user, BUT the post to the API is successfully executed but only right after the timeout.

Any suggestions appreciated!


回答1:


Common problem, but hard to track if too close to monitor.

I guess you run rails server which normally starts one single WEBrick server process.

Unfortunately you will block yourself while local testing.

Fix: Start another rails server with different port number. Or start something like unicorn and set worker number to 2 or more.



来源:https://stackoverflow.com/questions/16764175/strange-timeouterror-with-render-to-string-and-httparty-in-controller-action

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