// Ruby on Rails Rails · Developer Tools

Rails: Start multiple processes with one command in development

6 min read · Ruby on Rails
Summary 6 min read

Real Rails apps depend on several processes in development: the web server, Sidekiq, Mailcatcher, Webpack. Instead of starting each by hand, use a Procfile.dev with Foreman, then wrap it in a bin/server script with flags, so every app on the team starts the same way.

Any app that goes beyond the very basics will probably have many components that it depends on. Even the smallest Rails app will probably have some background job handler (like sidekiq) that takes care of things like sending emails asynchronously or calling analytics APIs without blocking the user’s actions. (NB: If you don’t do that already in your app, you should definitely look into it, we’ve written a post about how you can easily add it to your app here.)

So how do you handle making sure that all of the services you need for your app to function are running in development? Do you manually start each one? That must be a pain. Especially in some cases where you figure out that you forgot to spin up a process after the fact, and now you have to go through the same work you just went through again. It’s a minor annoyance, but once it happens the second, third, or fourth time you start thinking about ways to automate that part.

Well, there’s an easy way to do that. If you’ve ever deployed to Heroku, you probably have had to setup a Procfile that looks something like this:

web:    bundle exec puma -C config/puma.rb
worker: bundle exec sidekiq -e production -C config/sidekiq.yml

What the Procfile is doing is behaving a like a manifest file and specifying that we need a web process which is started using the bundle exec puma -C config/puma.rband a worker process that executes the bundle exec sidekiq -e production -C config/sidekiq.yml command.

We can add as many of these processes as we want, using the sidekiq example, we can have multiple workers that handle different queues:

web:     bundle exec puma -C config/puma.rb
worker1: bundle exec sidekiq -e production -C config/sidekiq.yml -q mailers -q analytics
worker2: bundle exec sidekiq -e production -C config/sidekiq.yml -q default

To make use of the Procfile setup in development, all you need to do is use the foreman gem and then run the foreman s (or foreman start) command, which will then look for a Procfile in the directory from which you’re running the command and then start a separate process for each one you’ve defined.

Great, right? Ummmmm… yeah, but there’s one small problem. We have -e production specified for sidekiq and we want to run it in development mode instead. So how can we do that?

Easy. foreman let’s use specify a different Procfile to use so we can have multiple ones that we reference using the -f option: foreman s -f Procfile.dev. What’s happening here is we’re specifying to foreman to look for a file called Procfile.dev instead of the default Procfile. In Procfile.dev we can tweak the commands to match the development environment.

Moar!

Now that we have the initial setup figured out, can we make this do more?

The answer is yes! Let’s consider the following scenario: We are working on a Rails app that uses sidekiq for background jobs, mailcatcher for debugging emails in development, and also React with webpacker. What would our Procfile.dev look like?

web:       bundle exec puma -C config/puma.rb
worker1:   bundle exec sidekiq -e development -C config/sidekiq.yml -q mailers -q analytics
worker2:   bundle exec sidekiq -e development -C config/sidekiq.yml -q default
worker3:   mailcatcher --foreground --http-ip=0.0.0.0
webpacker: ./bin/webpack-dev-server --listen-host 0.0.0.0 --watch-poll

That works. Except for the scenario where we might be working heavily in the React part of the app and need to restart the webpack server a bunch of times. In the current setup we would have to stop foreman and start it back up, which means a long boot time since we have to wait for all of the services mentioned to restart. There’s a case to have two different Procfiles for each of the situations: run webpacker with everything else, or run it separately.

Let’s say we’ve created a file Procfile.dev that does NOT include the webpacker line and another one called Procfile.dev.w-webpack that does include the last line. Now, all we have to do is run foreman s -f and point to the right file.

Not a bad situation. But to be honest, that’s a lot to type if I just want to start the server in development. How can we make this better? For this part, I’m going to take a page out of the Rails book. If you look carefully, you’ll notice that a Rails app has a bin directory which is just a collection of scripts. The one we use mostly is bin/rails but they all are convenience scripts that are at our disposal to make us type less. The sensible thing is for us to create another one to run the server.

Let’s create a file called server in the bin directory, so we’ll be able to use the bin/server command to start our Rails app in development. The simplest form of the file would look like this:

#!/usr/bin/env sh
foreman start -f Procfile.dev

That’s easy. Now let’s update this script to work with our webpack scenario:

#!/usr/bin/env sh
while getopts ":w" opt; do
  case $opt in
    w)
      WITH_WEBPACK=true
      ;;
  esac
done
if [ $WITH_WEBPACK ]; then
  echo 'Staring server using Procfile.dev.w-webpack'
  foreman start -f Procfile.dev.w-webpack
else
  echo 'Staring server using Procfile.dev'
  foreman start -f Procfile.dev
fi

This might be a bit more overwhelming, but in reality, all this is doing is looking for a -w flag that is passed to the script. And depending on the presence of this flag, it will use the appropriate Procfile to either start webpack along with everything else, or not. So now we can run bin/server to start the server, mailcatcher, and sidekiq or bin/server -w to also start webpack along with the rest. Pretty neat, right? And since we can use tabbing to let the command line autocomplete the command for us, we don’t have to type a lot in order to fire up the app (for the first case it’s bin/ser + tab and bin/ser + tab + -w for the second case).

I think this is a good solution when working with a team of developers as well, across multiple apps. It reduces confusion and having to think which command do I need to run to get started. If it’s standardized to bin/server and then the script in each app handles the rest, now you have one common way to run the app in all cases.

TIP: Make sure your default option in the script handles the most common scenario, and then add options for any other cases. So for example, if your most common case is to run webpack with everything else, make that the default case and add a flag for the case where it is not (-no-w ) as opposed to how the script in the example code behaves.

Bear & Giraffe Advisory

We partner with founders and tech leads to build sustainable Rails architecture, untangle monoliths, and scale pragmatic systems.

Schedule a Strategy Call
ESC

Type to search 38 posts