Managing your web applications with Foreman | Adventures In Coding

Managing your web applications with Foreman

  • Kevin Faustino
Foreman

Not all applications are created equal. While some applications can be as simple as a Ruby on Rails web app connecting to a database, others may have dependencies such as background workers. David Dollar realized his web applications were getting a lot more complicated to run, which resulted in him creating Foreman. Foreman is an excellent tool which allows you to encompass your entire application structure in a Procfile. A Procfile allows you to declare multiple processes that encompass your application and run them all with a single foreman start command.

Procfile

A Procfile is a list that contains a name to identify the process and the command used by the system to run it. Foreman will look for it by default in your project's root directory.

Here is an example Procfile of a Ruby on Rails application running Resque workers in the background:

web:    bundle exec thin start -p $PORT
worker: bundle exec rake resque:work QUEUE=*

Getting Started

Once your Procfile is in place, install the foreman gem:

$ gem install foreman

To start up your application from the command line, all you need to do is run foreman start. By default, this will run one instance of the each application type defined in your Procfile.

Concurrency

Foreman supports the ability to run more than 1 process of each type via the concurrency -c parameter. You must define the number of each process type to run.

For example, to increase the number of worker processes to two, execute:

$ foreman start -c web=1,worker=2

.env

Another great benefit to Foreman is the ability to define local ENV variables for development. By creating a .env file in the root directory of the project, Foreman will load all key/value pairs specified in the file into the default environment.

S3_KEY=abc
S3_SECRET=def

Be sure to include .env in your .gitignore file as well to ensure sensitive information is not stored in your repository.

Procfile.development

Sometimes you will want a different Procfile for development . For example, you may want to run redis on your local machine while developing your application to save system resources. A convention I use is that I suffix .development to my development Procfile.

# Procfile.development
web:    bundle exec thin start -p $PORT
redis:  redis-server /usr/local/etc/redis.conf
worker: bundle exec rake resque:work QUEUE=*

To run Foreman with a different Procfile, run foreman with the -f parameter:

$ foreman start -f Procfile.development

Further Reading

For more information, check out the following:


Comments

blog comments powered by Disqus