Skip to content

Commit

Permalink
WIP: trigger reload in running server process (draft fix for webxl#13)
Browse files Browse the repository at this point in the history
In the reload task, a request is made to
http://localhost:{port}/reloadTrigger.
If this succeeds, this emits reload messages in the running server process.
If the request fails, the server is started in the current process.
  • Loading branch information
manuel-woelker committed Dec 21, 2012
1 parent 13a4606 commit f438be5
Showing 1 changed file with 29 additions and 4 deletions.
33 changes: 29 additions & 4 deletions tasks/reload.js
Expand Up @@ -20,6 +20,7 @@ module.exports = function (grunt) {
var buffers = require('buffers');
var httpProxy = require('http-proxy');
var WebSocketServer = require("websocket").server;
var request = require('request');

var throttle = false;
// support for multiple reload servers
Expand Down Expand Up @@ -52,10 +53,8 @@ module.exports = function (grunt) {
};
}

grunt.registerTask('reload', "Reload connected clients when a file has changed.", function (target) {

function startServer(target) {
var server = servers[target ? target:'default'];

if (server) {
var errorcount = grunt.fail.errorcount;
// throttle was needed early in development because of rapid triggering by the watch task. Not sure if it's still necessary
Expand Down Expand Up @@ -179,6 +178,11 @@ module.exports = function (grunt) {
fs.createReadStream(__dirname + "/include/reloadClient.js").pipe(res); // use connect.static.send ?
}));

middleware.unshift(route('GET', '/triggerReload', function (req, res, next) {
taskEvent.emit('reload');
res.end("triggered");
}));

// if --debug was specified, enable logging.
if (grunt.option('debug')) {
connect.logger.format('grunt', ('[D] reloadServer :method :url :status ' +
Expand Down Expand Up @@ -241,7 +245,28 @@ module.exports = function (grunt) {
}

grunt.log.writeln("reload server running at http://localhost:" + port);

}

};

grunt.registerTask('reload', "Reload connected clients when a file has changed.", function (target) {
var done = this.async();
var that = this;
this.requiresConfig('reload');

// Get values from config, or use defaults.
var config = target ? grunt.config(['reload', target]) : grunt.config('reload');
var port = config.port || 8001;
request('http://localhost:'+port+'/triggerReload', function (error, response, body) {
if (!error && response.statusCode == 200) {
// Reload was triggered successfully
grunt.log.writeln("Triggered reload in running reload-server");
} else {
// Server doesn't seem to be running, start our own
startServer.call(that,target);
}
done();
});

});
};

0 comments on commit f438be5

Please sign in to comment.