Introducing Overmind and Hivemind
Never mind your usual Procfile manager, here’s 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 needed to use your app in the development environment. Rarely in the jungle of the modern web can a programmer get away by running one single process in the shell.
Next came the
Procfile—a format to specify the types of processes an application provides and the commands to run those services. It became a standard for Heroku and other Platforms-as-a-Service.
After that came Foreman. It allowed developers to launch all these processes with a single command. Everyone rejoiced, and David Dollar, who created it, got over 4k stars at GitHub. More similar tools would soon appear, and with it, a freedom of choice.
Sergey Aleksandrovich noticed something wrong with these options. Here’s how he explains it:
The problem with most of those tools is that the 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 lag, losing or breaking colored output. Tools can also add vanity information (unneeded timestamps in logs).
In an attempt to solve this problem (that might have been bugging you, too) Sergey turned his attention towards pseudoterminals.
TLC for the PTY
Pseudoterminals (pseudotty, or PTY) have been around since 1967. They’ve existed in the UNIX world since the 80’s and 2010’s The Linux Programming Interface contains an entire chapter explaining them.
An excellent breakdown of the concept was written by French programmer Rachid Koucha. Here’s an excerpt:
A pseudoterminal 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 the process manager behave in the terminal window! You don’t want processes to believe they’ve 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. So, 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 now run all the processes specified within simply 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 the path to the
Procfile you need.
Overmind goes up to eleven
Hivemind has won a lot of Evil Martians’ hearts, 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 (in somewhat of a chronological paradox) Hivemind’s big brother was born: 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 processes.
- 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 its magic, Overmind uses tmux, a terminal multiplexer. You’ll need to install it on your system first (with
homebrew on a Mac, or
apt-get on Ubuntu), this will allow you to 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 to use
tmux here, Overmind will do all the talking for you.
Let’s take a concrete case as an example. Let’s say you’ve developed a Rails app that uses Sidekiq for background jobs and a frontend builder to compile assets on the fly. You run all the processes you need with Foreman or a similar tool. Until recently, Sidekiq could not be hot reloaded, this means that if all your processes are bundled together, and you’re using an older version of Rails, the only way to restart Sidekiq is to interrupt and relaunch your entire Foreman session. With Overmind, you can quickly open a new tab in your shell and run
overmind restart worker: Sidekiq will restart, but the other processes will continue without interruption.
Now, how about some debugging?
Overmind allows us 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, but with Overmind, it’s as easy as opening a new tab and running the following:
overmind connect [process_name]
Here’s how that looks:
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!