Pre:ClojureRemote 17 — Node.js Server (restart (nodejs))

flyboarder
degree9
Published in
2 min readJan 2, 2017
ClojureRemote 17

During the New Year’s holiday I went back to tackle our Boot task for the Node.js server. After working through a few issues and spending much time in the Boot channel on Slack, I finally got the task in a restartable state.

To see why a restartable server saves time, get a ticket to ClojureRemote 17:

By making our server restartable, we don’t have to shutdown boot each time our server related files change.

Our Boot task beings by creating a temporary directory to work in. This keeps our server files isolated from the project working directory. We also store an instance of our server process within an atom, this allows us to work with the same application instead of spawning a new instance every time there is a change.

The real work is done by two small functions, start and stop. As the names imply they each either start or stop an instance of Node.js.

As for the task itself, it is rather simple, first we set a cleanup function to run whenever boot exits. This will make sure we are not leaving any unintended threads running.

Then we synchronize the output files from the fileset to our temporary directory. This happens each time our task is run by the fileset watcher.

We check for any existing instances of Node.js and perform stop. On our first run there shouldn’t be anything to worry about. On subsequent runs we are shutting down the the instance from the last run.

Finally we start Node.js in a new thread to avoid blocking and store that instance in our atom, from this point we are free to repeat the process.

--

--