Reloading dynamically generated pdf document hangs/stalls in Apache with Phusion passenger - ruby-on-rails

I have a situation where I'm dynamically generating a pdf document using Ruby on Rails [send_data call] (http://apidock.com/rails/v2.3.8/ActionController/Streaming/send_data).(Rails 2.3.18 ... its a legacy app),
The pdf is always generated fine the first time, but on reload, or making subsequent calls to the same endpoint (doesn't matter what the parameters are in the url) the request just stalls forever. Its the same from the browser, using curl from the commandline ... doesn't matter where or what browser is used.
If I kill the request (by x'ing out of it in the browser, for example, and retrying it) the call succeeds
In tracing the output using logs, I found out that the hung request never gets to the Rails server (Phusion passenger instance), because there are no Rails logs produced for the hung call. They also don't show up in Apache Access Logs since apparently the request never succeeds.
Sometimes in the log, I'll see
[ 2015-12-30 16:16:14.0423 14334/7fbb1a32d700 apache2/Hooks.cpp:718 ]: Unexpected error in mod_passenger: Cannot connect to the helper agent at /tmp/passenger.1.0.11825/generation-5/request
Backtrace:
in 'Passenger::FileDescriptor Hooks::connectToHelperAgent()' (Hooks.cpp:283)
in 'int Hooks::handleRequest(request_rec*)' (Hooks.cpp:532)
Any ideas?

Related

Rack error - RuntimeError: failed to get urandom in a Rails app (Rails 5.0.6, Ruby 2.5.5)

I'm making a lot of API requests to a Rails 5.0(.6) app running locally. It inconsistently fails with the Rack error:
RuntimeError: failed to get urandom
The backtrace is to the SecureRandom module, not in the application (specifically the uuid method). From there it is in ActionDispatch::RequestId. Unfortunately, the full backtrace is gone from my terminal history and does not show up in the logs (since it occurs outside Rails).
From the many requests that are working and the fact that it fails in different places, I'm guessing that it is due to some local system limitation.
After attempting a couple of different fixes, I eventually discovered that the issue was my open files limit. To check it, run ulimit -n in a terminal. Mine was set to 256.
I followed the instructions in this article:
https://wilsonmar.github.io/maximum-limits/
The specific instructions start at the "Sierra and newer versions" header and go to "Their permissions need to be “-rw-r–r–”, set by sudo chmod 644." under #5. After adding the files and changing ownership and permission, I restarted and the open file limit is now 524288.
I now have no further issues with rapid API calls.

How can i track the issue in production RAILS

have on pdf creating function, but that PDF Generation different response, in local development working fine but the server shows 500 error. how can i track the issue in production ?
You can track issues in production in various way. There are several answers available. But better I should share very common practice regarding a rails application. If you are using apache/nginx as your http server. You can tail there log and make the operation on browser on the same time so you can get realtime log from the http server.
Next, based on your configuration (as per convention over configuration) there should be production.log in your app log directory. You can run the following command in your terminal/command line tools and check the log on real time:
tail -f log/production.log
I assumed in your case production.log is the log file currently logging everything by your production application server (passenger/unicorn/puma/etc..)
For assets and client end issues, try using your browser's console to inspect the issues.
I hope my answer will give you some idea!

Can not open localhost by running Ruby in Rails application

I am trying to run a ruby script in rails application using system call, like
def runSystemCall
system("ruby /path/to/ruby/script/watir.rb localhost:3000/articles/14")
end
and watir.rb:
def watir(url)
bb = Watir::Browser.new :chrome
bb.goto "#{url[0]}"
end
watir(ARGV)
when running this in rails application, a browser opened and stay in a status of Waiting for localhost..., until error
.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/net/protocol.rb:158:in `rescue in rbuf_fill': Net::ReadTimeout (Net::ReadTimeout)
loaded the website localhost:3000/articles/14 opened.
Dose anyone know why?
and, when run this in a terminal
$ ruby /path/to/ruby/script/watir.rb localhost:3000/articles/14
a browser opened and open website localhost:3000/articles/14
That's what I expected.
Your local server is single threaded. That means it can only handle one request at a time. If one local request wants to load an other page from the same local server, than you need at least two local threads.

Passenger fails to send whole request to RoR

Recently, we are facing a strange problem where some XHR requests, sent from Passenger to our application server, fail to execute properly.
Please have look at following chain of events:
LB Passenger sends a request to the application server.
Rails returns a NoMethodError exception when trying to figure out which controller action this request is supposed to be processed by.
Rails error log:
Started POST "/user/" for 194.29.65.21 at 2013-03-25 13:14:38 +0100
NoMethodError (undefined method `[]' for nil:NilClass):
Here is a log when Rails is able to figure out which controller/action a request should be routed to:
Started POST "/User/" for 194.29.65.21 at 2013-03-25 13:14:38 +0100
Processing by Web::UserController#index as
Parameters: {"c"=>"1", "fref"=>""}
This error gets propagated to the application server passenger which throws the following error:
[ pid=20071 thr=3074915216 file=ext/nginx/HelperAgent.cpp:577
time=2013-03-24 22:25:58.257 ]: Uncaught exception in PassengerServer
client thread: exception: Cannot read response from backend
process: Connection reset by peer (104) backtrace:
in 'void Client::forwardResponse(Passenger::SessionPtr&, Passenger::FileDescriptor&)' (HelperAgent.cpp:361)
in 'void Client::handleRequest(Passenger::FileDescriptor&)' (HelperAgent.cpp:503)
in 'void Client::threadMain()' (HelperAgent.cpp:596)
And finally LB Nginx gives this error: up stream status = 500 error.
This error comes randomly for a given route. Even the frequency of occurrence of this error changes randomly.
System details:
Ruby: 1.9.2p180
Rails : 3.0.5
Phusion Passenger version 3.0.5
nginx version: n
We think this happens because Passenger is failing to send the whole request data to Rails which results in Rails failing to decide the valid controller action.
Review passenger config for nginx, I have experienced a lot issues with passenger for nginx.
http://www.modrails.com/documentation/Users%20guide%20Nginx.html#PassengerSpawnMethod specially verify this configuration and and add this in nginx.conf in parent node.
We just debugged and found that issue is not with XHR but with POST requests.
Also right now we are using passenger 3.0.5, does any know if upgrading to passenger 3.0.19 help?

Heroku - how to flush Rails logs after each request

I'm running a Rails 3 app on Heroku.
This is the output of heroku logs --tail --source app:
https://gist.github.com/4000998
The Started GET "/" lines appear in real time as HTTP requests are made. However, the Processing by Controller lines get buffered and then dumped to the log all at once.
This makes it very hard to debug issues in the app as it is difficult to match up the controller logs with the rest of the app's logs. How do I get the buffered logs to flush after each request?
Add the following line to your config.ru:
$stdout.sync = true

Resources