Invoking subprocess in from Ruby

Anybody here good with invoking background processes in Ruby?

I just struggled with I thought was my misunderstanding of JavaScript, when I was simply forgetting to run the process that would compile my JavaScript code into the bundle used by Rails.

It’s too easy to start rails using rails s and not have the JS code update. It should be fine just run rails s in development mode. We also need to make sure that the js code is updated before rspec starts. Thus, we should have the Rails server invoke webpack, so that it’s impossible to forget to start webpack to do this background compilation.

In essence, we need to fork webpack watch processes to compile the bundle JS files when needed.

Basically, this means taking what’s in the forman file:

server: sh -c 'cd client && $(npm bin)/webpack -w --config webpack.server.js'
client: sh -c 'cd client && $(npm bin)/webpack -w --config webpack.client.js'
web: bin/rails s

And having this simply work by running rails s

The requirements would be:

  1. All stdout and stderr from the webpack commands goes to stdout for the rails server.
  2. The webpack processes quit when the rails server quits, and preferably even if the the rails server is killed with a -9.

This article on StackOverflow suggests we should use Ruby “fork”

Other options:

  • Spawn
  • Daemon
    Process.daemon true, true to keep std out and current directory.

Other references