How to automatically launch browser when rails server starts? - ruby-on-rails

Is there a way of automatically launching your browser and pointing it to http://localhost:3000 when you run rails server?
And secondly ensuring this only happens in development?
I would have thought that Launchy would have done the trick, but I'm struggling to work out where to put it.
I've tried adding an initialiser config/initializers/launchy.rb
require 'launchy'
Launchy.open("http://localhost:3000")
And this triggers the browser to open, create a new tab and visit http://localhost:3000 as expected, however it runs before the server / application has finished booting and I get a Cannot connect to Server error in the browser.
If I reload in the browser my app works just fine so I'm confident it's not a problem with my app, rather the timing of the Launch.open call.
I tried renaming launchy.rb to z_launchy.rb so it gets loaded last as per the docs but still the same problem. Launchy fires before the Application is ready for it.
I've also tried adding the code to config/puma.rb (I'm using puma as my server), to config/enviroments/developoment.rb but always the same problem. The Launchy.open command gets called to soon.
Where should I call Launchy.open("http://localhost:3000") to ensure that a) It runs after the application has loaded and is ready to receive requests; and b) So it only runs in development, not in test or production?
System set up OS X 10.11.3, Rails 4.2.5, ruby 2.2.1p85, puma version 2.15.3

Putting it into config.ru would work, after the run Rails.application line. By the time that returns, your app is ready to go. To run it only in development, check the Rails.env.
require ::File.expand_path('../config/environment', __FILE__)
run Rails.application
Launchy.open("http://localhost:3000") if Rails.env.development?
This is easy, but it feels dirty to me to have this sitting in your app. Better would be a small script that starts your server, waits a moment, then launches the browser.

Related

Changes do no show up on production running Passenger

Have got new project with production server already running on passenger.
After doing normal deploy as described in 1 page docs left by old team (only cap prod deploy) we are having constant problems. First of all - code we pushed doesn't seem to be working. It should be adding new data on rake tasks to items in database. Physically code is there - in current folder. But it doesnt seems to be triggered.
I ve noticed that there were no rails or gem installed when I ve tried to do simple rails c via ssh. After installing everything and manually launching code with binding.pry added looks like code was triggered. But via normal scheduled rake task it was not.
It looks like passenger is running in daemon since there are no pid in tmp folder (usual for rails app).
1) Is there a chance restarting server will actually help - it was not restarted after deploy and I have no idea how to restart it without pid.
2)passenger-config restart-app returns actually 2 servers. Can they collide and prevent normal app work?(Update: servers are not same - single letter difference in the name)
Still passenger-config restart-app dont seem to restart server
Sorry for the wall of text by I ve spent 2 nights getting it to work and I still lost.

Having to restart thin server (with SSL) each time to trigger Ruby Debugger normal?

I installed the debugger gem in a project i'm working on. They are using thin server with ssl. When I put a debugger in the code, it doesn't trigger until I restart the server. And then if I remove a debugger, it doesn't go away until I restart the server. Any ideas why this is happening?
It depends on where you place the debugger. Rails is configured to reload all the content of /app on every request. If you place the debugger call there, then it will be reloaded on every request.
Otherwise, if you place it somewhere else, for example in lib, then it will not.
Also, you may want to use the debugger in combination of automated testing, rather than browser testing. It will make the test more effective.
In order to use debugger with thin, you need to start rails server with --debugger prompt
rails s --debugger
whenever server encounters debugger statement, rails server connect to debug console. No other request will be served in that context. when you finish up with debugging by continue statement or no break point remain, then server start behaving normally until it encounter debugger statement again.
using this command fixed it:
thin --ssl --debug start

Why WEBrick server is faster in production mode rather in development mode? + Rails

I have been developing ruby on rails application since some couple of months. I use the default WEBrick server to run the applications. And I found that when I start the WEBrick server in the development and production modes, the server works more speed for production mode than for the development mode.
Is there any specific reason behind that? Can anybody explain me?
In production mode, a server loads code into the cache, which makes things quick. However, that's not the case in development mode (since you don't want to restart your webrick every time you made a change). Every request loads the according code again, which takes a bit of time.
And the most of all time-eaters is the asset pipeline. In production, you get a compiled version of your assets (javascripts and css) in maybe one or two requests. In development, you get them split, for debugging purpose (based on your environment settings, of course). And because a browser does not handle all requests simultaneously, some assets are loaded after other ones are finished loading. You can watch this behaviour with e.g. firebug's network console. That means: the more assets you have, the longer your page takes to load in development mode.
In dev mode classes are not cached, so Rails reloads all the classes each time you refresh. Also, asset compilation is not done in development (by default), so Rails reloads all the assets (CSS, Javascript etc) each time you refresh.
The difference is between 2 environments. In Rails, there are several environment. Each has his own database configuration and Rails options.
You can use the Rails.env variable to made some different change with particular environment.
By default, the development environment is without all cache and activate the auto-reloading. The production environment is with all cache.
But if you want you can make a production environment like development or development environment like production.
You can add some new specific environment too.
Creating new Environment:
Assuming you want create the hudson environment.
Create a new environment file in config/environments/hudson.rb.
You can start by cloning an existing one, for instance config/environments/test.rb.
Add a new configuration block in config/database.yml for your environment.
That's all.
Now you can start the server
ruby script/server -e hudson
Run the console
ruby script/server hudson
And so on.

Rails 3.2 PrivatePub in production faye.js not found

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.

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

Resources