Cscott has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/209783

Change subject: Update P.fork().
......................................................................

Update P.fork().

Import improvements from mw-ocg-latexer.

Change-Id: I1b3997bea04deba314106590bd61731e18493c80
---
M lib/index.js
M lib/p.js
M samples/featured.js
3 files changed, 37 insertions(+), 9 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Collection/OfflineContentGenerator/bundler
 refs/changes/83/209783/1

diff --git a/lib/index.js b/lib/index.js
index f47a620..7dd61f8 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -397,7 +397,7 @@
                        params.unshift('-n', '.db');
                }
                var p = P.spawn('zip', params, {
-                       cwd: options.output
+                       childOptions: { cwd: options.output }
                });
 
                // always clean up at the end
diff --git a/lib/p.js b/lib/p.js
index 93efc77..efcf20e 100644
--- a/lib/p.js
+++ b/lib/p.js
@@ -18,18 +18,46 @@
 // Returns a promise for completion after spawning `program`
 P.spawn = function(program, args, options) {
        return new Promise(function(resolve, reject) {
-               spawn(program, args || [], options || { stdio: 'inherit' }).
-                       on('exit', function(exitCode) {
+               options = options || {};
+               options.childOptions = options.childOptions || {};
+               options.childOptions.stdio = options.childOptions.stdio || 
'inherit';
+               var killTimer = null, killed = false;
+               var clearKillTimer = function() {
+                       if (killTimer) {
+                               clearTimeout(killTimer);
+                               killTimer = null;
+                       }
+               };
+               var child = spawn(program, args || [], options.childOptions).
+                       on('exit', function(exitCode, signal) {
+                               clearKillTimer();
                                if (exitCode === 0) {
-                                       resolve();
-                               } else {
-                                       reject(new Error(
-                                               program+' '+args.join(' ')+' 
exited with code '+exitCode
-                                       ));
+                                       return resolve();
                                }
+                               var timeout = killed && /* maybe we tried, but 
failed */
+                                       ( signal === 'SIGTERM' || signal === 
'SIGKILL');
+                               var e = new Error(
+                                       program + ' ' + args.join(' ') + ' ' +
+                                       (timeout ? 'exceeded execution time' : 
'exited with code '+exitCode)
+                               );
+                               e.code = exitCode;
+                               e.signal = signal;
+                               e.timeout = timeout;
+                               return reject(e);
                        }).on('error', function(err) {
+                               clearKillTimer();
                                reject(err);
                        });
+               if (options.timeout) {
+                       killTimer = setTimeout(function() {
+                               killed = true;
+                               child.kill('SIGTERM');
+                               killTimer = setTimeout(function() {
+                                       child.kill('SIGKILL');
+                                       killTimer = null;
+                               }, options.timeout * 2);
+                       }, options.timeout);
+               }
        });
 };
 
diff --git a/samples/featured.js b/samples/featured.js
index 306ff32..b466bad 100755
--- a/samples/featured.js
+++ b/samples/featured.js
@@ -60,6 +60,6 @@
                then(function() { }, function() { /* ignore unlink errors */ }).
                then(function() {
                        return P.spawn(path.join(__dirname, '..', 'bin', 
'mw-ocg-bundler'),
-                                                  args, { cwd: __dirname, 
stdio: 'inherit' });
+                                                  args, { childOptions: { cwd: 
__dirname, stdio: 'inherit' } });
                });
 }).done();

-- 
To view, visit https://gerrit.wikimedia.org/r/209783
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I1b3997bea04deba314106590bd61731e18493c80
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Collection/OfflineContentGenerator/bundler
Gerrit-Branch: master
Gerrit-Owner: Cscott <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to