5

A Jake task executes a long-running system command. Another task depends on the first task being completely finished before starting. The 'exec' function of 'child_process' executes system commands asynchronously, making it possible to for the second task to begin before the first task is complete.

What's the cleanest way to write the Jakefile to ensure that the long-running system command in the first task finishes before the second task starts?

I've thought about using polling in a dummy loop at the end of the first task, but this just smells bad. It seems like there must be a better way. I've seen this SO question, but it doesn't quite address my question.

var exec = require('child_process').exec;

desc('first task');
task('first', [], function(params) {
  exec('long running system command');
});

desc('second task');
task('second', ['first'], function(params) {
  // do something dependent on the completion of 'first' task
});
Community
  • 1
  • 1
Rich Apodaca
  • 28,316
  • 16
  • 103
  • 129

2 Answers2

2

I found the answer to my own question by re-rereading Matthew Eernisse's post. For those wondering how to do it:

var exec = require('child_process').exec;

desc('first task');
task('first', [], function(params) {
  exec('long running system command', function() {
    complete();
  });
}, true); // this prevents task from exiting until complete() is called

desc('second task');
task('second', ['first'], function(params) {
  // do something dependent on the completion of 'first' task
});
Rich Apodaca
  • 28,316
  • 16
  • 103
  • 129
  • 1
    Why not just have the first task emit a signal when it's done, and the listener for that signal spawns the second task? – Keith Jul 20 '11 at 02:28
1

Just for future reference, I have a synchronous exec module with no other dependencies.

Example:

var allsync = require("allsync");
allsync.exec( "find /", function(data){
    process.stdout.write(data);
});
console.log("Done!");

In the above exampale, Done is only printed after the find process exists. The exec function essentially blocks until complete.

Jacob Groundwater
  • 6,581
  • 1
  • 28
  • 42