Tuesday, November 20, 2012

Cool Tools: Foreman


The process of building web apps these days has become exponentially easier with all of the open source projects out there to provide data persistance, messaging, job scheduling, etc... you name it and someone has probably built it and open sourced it.  This is great for both the software community and entrepreneuers who need to build a product quickly to get in front of potential customers. Now with some research you can create very powerful applications with only marginal effort by leverage and connecting these services using good client libraries and some application logic you fill in.  Unfortunately then your app needs to have these services running for you to boot it up and start development, testing, and deployments. 

The problem is that you don't want to necessarily run everything all the time on your laptop to be able to sit down and crank out some features.  Modern laptops pack a lot of power and computing resources, but if you are an ultra-light kind of person with a Macbook Air, you still want to be a little choosy about what's running and what contextually makes sense for the current tasks you want to accomplish.  I personally detest the idea of running something like MySQL server all the time just so that I don't have to remember to kick off the server daemon (mysqld, fyi) when I want ot write a little code.  Enter Foreman by David Dollar.

Foreman is a tool that allows you to declaratively write out the list of services you need to run for your application and capture them in a simple file called Procfile.  For instance, stick this in the root of your Rails app, fill it in with the services you need, and when you want to start working open a terminal to the root directory of your Rails app and type foreman start.  This will start up all the services listed in your Procfile and connect stdout and stderr to the terminal window.  Each service is color-coded to make it easy to visually inspect the output, and when you're done developing just send hit ctrl-c in the foreman terminal and all the running services will be gracefully shut down.

Here is a sample Procfile for starting mysql, redis, and resque.  The format is very simple.  Each line is the name you would like to refer to the service as in the foreman output followed by a colon and then the shell command to start the service:



To get Foreman installed, just do gem install foreman, create the Procfile in your project, fill it in with your services, and start the service with foreman start.  You're now ready to start working.  Pretty awesome.

A few other noteworthy features of foreman include an ability to export your Procfile as an upstart or Unix init control script for your production environments, and the ability to run multiple concurrent processes for any of your services.  Check the documentation on the Foreman page to take advantage of these awesome features.

No comments:

Post a Comment