Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to run rails puma server with config file using 'rails s puma'

I am able to run a puma server in rails using either rails s puma or just puma.

According to this answer, running rails s puma makes the server aware of the rails environment. It shows server errors etc that running puma alone does not.

I want to set a config file like so:

config/puma.rb

workers Integer(ENV['PUMA_WORKERS'] || 3)
threads Integer(ENV['MIN_THREADS']  || 1), Integer(ENV['MAX_THREADS'] || 16)

rackup      DefaultRackup
port        ENV['PORT']     || 3000
environment ENV['RACK_ENV'] || 'development'

...

If I run puma -C config/puma.rb everything works. However if I run rails s puma I can't work out how to give options to puma. I have tried the following:

rails s puma                     # Puma server works but no config file is passed in.
rails s puma -C config/puma.rb   # Invalid option -C
rails s puma -c config/puma.rb   # Undefined method 'workers'. So rails is
                                 # trying to use the config instead of puma?

I have also tried putting the config file at config/puma/development.rb as per the puma docs.

Appreciate any help on this :)

like image 659
Subtletree Avatar asked Aug 10 '14 04:08

Subtletree


People also ask

How do I start a puma server?

Puma has a built-in status and control app that can be used to query and control Puma. Puma will start the control server on localhost port 9293. All requests to the control server will need to include control token (in this case, token=foo ) as a query parameter. This allows for simple authentication.

Where is Puma config file?

By default, if no configuration file is specified, Puma will look for a configuration file at config/puma. rb. If an environment is specified, either via the -e and --environment flags, or through the RACK_ENV environment variable, the default file location will be config/puma/environment_name. rb.

How does PUMA work in Rails?

Puma allows you to configure your thread pool with a min and max setting, controlling the number of threads each Puma instance uses. The min threads setting allows your application to spin down resources when not under load.

How do I run a Rails server?

Go to your browser and open http://localhost:3000, you will see a basic Rails app running. You can also use the alias "s" to start the server: bin/rails s . The server can be run on a different port using the -p option. The default development environment can be changed using -e .


2 Answers

It is not possible to use rails s puma to load your puma configuration file, as confirmed here https://github.com/puma/puma/issues/512, you might want to take a look at a similar question here How do I get 'puma' to start, automatically, when I run `rails server` (like Thin does) where this is discussed

like image 150
Robert Christopher Avatar answered Sep 19 '22 05:09

Robert Christopher


I have found that using Foreman (https://github.com/ddollar/foreman) is a nice workaround for this, and gives additional flexibility as well.

Heroku has written a nice guide for this ( https://devcenter.heroku.com/articles/deploying-rails-applications-with-the-puma-web-server ). A very quick getting started is below.

Step 1: Install Foreman. Example for Mac OS X below, full guide on the Foreman site

$ brew install foreman

Step 2: Add this to your Gemfile:

gem 'puma'

Step 3: Create a file called Procfile:

web: bundle exec puma -C config/puma.rb

Step 4: Now start your application by using

$ foreman start

00:36:05 web.1  | started with pid 19869
00:36:05 web.1  | [19869] Puma starting in cluster mode...
00:36:05 web.1  | [19869] * Version 2.11.1 (ruby 2.2.1-p85), codename: Intrepid Squirrel
00:36:05 web.1  | [19869] * Min threads: 1, max threads: 1
00:36:05 web.1  | [19869] * Environment: development
00:36:05 web.1  | [19869] * Process workers: 1
00:36:05 web.1  | [19869] * Preloading application
00:36:07 web.1  | [19869] * Listening on tcp://0.0.0.0:3000
00:36:07 web.1  | [19869] Use Ctrl-C to stop
00:36:07 web.1  | [19869] - Worker 0 (pid: 19870) booted, phase: 0
like image 20
Jimmy S Avatar answered Sep 19 '22 05:09

Jimmy S