Development Trick: auto-updating & auto-reloading

For the upgrade of JS Bin to version 3 (an open beta), the whole code base is in the process of being rewritten to run as a Node.js process (whilst still working as a PHP application).

What’s neat is I’ve got it to automatically pull the latest changes from github and automatically restart my Node process.

I don’t claim this to be particularly clever, and I’m sure I should be able to trigger a pull on the server as a push happens, but this works fairly well for my development workflow (where the app needs to be online) and is pretty simple – which I like.

Commands

For those like me who don’t care about the detail, here’s exactly what I’m doing in the terminal:

$ cd /WWW/jsbin/
$ screen -S gitpull
$ watch git pull
CTRL+a d
$ screen -S jsbin
$ nodemon .
CTRL+a d

Here’s what’s going on.

Screen

I’m using a linux process called screen which simply put: creates a persistent shell that you can send to the background and retrieve at a later date. This means I can run my processes inside a screen and get them back any time I want if there’s some debugging to do.

Now I’ve got a way of managing two tasks:

  1. Pull from github for any changes
  2. Restart my node process

Once you’re inside screen to detach (and return to your original shell) you use the command sequence CTRL+a d.

Here’s a few extra simple commands:

  • screen -ls will show all the screen processes you’re running and the PIDs they have. You’ll need the PID to reattach the screen
  • screen -r<PID> so in my case: screen -r gitpull will reattach and put me back in to the screen

Auto-updating code

Inside one screen we make use of watch which will keep running a process every 2 seconds. So I “watch” the git pull command.

Obviously this is a bit crap because it’s trying to pull from github to no avail most of the time, but it does mean that when I push new code up, this process will automatically pull.

Which leads me on to restarting…

Auto-reloading

In another screen I’m running the development tool Nodemon to auto restart when any JavaScript files change (which come from the git pull).

So that’s it. Pretty simple.

5 Responses to “Development Trick: auto-updating & auto-reloading”

  1. Rickard Andersson June 16th, 2012 at 6:17 pm

    Another way you could set it up is with one screen and multiple windows. Create new windows with ctrl+a+c, delete them with ctrl+a+K and cycle through them with ctrl+a+space

  2. why don’t you set up the server you pull on as a remote of your local git repo (assuming you push to github from there)? then a git hook on the server could restart the node whenever you push. this way it would all be push and on demand only

  3. A post-receive hook could be used to the same effect (https://help.github.com/articles/post-receive-hooks/) – basically you can have GH post to an arbitrary url whenever you push, and then have that trigger your pull of new code instead of using watch.

    Bit cleaner and no need for screen either.

  4. @Tom – I do know about post-receive-hooks, and I do know that that’s the “right way” of doing it. My issue is a) it’s complicated, but more importantly b) I’ve tried before and failed because it requires special permission set up. Your git repo needs to be writable by the same user access as the web server runs under, and as much as I’m comfortable in linux, I couldn’t quite get the combination of permissions to work.

  5. @Rocco – this sounds like a decent solution, and if there was just one entry point for the development, a sound solution. However, there’s multiple developers working from multiple machines (I even make changes directly inside of github and commit from there on rare occasion), so it wouldn’t pick the changes. Either way, good suggestion, but not quite right for this particular problem :)

Leave a Reply
Not required

CODE: Please escape code and wrap in <pre><code>, doing so will automatically syntax highlight