Save Time,

Amit Narayanan
Mammoth Stories
Published in
3 min readMar 6, 2015

--

Start your App With Foreman

Managing Multiple Environments

One of the best advantages to give yourself is to keep multiple environments as similar as possible (ideally they’re exactly the same, but something or the other quickly makes that a pipe dream). Foremost in this consolidation is simplifying and standardizing the way an app is started.

Running Locally

Mammoth is built on Rails (among a fleet of other things). It is the Rails app that is the focus of this post.

Over time, our app got more complicated to run since it needed several different services to be functional.

rails start 

is fine and dandy, but for anything non-trivial, we want to do better. We needed multiple web process, multiple workers, a SOLR server, a Redis backed queue for workers, and 2 separate memcached instances (don’t ask☺). And starting each of these services manually gets old fast.

Foreman

A great gem that simplifies and makes this process elegant is Foreman. As added bonus, if you’re on Heroku, Foreman is automatically installed as part of Heroku Toolbelt.

Foreman is a command-line tool for running Procfile-backed apps. It’s installed automatically by the Heroku Toolbelt.

It looks something like this:

web1:           bundle exec unicorn -p $PORT -c ./config/unicorn.rb

To simplify it to the point of nothingness, on the left is what you want to call the process (call it anything) and to the right is how it needs to be started. Starting a worker ends up looking like this:

worker1:        bundle exec sidekiq -q default -c 2

And memcache(d) like so:

frag-cache:     memcached -vv -m 25 -p 11211
snippet-cache: memcached -vv -m 100 -p 11212

And SOLR (we do want our local environment fully indexed and searchable)

search:         bundle exec rake sunspot:solr:run

And finally Redis

redis:          redis-server /usr/local/etc/redis.conf

The Whole Nine Yards

Here’s the full Procfile for reference.

# I don't want to manually start everything I need... so

#
# Two web processes
#
web1: bundle exec unicorn -p $PORT -c ./config/unicorn.rb
web2: bundle exec unicorn -p $PORT -c ./config/unicorn.rb

#
# Workers
#
worker: bundle exec sidekiq -q default -c 2

#
# Two memcache instances, one for frags and another for snippets
#
frag-cache: memcached -vv -m 25 -p 11211
snippet-cache: memcached -vv -m 100 -p 11212

#
# One SOLR instance (run the one bundled within rails sunspot)
#
search: bundle exec rake sunspot:solr:run

#
# And finally Redis
#
redis: redis-server /usr/local/etc/redis.conf

Save this as Procfile in the app’s root dir, and now a simple

foreman start

boots up everything that our app needs. Brilliant.

We could even go further and call it Profile.dev (for obvious reasons) and now, it just changes to

foreman start -f Profile.dev

Finally, foreman defaults to port 5000. This shouldn’t matter for most purposes, but if it does, it’s trivial to change this with the -p option.

foreman start -p 3000 -f Procfile.dev

Thanks to foreman, one more spoke in our app dev is now elegant.

I’m @amitnarayanan on Twitter.

Also, check out Mammoth, a communication and publishing tool I’m currently building.

On the web and in the AppStore.

--

--