From 6011f33f32039e243756775fe859d4fc2c537381 Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Thu, 26 Jul 2018 01:44:49 -0400 Subject: [PATCH 01/12] support for prerun capability --- lib/support/util.js | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/support/util.js b/lib/support/util.js index 6ff52f4..50ec37d 100644 --- a/lib/support/util.js +++ b/lib/support/util.js @@ -67,13 +67,15 @@ module.exports = function () { if (hidePid) { return container.process.colour(line) + '\n' } else { - return container.process.colour('[' + container.name + ' - ' + container.process.child.pid + ']: ' + line) + '\n' + var pid = (container.process.child && container.process.child.pid) || 'unknown' + return container.process.colour('[' + container.name + ' - ' + pid + ']: ' + line) + '\n' } } }) var errorColorizer = split(function (line) { - return chalk.red('[' + container.name + ' - ' + container.process.child.pid + ']: ' + line) + '\n' + var pid = (container.process.child && container.process.child.pid) || 'unknown' + return chalk.red('[' + container.name + ' - ' + pid + ']: ' + line) + '\n' }) var logStream = fs.createWriteStream(logPath + '/' + container.name + '.log') From 3520ea1fbcd7f95f98fffbb458e7eb3d27d6c58b Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Thu, 26 Jul 2018 01:45:21 -0400 Subject: [PATCH 02/12] support for prerun capability --- lib/support/processRunner.js | 50 +++++++++++++++++++++++++++++++++--- 1 file changed, 47 insertions(+), 3 deletions(-) diff --git a/lib/support/processRunner.js b/lib/support/processRunner.js index 3068e19..b5e2276 100644 --- a/lib/support/processRunner.js +++ b/lib/support/processRunner.js @@ -25,7 +25,11 @@ var util = require('./util')() module.exports = function () { var isWin = /^win/.test(process.platform) - var run = function (mode, container, exitCb, cb) { + var commonRun = function (isPrerun, mode, container, exitCb, cb) { + if (isPrerun && container.process) { + container.process.flags.prerunning = true + } + var child = {} var cwd = container.path var called = false @@ -34,7 +38,8 @@ module.exports = function () { var env var toks - toks = util.tokenizeCommand(container.run) + var command = isPrerun ? container.prerun : container.run + toks = util.tokenizeCommand(command) if (container.type === 'node') { cmd = process.argv[0] @@ -63,11 +68,22 @@ module.exports = function () { options.stdio[3] = 'ipc' } + if (isPrerun) { + console.log('pre-run step: ' + container.name + ' [' + container.prerun + ']') + } + child = spawn(cmd, args, options) child.unref() child.on('error', function (err) { if (!called) { + if (isPrerun) { + console.log('pre-run step had errors') + if (container.process) { + container.process.flags.prerunning = false + } + } + called = true exitCb(err, container, child, -1) } @@ -75,6 +91,12 @@ module.exports = function () { child.on('exit', function (code) { if (!called) { + if (isPrerun) { + console.log('pre-run step completed successfully') + if (container.process) { + container.process.flags.prerunning = false + } + } called = true exitCb(null, container, child, code) } @@ -90,11 +112,33 @@ module.exports = function () { cb(null, child) } + var prerun = function (mode, container, exitCb, cb) { + var runModeIsPrerun = true + commonRun(runModeIsPrerun, mode, container, exitCb, cb) + } + var run = function (mode, container, exitCb, cb) { + var runModeIsPrerun = false + commonRun(runModeIsPrerun, mode, container, exitCb, cb) + } var start = function (system, mode, container, exitCb, cb) { if (container && container.run) { - run(mode, container, exitCb, cb) + if (container.type === 'process' && container.prerun) { + // if there's a prerun task then execute that before running + prerun(mode, container, function (err, child) { + // when the pre-run task has exited we can continue + if (err) { + exitCb(err, container, child, -1) + } else { + run(mode, container, exitCb, cb) + } + }, function () { + // for the pre-run step we ignore standard callback + }) + } else { + run(mode, container, exitCb, cb) + } } else { console.log('warning: ' + container.name + ' not started, missing execute statement') cb(container.name + ' not started, missing execute statement') From 8eb9286eda7358df17a22ca6d942c90fa137cd07 Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Thu, 26 Jul 2018 01:46:02 -0400 Subject: [PATCH 03/12] support for prerun capability --- lib/system.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/lib/system.js b/lib/system.js index 5e156f6..8d8077a 100644 --- a/lib/system.js +++ b/lib/system.js @@ -72,9 +72,14 @@ module.exports = function () { return function (pth) { console.log('[' + container.name + '] file change: ' + path.basename(pth)) - if ((container.process && container.process.flags.running)) { + if (container.process && container.process.flags.running) { + var restartCausedByPrerun = container.process.flags.restarting && container.prerun; container.process.flags.restarting = true - stopContainer(container, function () {}) + if (!restartCausedByPrerun) { + stopContainer(container, function () {}) + } + } else if (container.process && container.process.flags.prerunning) { + // Do nothing } else { startContainer(container, function () {}) } @@ -148,7 +153,7 @@ module.exports = function () { var stopContainer = function (container, cb) { container.process.flags.stopping = true runner.stop(container, container.process.child.pid, function () { - if (container.process.monitor && !container.process.flags.restarting) { + if (container.process.monitor) { directoryWatcher.stop(container.process.monitor) container.process.monitor = null } From 6cc086a5d15552f5a2728011d60e8a2c36dfe156 Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Thu, 26 Jul 2018 21:10:18 -0400 Subject: [PATCH 04/12] fix: apply shouldn't execute prerun --- lib/support/processRunner.js | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/support/processRunner.js b/lib/support/processRunner.js index b5e2276..be1fb9d 100644 --- a/lib/support/processRunner.js +++ b/lib/support/processRunner.js @@ -195,6 +195,7 @@ module.exports = function () { c.run = command c.name = c.name + '_' + commandName c.type = 'process' + c.prerun = undefined start(system, 'live', c, exitCb(cb), function (err, child) { process = {identifier: c.name, From 0948c68d4de1e327905d1de6be3beba87d8f8557 Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Thu, 26 Jul 2018 23:35:47 -0400 Subject: [PATCH 05/12] Create prerun.yml --- test/fixture/system/fuge/prerun.yml | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 test/fixture/system/fuge/prerun.yml diff --git a/test/fixture/system/fuge/prerun.yml b/test/fixture/system/fuge/prerun.yml new file mode 100644 index 0000000..e0d30b9 --- /dev/null +++ b/test/fixture/system/fuge/prerun.yml @@ -0,0 +1,14 @@ +prerun: + type: process + path: ../prerun + prerun: node prerunbuild.js + run: node prerun.js + ports: + - main=8002 +prerunfail: + type: process + path: ../prerun + prerun: xxxxxxxxxxxxxx + run: node prerun.js + ports: + - main=8002 From 2b38f57d379d6d319c842e03113321f22ce0753b Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Thu, 26 Jul 2018 23:37:01 -0400 Subject: [PATCH 06/12] Create prerun.js --- test/fixture/system/prerun/prerun.js | 11 +++++++++++ 1 file changed, 11 insertions(+) create mode 100644 test/fixture/system/prerun/prerun.js diff --git a/test/fixture/system/prerun/prerun.js b/test/fixture/system/prerun/prerun.js new file mode 100644 index 0000000..6eb8fea --- /dev/null +++ b/test/fixture/system/prerun/prerun.js @@ -0,0 +1,11 @@ +'use strict' + +var http = require('http') +var resp = { resp: 'Hello World!\n' } + +var server = http.createServer(function (request, response) { + response.writeHead(200, {'Content-Type': 'text/plain'}) + response.end(resp.resp) +}) +server.listen(8002) +console.log('Server running at http://127.0.0.1:8002/') From 68b9e7ab5bb56c3abf11c96208b50476ce7dca97 Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Thu, 26 Jul 2018 23:37:27 -0400 Subject: [PATCH 07/12] Create prerunbuild.js --- test/fixture/system/prerun/prerunbuild.js | 1 + 1 file changed, 1 insertion(+) create mode 100644 test/fixture/system/prerun/prerunbuild.js diff --git a/test/fixture/system/prerun/prerunbuild.js b/test/fixture/system/prerun/prerunbuild.js new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/test/fixture/system/prerun/prerunbuild.js @@ -0,0 +1 @@ + From f69e2c56e665fae57eea5995a3f9327f9922e14a Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Thu, 26 Jul 2018 23:38:36 -0400 Subject: [PATCH 08/12] added prerun tests --- test/processRunner.test.js | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) diff --git a/test/processRunner.test.js b/test/processRunner.test.js index 3ad4035..1d4fda2 100644 --- a/test/processRunner.test.js +++ b/test/processRunner.test.js @@ -193,3 +193,34 @@ test('process kill test', function (t) { }) }) + +test('prerun test 1', function (t) { + t.plan(4) + + config.load(path.join(__dirname, 'fixture', 'system', 'fuge', 'prerun.yml'), function (err, system) { + t.equal(err, null) + runner.start(system, 'live', system.topology.containers.prerun, exitCb, function (err, child) { + t.equal(null, err) + t.notEqual(undefined, child.pid) + + setTimeout(function () { + runner.stop(system.topology.containers.prerun, child.pid, function (err) { + t.equal(null, err) + }) + }, 100) + }) + }) +}) + +test('prerun test 2 (failure test)', function (t) { + t.plan(3) + + config.load(path.join(__dirname, 'fixture', 'system', 'fuge', 'prerun.yml'), function (err, system) { + t.equal(err, null) + runner.start(system, 'live', system.topology.containers.prerunfail, exitCb, function (err, child) { + console.log('ERRRRRRRRRRRRR = ' + err) + t.notEqual(null, err) + t.equal(undefined, child) + }) + }) +}) From b8df668e6fb599a85b8e283989d9f2b07f3f5805 Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Thu, 26 Jul 2018 23:41:13 -0400 Subject: [PATCH 09/12] improved error handling --- lib/support/processRunner.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/support/processRunner.js b/lib/support/processRunner.js index be1fb9d..e89ee91 100644 --- a/lib/support/processRunner.js +++ b/lib/support/processRunner.js @@ -129,7 +129,7 @@ module.exports = function () { prerun(mode, container, function (err, child) { // when the pre-run task has exited we can continue if (err) { - exitCb(err, container, child, -1) + cb("prerun step error: " + err) } else { run(mode, container, exitCb, cb) } From 2ddefe08776fa03c35ec3f74a7199aaef98d4cd2 Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Thu, 26 Jul 2018 23:46:06 -0400 Subject: [PATCH 10/12] Update processRunner.test.js --- test/processRunner.test.js | 1 + 1 file changed, 1 insertion(+) diff --git a/test/processRunner.test.js b/test/processRunner.test.js index 1d4fda2..06ea1f6 100644 --- a/test/processRunner.test.js +++ b/test/processRunner.test.js @@ -224,3 +224,4 @@ test('prerun test 2 (failure test)', function (t) { }) }) }) + From 1fc0a89e417ea4361912d0260a8e5a980ac51229 Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Thu, 26 Jul 2018 23:50:39 -0400 Subject: [PATCH 11/12] Update processRunner.test.js --- test/processRunner.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/processRunner.test.js b/test/processRunner.test.js index 06ea1f6..1d4fda2 100644 --- a/test/processRunner.test.js +++ b/test/processRunner.test.js @@ -224,4 +224,3 @@ test('prerun test 2 (failure test)', function (t) { }) }) }) - From ab05baeb3ef9dd62e5b5daa0ed153e6510c92ae6 Mon Sep 17 00:00:00 2001 From: Kevin Berry <41717340+51ngul4r1ty@users.noreply.github.com> Date: Fri, 27 Jul 2018 00:05:55 -0400 Subject: [PATCH 12/12] Update processRunner.test.js --- test/processRunner.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/processRunner.test.js b/test/processRunner.test.js index 1d4fda2..2f72090 100644 --- a/test/processRunner.test.js +++ b/test/processRunner.test.js @@ -217,8 +217,8 @@ test('prerun test 2 (failure test)', function (t) { config.load(path.join(__dirname, 'fixture', 'system', 'fuge', 'prerun.yml'), function (err, system) { t.equal(err, null) + runner.start(system, 'live', system.topology.containers.prerunfail, exitCb, function (err, child) { - console.log('ERRRRRRRRRRRRR = ' + err) t.notEqual(null, err) t.equal(undefined, child) })