rails: What happens when two web servers are specified in a Gemfile - ruby-on-rails

If two web servers (say puma and unicorn) are specified
in the Gemfile, which one gets precedence?
I tried doing this myself but realized I was on windows with not much ruby support.

Long Answer with example:
you created app with rails 5.0.0, you get puma webserver by default.
you install unicorn gem. and you start the server by rails server still your server is puma. why ? because config/puma.rb file which generated with rails new application-name .
To start unicorn server you have to created one file for unicorn in config folder.
To start unicorn server you have to execute command like this unicorn -c config/unicorn.rb in your terminal.
I think this is the answer of your question.
Short answer:
You have tell rails explicitly which webserver you have to run. Rails will not decide which to run (in case of multiple web server).
I hope this clear your doubts.
Cheers

Related

Understanding rails s conflict with Puma and Capybara. How do I properly install puma in the Gemfile?

I am inheriting a code base where the tests have been abandoned for a little over a year. I am trying to clean up the test suite as much as possible but I am having a difficult time teasing out my integration tests with 1. Capybara 2. Puma, 3. Selenium, 4. Starting the Rails Server with rails s.
Here is my initial setup and problem with the Rails 4.2 app. So without doing anything from the get go, when I execute rails testing, I get the following error:
Capybara is unable to load puma for its server, please add puma to your project or specify a different server via something like Capybara.server = :webrick. (LoadError).
I want to point out that in a separate file named start-dev, I have the following in its contents:
rails s -b 0.0.0.0
When I execute this comand with ./start-dev, I am able to view my development app with this url defined in my /etc/hosts 127.0.0.1 secure.ssl.local
Now here is where I start to run into trouble, Through reading some github forums regarding capybara and puma, I start by adding puma into my Gemfile and bundle install but now I am unable to see my development app through the browser at secure.ssl.local.
This is the erorr I get:
/usr/lib/ruby/2.6.0/uri/rfc3986_parser.rb:67:in `split': bad URI(is not URI?): "tcp://0.0.0.0\r:3000" (URI::InvalidURIError)
I noticed that in the start up though, when I execute ./start-dev which if you remeber has rails s -b 0.0.0.0, I see this:
Booting Puma rails 4.2.11.1 application starting in development on https://0.0.0.0:3000
So I am confused by this error. Is Puma blocking my port of 3000 meaning that I have to change the port of Puma? And what makes this even more confusing is that instead of running ./start-dev in the terminal and I simply run rails s -b 0.0.0.0 it magically works except it only works if I navigate to localhost:3000 and not secure.ssl.localhost. This is important because on secure.ssl.localhost I have and need a certificate and localhost I don't.
And finally to add one more layer of confusion, when I run the tests with the puma gem installed and run rake test I get this with Puma:
Capybara starting Puma...
* Version 4.2.1 , codename: Distant Airhorns
* Min threads: 0, max threads: 4
* Listening on tcp://127.0.0.1:36608
And the test takes ages to load. There is a lot going on but I guess my question can be summarised by the following? When I install puma into my rails application, do I need to specify it on a specific port so it does not conflict with my app? Without puma, my tests can't run and my ./start-dev file works. With puma, my tests are kind of working but my ./start-dev file isn't anymore. Surely there must be a standard to configure puma. Thank you.
Install puma only for test environment by putting it into test group:
group :test do
gem "puma"
end
Or do as Capybara proposes and put Capybara.server = :webrick into your spec/rails_helper.rb
FYI: Rails uses puma as default web server if puma is installed

Does Heroku support ActionController::Live?

I have a Rails app hosted on Heroku and I want to add Server Sent Events functionalities, but I can't find any documentation or blog post specific for Heroku.
As not all servers (e.g. WEBrick) support ActionController::Live I was wondering what is the default server on Heroku and whether is possible to configure the environment (i.e. change server) to support SSEs.
Any further advice about the server to use and how to configure would be greatly appreciated.
I think my answer is not so widely helpfull, but you can try.
For the first thing:
create Procfile in rails root within the following content:
web: bundle exec rails server puma -p $PORT -e $RACK_ENV
then add to Gemfile:
gem 'puma'
In above you can switch to thin, but consider link below (and many more details)
http://tenderlovemaking.com/2012/07/30/is-it-live.html
Heroku wouldn't necessarily be the issue here - it's an environment which allows your app to run (on Amazon EC2 I think)
Multi-Threaded Servers
The thing you've got to look for is the server software you use to run your app. Heroku basically takes your server gem & allows it to run with their processors, and other computing power; so it's really whether their platform can play ball with the right server
You're really looking for multi-threaded servers, which you can find here Is puma the ONLY multi-threaded rails 4 http server?
Puma
Rainbows! supports multiple concurrency models, including multithreading
Zbatery - Rack HTTP server without a fork stuck in it
Phusion Passenger 4 has supported multithreading since its beta stages
Thin does have a threaded mode which can be enabled by passing
--threaded or by setting threaded: true in the appropriate configuration file (e.g. bundle exec thin start --threaded)
Net::HTTP::Server, despite the lack of advertising, supports
multithreading; very minimalist

Request-URI Too Large in Rails

I have 2 running environments of same rails app. one is development and second one is say staging. when i try to run my app on development environment then it runs good without any exception. but when i try to run same app on staging, then it throws an exception of Request-URI too large error. i run this app using webrick on both development and staging.
It seems to be that your request uri is longer than 2083 chars. This is the max uri size of the webrick. But to replace webrick with thin will help.
sudo gem install thin
and
thin start
Webrick can't handle URI's longer than MAX_URI_LENGTH = 2083 (according to this). And it's not really supposed to be run in production (some people say)
Try using thin. gem 'thin' in your Gemfile, installing and then rails s

Running nginx in rails automatically

I am using rails 2.3.9, rubygems 1.8.24, ruby 1.9.3 and Windows 7 ultimate 64-bit
I just installed nginx as my web server through passenger. Now I want to run nginx as my default server such that when i run ruby script/server, it runs instead of the default WeBrick. Is there any way to do this? Thanks a million.
Nginx doesn't work the way you described. Once it is started, you won't need to run script/server, the rails app will be run at the same time when the Nginx/Apache started.
So, just deploy your rails app following the 'Passenger' manual( in development mode), and you will get your app always running.
so, as conclusion, we can tell that, when deploying a Rails app, Nginx and Apache is in the same group( work together with Passenger), and Mongrel/Webrick/Thin is another group(script/server approach).
You may want to take a look at Foreman.

How to set Thin as default in Rails 3

I've been starting Thin with thin -V start in development.
However, I would like Thin to be the default instead of WEBrick and to be able to start it with rails s.
Is there a way to set Thin as the default instead of WEBrick in Rails 3?
If that's not possible, is there at least a way to start it in the test environment automatically?
I sent a pull request on the Github repository of rack and it was accepted:
https://github.com/rack/rack/commit/b487f02b13f42c5933aa42193ed4e1c0b90382d7
In a near future, we will be able to use Thin just by adding gem 'thin' to our Gemfile and starting app with rails s.
Note that this may be a temporary measure, however.
I chose Thin because Mongrel was not maintained currently and no other server seemed to suit as an alternative to Mongrel.
Alternatively you could use foreman, especially if your web applications tend to get more complicated to run (background workers, clock processes to handle scheduling, etc.)
Taking thin as an example, you would need to create a Procfile in your Rails app with the following content:
web: bundle exec rails server thin -p $PORT
Then just:
foreman start
to start your server.
You can run rails3 with thin using rails server thin
See the output of rails server -h for more options.
In Gem file use: gem 'thin'
bundle install
then rails s it will take thin as default server for your project.

Resources