sys.exec() no longer uses Promise
authorRyan Dahl <ry@tinyclouds.org>
Sat, 20 Feb 2010 00:55:46 +0000 (16:55 -0800)
committerRyan Dahl <ry@tinyclouds.org>
Sat, 20 Feb 2010 00:55:46 +0000 (16:55 -0800)
doc/api.txt
lib/sys.js
test/mjsunit/test-exec.js
test/mjsunit/test-keep-alive.js
test/mjsunit/test-remote-module-loading.js

index fbbe52704f226d1b59e8740f592eeb7ef8800ee1..dcdc66a9660f0a97ede69eca7463edde31983c57 100644 (file)
@@ -208,19 +208,21 @@ output the string immediately to stdout.
 +inspect(object, showHidden)+ ::
 Return a string representation of the +object+. (For debugging.)  If showHidden is true, then the object's non-enumerable properties will be shown too.
 
-+exec(command)+::
++exec(command, callback)+::
 Executes the command as a child process, buffers the output and returns it
-in a promise callback.
+in a callback.
 +
 ----------------------------------------
 var sys = require("sys");
-sys.exec("ls /").addCallback(function (stdout, stderr) {
+sys.exec("ls /", function (err, stdout, stderr) {
+  if (err) throw err;
   sys.puts(stdout);
 });
 ----------------------------------------
 +
-- on success: stdout buffer, stderr buffer
-- on error: exit code, stdout buffer, stderr buffer
+The callback gets the arguments +(err, stdout, stderr)+. On success +err+
+will be +null+. On error +err+ will be an instance of +Error+ and +err.code+
+will be the exit code of the child process.
 
 
 
index 65badcdc057b7d0a61e9a37834201ceb177a7840..cbdf0c578816d21f7a50c3e92139b3409cf0e5c9 100644 (file)
@@ -138,11 +138,10 @@ exports.p = function () {
   }
 };
 
-exports.exec = function (command) {
+exports.exec = function (command, callback) {
   var child = process.createChildProcess("/bin/sh", ["-c", command]);
   var stdout = "";
   var stderr = "";
-  var promise = new events.Promise();
 
   child.addListener("output", function (chunk) {
     if (chunk) stdout += chunk;
@@ -154,13 +153,13 @@ exports.exec = function (command) {
 
   child.addListener("exit", function (code) {
     if (code == 0) {
-      promise.emitSuccess(stdout, stderr);
+      if (callback) callback(null, stdout, stderr);
     } else {
-      promise.emitError(code, stdout, stderr);
+      var e = new Error("Command failed: " + stderr);
+      e.code = code;
+      if (callback) callback(e, stdout, stderr);
     }
   });
-
-  return promise;
 };
 
 /**
index 2098fdfd677e458bbc0a3cf23633438a96e4a0aa..f817ebd696fb3605a5b6c0c4b8fe150f9f0bdbbb 100644 (file)
@@ -3,32 +3,32 @@ process.mixin(require("./common"));
 success_count = 0;
 error_count = 0;
 
-exec("ls /").addCallback(function (out) {
-  success_count++;
-  p(out);
-}).addErrback(function (code, out, err) {
-  error_count++;
-  puts("error!: " + code);
-  puts("stdout: " + JSON.stringify(out));
-  puts("stderr: " + JSON.stringify(err));
+exec("ls /", function (err, stdout, stderr) {
+  if (err) {
+    error_count++;
+    puts("error!: " + err.code);
+    puts("stdout: " + JSON.stringify(stdout));
+    puts("stderr: " + JSON.stringify(stderr));
+  } else {
+    success_count++;
+    p(stdout);
+  }
 });
 
 
-
-exec("ls /DOES_NOT_EXIST").addCallback(function (out) {
-  success_count++;
-  p(out);
-  assert.equal(true, out != "");
-
-}).addErrback(function (code, out, err) {
-  error_count++;
-
-  assert.equal("", out);
-  assert.equal(true, code != 0);
-
-  puts("error!: " + code);
-  puts("stdout: " + JSON.stringify(out));
-  puts("stderr: " + JSON.stringify(err));
+exec("ls /DOES_NOT_EXIST", function (err, stdout, stderr) {
+  if (err) {
+    error_count++;
+    assert.equal("", stdout);
+    assert.equal(true, err.code != 0);
+    puts("error code: " + err.code);
+    puts("stdout: " + JSON.stringify(stdout));
+    puts("stderr: " + JSON.stringify(stderr));
+  } else {
+    success_count++;
+    p(stdout);
+    assert.equal(true, stdout != "");
+  }
 });
 
 
index 2307cd3bf43ff1d590e704773927fdf12900e3d5..b9ec2703f0011aceb54e871bc7e53ac9d611bea5 100644 (file)
@@ -23,22 +23,22 @@ function error (msg) {
 }
 
 function runAb(opts, callback) {
-  sys.exec("ab " + opts + " http://127.0.0.1:" + PORT + "/")
-    .addErrback(error)
-    .addCallback(function (out) {
-      var matches = /Requests per second:\s*(\d+)\./mi.exec(out);
-      var reqSec = parseInt(matches[1]);
-
-      matches = /Keep-Alive requests:\s*(\d+)/mi.exec(out);
-      var keepAliveRequests;
-      if (matches) {
-        keepAliveRequests = parseInt(matches[1]);
-      } else {
-        keepAliveRequests = 0;
-      }
-
-      callback(reqSec, keepAliveRequests);
-    });
+  var command = "ab " + opts + " http://127.0.0.1:" + PORT + "/";
+  sys.exec(command, function (err, stdout, stderr) {
+    if (err) throw err;
+    var matches = /Requests per second:\s*(\d+)\./mi.exec(stdout);
+    var reqSec = parseInt(matches[1]);
+
+    matches = /Keep-Alive requests:\s*(\d+)/mi.exec(stdout);
+    var keepAliveRequests;
+    if (matches) {
+      keepAliveRequests = parseInt(matches[1]);
+    } else {
+      keepAliveRequests = 0;
+    }
+
+    callback(reqSec, keepAliveRequests);
+  });
 }
 
 runAb("-k -c 100 -t 2", function (reqSec, keepAliveRequests) {
index 6f6f82e08053f47e49699526449bc5f20c834373..e49eed70a04da564e82b410378edc8d57aad6030 100644 (file)
@@ -26,15 +26,12 @@ assert.throws(function () {
 var nodeBinary = process.ARGV[0];
 var cmd = 'NODE_PATH='+libDir+' '+nodeBinary+' http://localhost:'+PORT+'/moduleB.js';
 
-sys
-  .exec(cmd)
-  .addCallback(function() {
-    modulesLoaded++;
-    server.close();
-  })
-  .addErrback(function(code, stdout, stderr) {
-    assertUnreachable('node binary could not load module from url: ' + stderr);
-  });
+sys.exec(cmd, function (err, stdout, stderr) {
+  if (err) throw err;
+  puts('success!');
+  modulesLoaded++;
+  server.close();
+});
 
 process.addListener('exit', function() {
   assert.equal(1, modulesLoaded);