Although the selected solution is shorter, here's what I'm doing in an angular application that is driven from grunt
The application has a frontend and an api backend.
First there is a shell
task for starting the api:
shell: {
api:{
options: {
execOptions : {
cwd: '..\\$working_directory'
},
callback : function (err, stdout, stderr, cb) {
restartServer('shell:api',err,cb);
}
},
command: function() {
return 'dnx --watch localfarm';
}
},
at the top of my gruntfile
I define the function that restarts the server:
var restartServer = function (task, err, cb) {
var timeoutInSec = 30;
if (err === null)
timeoutInSec = 2;
grunt.log.write(task + ' Task ended. Retrying in ' + timeoutInSec + ' seconds');
setTimeout(function() {
grunt.log.write('retrying ' + task );
grunt.task.run(task);
cb();
}, timeoutInSec*1000); };
Everytime something changes in the code, the dnx --watch
process is killed automatically. Grunt
then waits for 2 seconds and restarts the dnx
process. If dnx
fails, it waits for 30 seconds until it tries again. This gives me time to fix the code.
(In my version of the code I also use beep
to get a notification when the api is reloaded.)
To run the farm (frontend and backend) I added a concurrent task that starts multiple threads:
concurrent: {
...
farm: {
tasks: ['shell:api', 'serve'],
options: { logConcurrentOutput: true, limit: 10 }
}
...
}