Rails 3.2 PrivatePub in production faye.js not found - ruby-on-rails

I'm having an issue with a gem called private_pub that uses a faye gem and thin server.
This all works fine in development, but on the server I can get everything started up fine but on the page where I'm using private_pub I get an error in the js console (chrome) that says
GET http://myapp.example.com/faye.js 406 (Not Acceptable)
and when I view http://myapp.example.com/faye.js in the browser (url changed) I get an empty screen where in development it displays all the js code. Also I can see in chrome's developer tools I can see in development the type is "Pending" and in production I'm seeing it passed as "text/html"
I've googled and googled and have come up with exactly nothing. Can anyone point me in the right direction.
Is there some special mime-type that is being passed here that I need to configure apache or rails to accept?
Thank you in advance

HAZZAH!
I figured it out.
I jumped through all kinds of hoops and am not 100% sure that the solution I found isn't working because of some of the other things I tried but...
First thing I tried was following a tutorial for installing Thin with a Rails app on Centos, (from Slicehost's docs) Slicehost Articles: CentOS - thin web server for Ruby and did a whole bunch of thin configurations. But I don't believe this was necessary because private_pub/faye is supposed to handle this all for you. (from what I understand)
One important thing is that I know you need to use the startup that private_pub describes, even though you can start thin directly.
RAILS_ENV=production bundle exec rackup private_pub.ru -s thin -D -E production
The '-D' makes sure that it runs as a background process.
In my private_pub.yml:
production:
server: "http://myapp.example.com:9292/faye"
secret_token: "{SECRET_TOKEN HERE}"
signature_expiration: 3600 # one hour
I added in the port# here and it all works now.

Related

Debugging rails on passenger

I am running a Rails 5 API on passenger locally.
I start the server with passenger start command.
I installed both byebug and pry-rails but none of them are working for me.
I put a debug point, either with byebug or binding.pry, and when the code hits that point it does print the debug point in the server logs, with the code that it is executing but it doesn't actually stop, and I can type but I don't see the results of what I type.
I am starting to believe, based on posts like this one:
https://coderwall.com/p/rtskuw/create-break-points-in-rails-with-passenger-phusion-pry-pry-remote
that I can't actually debug like I debugged on rails s with passenger, but I don't actually want to believe that. There must be an easy way for me to debug the rails code on my passenger terminal without having to open a different terminal to act as a remote client or something like that.
Thank you very much

net/http rails issue works only in console

in ruby on rails console 'net/http' works, but in controller it doesn't and gives timeout error.
require 'net/http'
uri = URI('http://localhost:3000/api_json.json')
json = Net::HTTP.get(uri)
parsed_json = ActiveSupport::JSON.decode(json)
Most likely you're using default Webrick server, that serves one request a time. So, from console it works fine, but fails when you try to call it from controller (when the Webrick worker is already busy).
You can try to setup and run another server like unicorn or thin, or run two Webrick instances on different ports:
rails server
rails server -p 3001
and go to localhost:3001
#dimuch's solution might have solved your issue, but it might help someone facing similar situation. I will explain the issue, and the solution in detail (extension of #dimuch's solution).
Issue:
You might have a controller like some:"/test_controller/test_method", and you might want to call a method in a controller, like /api/v1/some_test_api, and facing error like Completed 500 Internal Server Error in 60004.4ms
[27580c5c46770812c550188346c2dd3e] [127.0.0.1] [/xauth_test/sanity_oauth_login]
Timeout::Error (Timeout::Error):
Solution:
As said by #dimuch, "
Most likely you're using default Webrick server, that serves one request a time.....". 1. You need to run the application on different ports, like
rails s -p 3000, and rails s -p 3001, then make the request from 3001.
If you face an issue like "A server is already running. Check /tmp/pids/server.pid. Exiting", then try running rails s -p 3001 -P PROCESS_ID.
2. Use other server's like Unicorn, or Puma.
Note: If you want it for just testing purpose in local, then I would suggest to go with the first solution, which is easy and simple. I am sorry for poor English, and I found most of solutions from other stack overflow pages, and websites, which I am attaching (links for refs) below, and sorry if I missed some one or some thing to refer. Hope this helps someone.
Refs:
For running multiple instances:
Running multiple instances of Rails Server
Similar errors and way they are handled:
Rails HTTParty Getting Timeout::Error
Faraday timeout error with omniauth (custom strategy)/doorkeeper
Strange Timeout::Error with render_to_string and HTTParty in Controller Action
Configuring Unicorn &Puma:
http://vladigleba.com/blog/2014/03/21/deploying-rails-apps-part-3-configuring-unicorn/
https://github.com/puma/puma

No log messages in production.log

I wrote a demo HelloWorld Rails app and tested it with WEBrick (it doesn't even use a DB, it's just a controller which prints "hello world"). Then I tried to deploy it to a local Apache powered with Passenger. In fact this test is just to get Passenger working (it's my first deploy on Apache). Now I'm not even sure that Passenger works, but I don't get any error on the Apache side.
When I fire http://rails.test/ the browser shows the Rails 500 error page - so I assume that Passenger works. I want to investigate the logs, but it happens that production.log is empty! I don't think it's a permission problem, because if I delete the file, it is recreated when I reload the page. I tried to change the log level in conf/environments/production.rb, tried to manually write to log file with Rails console production and
Rails.logger.error('asdf')
it returns true but nothing gets written to production.log. The path (obtained per Rails.logger.inspect) is correct, and I remark that the file is recreated if I manually remove it. How can I know what's going on?
(I already checked the Apache logs, plus I set the highest debug level for Passenger but it seems a Rails problem, so is not logged by the server)
Assuming you're running Rails 3.2.1, this is a bug. It was patched in 3.2.2.
If you can't upgrade to 3.2.2 for any reason, this comment on GitHub has a workaround:
# config/initializers/patch_rails_production_logging.rb
Rails.logger.instance_variable_get(:#logger).instance_variable_get(:#log_dest).sync = true if Rails.logger
Setting this works on Rails 3.2.11:
Rails.logger = ActiveSupport::BufferedLogger.new(Rails.root.join("log","production.log"))

thin slow in development when not using localhost

I switched to using thin in local development instead of webrick.
When I access localhost:3000 it returns the page almost instantly just like webrick
But when I access myapp.local:3000 the browser spins for 20 seconds or so on each request before rendering the page. I'm not sure what it's doing during that time - the rails log shows the page being generated almost instantly - it almost seems like the browser is doing name resolution during that time or something else.
In my /etc/hosts i have
127.0.0.1 myapp.local
In webrick there was no difference between accessing myapp.local:3000 and localhost:3000.
But in thin there is the large difference mentioned above. Any theories? Much appreciated!
Look for the file /usr/lib/ruby/VERSION_OF_RUBY/webrick/config.rb and edit it.
Replace/insert the following line as a new key of the General hash.
:DoNotReverseLookup => true
Restart webrick.
Otherwise try running sudo service avahi-daemon stop
See Webrick is very slow to respond. How to speed it up? for more details

Why would running a Rails app as a WEBrick server work, but installing it as a Mongrel service would not?

Yet another newbie RoR question from me.
I started banging my head against a wall last night when I simply could not get my Rails app to display in my browser after installing it as a Mongrel service.
I installed it using a command like this (from the app's root directory):
mongrel_rails service::install -N MyAppName -e development -p 3000
This set up the Windows service and everything seemed to be just fine. I could start/stop the service and saw no errors in the logs. Then navigating to localhost:3000 in my browser, I was greeted with a variety of errors, none Rails-specific (all along the lines of "Could not connect to server" or the like). Consulting the log at this point revealed no obvious problems.
I could not for the life of me figure out how to get this to work. So, out of exasperation, I tried simply running the app on WEBrick instead:
ruby script/server webrick -p 3000
When I did this, my app ran perfectly! Opening my browser to localhost:3000 now displayed my front page as expected.
I should note that I have used Mongrel successfully for other apps on my local machine.
So what app-specific characteristics could be responsible for WEBrick working where Mongrel doesn't?
Just some ideas to try:
Add -c param with full path to application:
-c "C:\xxx\yyy\zzz"
Check if system-wide PATH environment variable contains ruby bin directory - maybe just user's PATH is set.
Switch service to run as your user.

Resources