I have a project that runs mainly on NodeJS. During startup, it creates a socket.io+http server and a few workers (mainly event listeners that do their job on specific events).
Now I would like to implement a worker process that automatically checks if a new commit is available on a git repo that is hosted via GitLab on the same server.
So I would like to know:
Currently this project is running as a development server and I might turn off the auto-update function once it is in productive state. But since I develop on my laptop but test on my remote server, an auto-updater would be handy.
After some researching, I did find a solution, that actually seems to work too. Barely documented and a bit buggy - but it should work for the base purpose of a self-updating NodeJS app: http://registry.npmjs.org/gitlabhook
Here is how I coded it (taken directly from my code):
var fs=require("fs");
module.exports = function() {
// Dynamically write this config.
var obj = {
tasks: {
"*": [
"cd '"+config.base+"'",
"git pull",
"git submodule update",
"npm install",
"node lib/updater.js '%m'"
],
}
}, str = JSON.stringify(obj), glConf = config.base+"/config/gitlabhook.json";
log.info("BIRD3 Autp updater: Generating config to "+glConf);
fs.writeFileSync(glConf, str);
// Set it up
var gitlabhook = require("gitlabhook"),
gitlab = gitlabhook({
host: config.host,
configFile: "gitlabhook.json",
configPathes: [ config.base+"/config" ],
logger: log,
});
log.info("BIRD3 Auto updater: Starting");
gitlab.listen();
BIRD3.on("update", function(){
setTimeout(function(){
log.info("BIRD3 Auto updater: Exiting to allow update.");
process.exit(2);
}, 200);
});
log.info("BIRD3 Auto Updater -> Online!");
}
To explain:
updater.js
sends a message to a redis server - which, by another half of the app, is turned into a proper event.I see much potential in this module, and hope that it can develop further.
However, if you have another solution that work too, share it. I am sure that other people that may read this will find it useful, as it is a neat feature for continuous deployment.