If you’re interested in translating or adapting this post, pleaseemail us first.
Never mind your usual Procfile manager, here’s the Overmind and its little brother—Hivemind. Evil Martians use these tools built by Sergey Alexandrovich to manage Procfile-based applications for development. You may want to consider a switch too.
First, some history
First, there was
rails s and then there was
sidekiq, and then
webpack-dev-server, or whatever else you need to use your app in the development environment. Rarely in the jungle of the modern web a programmer can get away by running one single process in the shell.
Then came the
Procfile—a format to specify types of processes an application provides and commands to run those services. It became a standard for Heroku and other Platforms-as-a-Service.
Then came Foreman, and it allowed developers to launch all these processes with a single command, and everyone rejoiced, and David Dollar, who created it, got over 4K stars at GitHub. And then more similar tools appeared, and there was a freedom of choice.
Sergey Aleksandrovich has noticed there’s something wrong with these options. Here’s how he explains it:
The problem with most of those tools is that processes you want to manage start to think they are logging their output into a file, and that can lead to all sorts of problems: severe lagging, losing or breaking colored output. Tools can also add vanity information (unneeded timestamps in logs).
In an attempt to solve the problem, that may have bugged you too, Sergey turned his attention towards pseudoterminals.
TLC for the PTY
pty to preserve everything your processes want to output.
Pseudoterminals (pseudotty, or PTY) have been around since 1967. They’ve existed in UNIX-world since the 80’s and The Linux Programming Interface from 2010 contains an entire chapter explaining pseudoterminals. An excellent breakdown of the concept was written by a French programmer Rachid Koucha. Here’s the excerpt:
A pseudo-terminal is mainly used to make a process believe that it interacts with a terminal although it actually interacts with one or more processes.
That’s exactly what’s needed to make process manager behave in the terminal window! You don’t want processes to believe they output to a file, you want them to print neatly to the shell so that
sidekiq’s karate guy can keep on kicking.
Hivemind, written in Go, harnesses the power of
pty entirely. Now, whenever you have a
Procfile in your working directory, that looks something like this…
web: bundle exec rails server -p $PORT -b 0.0.0.0 worker: bundle exec sidekiq -C config/sidekiq.yml assets: gulp watch
…you can run all the processes specified in it just by typing
hivemind in your console. If you need to specify a path to a file located somewhere else, just follow your
hivemind command with a path to the
Procfile you need.
Overmind goes up to eleven
Overmind gives you everything Hivemind has to offer and much more.
Hivemind won a lot of hearts at Evil Martians, but after collecting some feedback, Sergey decided to up the ante. What if processes were still bundled together, but a developer could control them separately?
One idea led to another, and a few weeks later Hivemind got a big brother: Overmind. Here are some of its special powers:
- Overmind starts processes in a tmux session, so you can easily connect to any process and gain control over it;
- Overmind can restart a single process on the fly — you don’t need to restart the whole stack;
- Overmind allows a specified process to die without interrupting all of the other ones;
- Overmind can read environment variables from a file and use them as parameters so that you can configure Overmind behavior globally and/or per directory.
To do the magic, Overmind uses tmux, a terminal multiplexer. You need to install it on your system first (with
homebrew on a Mac or
apt-get on Ubuntu), it will let you switch between several programs in one terminal, detach them, while they are running in the background, and reattach them to a different terminal. Don’t worry, you don’t have to learn a new tool from scratch, Overmind will do all the talking with
tmux for you.
Let’s take a concrete case. You develop a Rails app that uses Sidekiq for background jobs and some frontend builder to compile assets on the fly. You run all the processes you need with Foreman or alike. Until recently, Sidekiq could not be hot reloaded, so if all your processes are bundled together, and you are using the older version of Rails, the only way to restart Sidekiq is to interrupt your whole Foreman session and relaunch. With Overmind, you can quickly open a new tab in your shell and run
overmind restart worker. Sidekiq will restart, but other processes will continue without interruption.
Now, how about some debugging?
Overmind allows to connect to any running process in a new shell window and interact with it on the fly. You often need to put
byebug in your Rails code and do some live debugging. A standard Procfile-manager does not allow you to do that, with Overmind it’s as easy as opening a new tab and running
overmind connect [process_name]
Here’s how it looks like:
You can also let select processes die gracefully by putting their names in the environment variable before launching Overmind:
OVERMIND_CAN_DIE=assets,npm_install overmind start
Alternatively, you can set any environment variables that configure Overmind inside
.overmind.env file in your project’s root.
For now, both tools only make sense during the development, but Sergey is already thinking about adding some features that may come handy in the production environment. Stay tuned!