Arlolra has uploaded a new change for review. https://gerrit.wikimedia.org/r/289508
Change subject: WIP: Replace custom server.js with service-runner ...................................................................... WIP: Replace custom server.js with service-runner * This is installed with --no-optional to avoid the binary dep on dtrace-provider. Change-Id: I41265ecd955259f29af0fc84a53a0af489a8ec79 --- A config.yaml M lib/index.js M npm-shrinkwrap.json M package.json M tests/apiServer.js M tests/mocha/api.js M tests/mocha/templatedata.js M tests/mockAPI.js M tests/testreduce/rtTestWrapper.js 9 files changed, 335 insertions(+), 66 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid refs/changes/08/289508/1 diff --git a/config.yaml b/config.yaml new file mode 100644 index 0000000..216ccef --- /dev/null +++ b/config.yaml @@ -0,0 +1,5 @@ +services: + - module: lib/index.js + entrypoint: apiServiceWorker + conf: + localsettings: ./localsettings.js \ No newline at end of file diff --git a/lib/index.js b/lib/index.js index 6dd6017..b96baf0 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,6 +1,7 @@ 'use strict'; require('../core-upgrade.js'); +var path = require('path'); var json = require('../package.json'); var parseJs = require('../bin/parse.js'); var ParsoidConfig = require('./config/ParsoidConfig.js').ParsoidConfig; @@ -149,6 +150,10 @@ * @return {Promise} a Promise for an `http.Server`. */ Parsoid.apiServiceWorker = function apiServiceWorker(options) { - var parsoidConfig = new ParsoidConfig(null, options.config); + var localSettings = null; + if (options.config.localsettings) { + localSettings = require(path.resolve(process.cwd(), options.config.localsettings)); + } + var parsoidConfig = new ParsoidConfig(localSettings, options.config); return ParsoidService.init(parsoidConfig, options.logger); }; diff --git a/npm-shrinkwrap.json b/npm-shrinkwrap.json index 8a375c5..7ecab62 100644 --- a/npm-shrinkwrap.json +++ b/npm-shrinkwrap.json @@ -4432,6 +4432,287 @@ } } }, + "service-runner": { + "version": "1.2.2", + "resolved": "https://registry.npmjs.org/service-runner/-/service-runner-1.2.2.tgz", + "dependencies": { + "bluebird": { + "version": "3.2.2", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.2.2.tgz" + }, + "bunyan": { + "version": "1.8.1", + "resolved": "https://registry.npmjs.org/bunyan/-/bunyan-1.8.1.tgz" + }, + "bunyan-syslog-udp": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/bunyan-syslog-udp/-/bunyan-syslog-udp-0.1.0.tgz" + }, + "core-js": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/core-js/-/core-js-2.4.0.tgz" + }, + "extend": { + "version": "3.0.0", + "resolved": "https://registry.npmjs.org/extend/-/extend-3.0.0.tgz" + }, + "gelf-stream": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/gelf-stream/-/gelf-stream-1.1.1.tgz", + "dependencies": { + "gelfling": { + "version": "0.3.0", + "resolved": "https://registry.npmjs.org/gelfling/-/gelfling-0.3.0.tgz" + } + } + }, + "hot-shots": { + "version": "2.4.0", + "resolved": "https://registry.npmjs.org/hot-shots/-/hot-shots-2.4.0.tgz" + }, + "js-yaml": { + "version": "3.6.1", + "resolved": "https://registry.npmjs.org/js-yaml/-/js-yaml-3.6.1.tgz", + "dependencies": { + "argparse": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/argparse/-/argparse-1.0.7.tgz", + "dependencies": { + "sprintf-js": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/sprintf-js/-/sprintf-js-1.0.3.tgz" + } + } + }, + "esprima": { + "version": "2.7.2", + "resolved": "https://registry.npmjs.org/esprima/-/esprima-2.7.2.tgz" + } + } + }, + "limitation": { + "version": "0.1.8", + "resolved": "https://registry.npmjs.org/limitation/-/limitation-0.1.8.tgz", + "dependencies": { + "bluebird": { + "version": "3.4.0", + "resolved": "https://registry.npmjs.org/bluebird/-/bluebird-3.4.0.tgz" + }, + "kad": { + "version": "1.3.6", + "from": "kad@git+https://github.com/gwicke/kad.git#31449a27a289b6f0ecbeec193d8bac9db8b9f513", + "resolved": "git+https://github.com/gwicke/kad.git#31449a27a289b6f0ecbeec193d8bac9db8b9f513", + "dependencies": { + "clarinet": { + "version": "0.11.0", + "resolved": "https://registry.npmjs.org/clarinet/-/clarinet-0.11.0.tgz" + }, + "hat": { + "version": "0.0.3", + "resolved": "https://registry.npmjs.org/hat/-/hat-0.0.3.tgz" + }, + "kad-fs": { + "version": "0.0.4", + "resolved": "https://registry.npmjs.org/kad-fs/-/kad-fs-0.0.4.tgz" + }, + "kad-localstorage": { + "version": "0.0.7", + "resolved": "https://registry.npmjs.org/kad-localstorage/-/kad-localstorage-0.0.7.tgz", + "dependencies": { + "dom-storage": { + "version": "2.0.2", + "resolved": "https://registry.npmjs.org/dom-storage/-/dom-storage-2.0.2.tgz" + } + } + }, + "kad-memstore": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/kad-memstore/-/kad-memstore-0.0.1.tgz" + }, + "lodash": { + "version": "3.10.1", + "resolved": "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz" + }, + "merge": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/merge/-/merge-1.2.0.tgz" + }, + "ms": { + "version": "0.7.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-0.7.1.tgz" + }, + "msgpack5": { + "version": "3.3.0", + "resolved": "https://registry.npmjs.org/msgpack5/-/msgpack5-3.3.0.tgz", + "dependencies": { + "bl": { + "version": "1.1.2", + "resolved": "https://registry.npmjs.org/bl/-/bl-1.1.2.tgz", + "dependencies": { + "readable-stream": { + "version": "2.0.6", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.0.6.tgz", + "dependencies": { + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz" + }, + "process-nextick-args": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz" + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz" + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz" + } + } + } + } + }, + "inherits": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz" + } + } + } + } + }, + "readable-stream": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-2.1.2.tgz", + "dependencies": { + "core-util-is": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz" + }, + "inherits": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.1.tgz" + }, + "isarray": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/isarray/-/isarray-1.0.0.tgz" + }, + "process-nextick-args": { + "version": "1.0.7", + "resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-1.0.7.tgz" + }, + "string_decoder": { + "version": "0.10.31", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-0.10.31.tgz" + }, + "util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz" + } + } + } + } + }, + "yargs": { + "version": "3.32.0", + "resolved": "https://registry.npmjs.org/yargs/-/yargs-3.32.0.tgz", + "dependencies": { + "camelcase": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/camelcase/-/camelcase-2.1.1.tgz" + }, + "cliui": { + "version": "3.2.0", + "resolved": "https://registry.npmjs.org/cliui/-/cliui-3.2.0.tgz", + "dependencies": { + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "dependencies": { + "ansi-regex": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.0.0.tgz" + } + } + }, + "wrap-ansi": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/wrap-ansi/-/wrap-ansi-2.0.0.tgz" + } + } + }, + "decamelize": { + "version": "1.2.0", + "resolved": "https://registry.npmjs.org/decamelize/-/decamelize-1.2.0.tgz" + }, + "os-locale": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/os-locale/-/os-locale-1.4.0.tgz", + "dependencies": { + "lcid": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/lcid/-/lcid-1.0.0.tgz", + "dependencies": { + "invert-kv": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/invert-kv/-/invert-kv-1.0.0.tgz" + } + } + } + } + }, + "string-width": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/string-width/-/string-width-1.0.1.tgz", + "dependencies": { + "code-point-at": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/code-point-at/-/code-point-at-1.0.0.tgz", + "dependencies": { + "number-is-nan": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.0.tgz" + } + } + }, + "is-fullwidth-code-point": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/is-fullwidth-code-point/-/is-fullwidth-code-point-1.0.0.tgz", + "dependencies": { + "number-is-nan": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/number-is-nan/-/number-is-nan-1.0.0.tgz" + } + } + }, + "strip-ansi": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/strip-ansi/-/strip-ansi-3.0.1.tgz", + "dependencies": { + "ansi-regex": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/ansi-regex/-/ansi-regex-2.0.0.tgz" + } + } + } + } + }, + "window-size": { + "version": "0.1.4", + "resolved": "https://registry.npmjs.org/window-size/-/window-size-0.1.4.tgz" + }, + "y18n": { + "version": "3.2.1", + "resolved": "https://registry.npmjs.org/y18n/-/y18n-3.2.1.tgz" + } + } + } + } + }, "simplediff": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/simplediff/-/simplediff-0.1.1.tgz" diff --git a/package.json b/package.json index 3eaf42e..b8f2fb8 100644 --- a/package.json +++ b/package.json @@ -27,6 +27,7 @@ "request": "^2.63.0", "semver": "^5.1.0", "serve-favicon": "^2.3.0", + "service-runner": "^1.2.2", "simplediff": "^0.1.1", "yargs": "^1.3.1" }, @@ -50,7 +51,7 @@ "scripts": { "lint": "npm run dump-tokenizer && npm run jscs && npm run jshint", "no-0.8": "node -e 'process.exit(/v0[.][0-8][.]/.test(process.version) ? 0 : 1)' || (npm run nsp && npm run lint)", - "start": "node bin/server.js", + "start": "service-runner", "jshint": "jshint bin lib tests tools core-upgrade.js", "jscs": "jscs bin lib tests tools core-upgrade.js", "jscs-fix": "jscs --fix bin lib tests tools core-upgrade.js", diff --git a/tests/apiServer.js b/tests/apiServer.js index 03f8c0a..7f490b1 100644 --- a/tests/apiServer.js +++ b/tests/apiServer.js @@ -6,51 +6,13 @@ * Uses port randomization to make sure we can use multiple servers concurrently. */ +// var cluster = require('cluster'); var childProcess = require('child_process'); var path = require('path'); +var ServiceRunner = require('service-runner'); -var Promise = require('../lib/utils/promise.js'); -var Util = require('../lib/utils/Util.js').Util; - -// Keep all started servers in a map indexed by the url -var forkedServers = new Map(); -var exiting = false; - -var stopServer = function(url) { - var forkedServer = forkedServers.get(url); - if (forkedServer) { - // Prevent restart if we explicitly stop it - forkedServer.child.removeAllListeners('exit'); - forkedServer.child.kill(); - forkedServers.delete(url); - } -}; - -var stopAllServers = function() { - forkedServers.forEach(function(forkedServer, url) { - stopServer(url); - }); -}; - -/** - * Make sure the servers are killed if the process exits. - */ -process.on('exit', function() { - exiting = true; - stopAllServers(); -}); - -var exitOnProcessTerm = function(res) { - var stopAndExit = function() { - process.exit(res || 0); - }; - process.on('SIGINT', stopAndExit); - process.on('SIGTERM', stopAndExit); - process.on('uncaughtException', function(e) { - console.log(e.stack); - process.exit(1); - }); -}; +// var Promise = require('../lib/utils/promise.js'); +// var Util = require('../lib/utils/Util.js').Util; /** * Starts a server on passed port or a random port if none passed. @@ -140,18 +102,21 @@ iface: "localhost", port: 0, // Select a port at random. urlPath: "/", - filePath: '/../bin/server.js', + filePath: '/../node_modules/.bin/service-runner', serverArgv: [ // we want the cluster master so that timeouts on stuck titles lead to a restart. '--num-workers', '1', - '--config', path.resolve(__dirname, './rttest.localsettings.js'), + '--config', path.resolve(__dirname, './rttest.config.yaml'), ], serverEnv: {}, }; // Returns a Promise. (see startServer) var startParsoidServer = function(opts) { + + opts = !opts ? parsoidServerOpts : Util.extendProps(opts, parsoidServerOpts); + return startServer(opts); }; @@ -168,8 +133,24 @@ // Returns a Promise (see startServer) var startMockAPIServer = function(opts) { opts = !opts ? mockAPIServerOpts : Util.extendProps(opts, mockAPIServerOpts); - return startServer(opts); + var conf = { + num_workers: 3, + services: [{ + module: path.join(__dirname, './mockAPI.js'), + conf: { + port: 0, // Select a port at random. + iface: 'localhost', + urlPath: '/api.php', + } + }], + }; + var runner = new ServiceRunner(); + return runner.run(conf); }; + +startMockAPIServer().then(function(ret) { + console.log(ret) +}); module.exports = { // These functions aren't currently used outside this module, so @@ -178,8 +159,6 @@ startServer: startServer, stopServer: stopServer, */ - stopAllServers: stopAllServers, startParsoidServer: startParsoidServer, startMockAPIServer: startMockAPIServer, - exitOnProcessTerm: exitOnProcessTerm, }; diff --git a/tests/mocha/api.js b/tests/mocha/api.js index cac51cc..a2178b6 100644 --- a/tests/mocha/api.js +++ b/tests/mocha/api.js @@ -26,13 +26,12 @@ mockUrl: ret.url, serverArgv: [ '--num-workers', '1', - '--config', configPath, + '--config', path.resolve(__dirname, './apitest.config.yaml'), ], }); }).then(function(ret) { api = ret.url; }); - apiServer.exitOnProcessTerm(); return p; }); diff --git a/tests/mocha/templatedata.js b/tests/mocha/templatedata.js index 8acaa82..3b7b975 100644 --- a/tests/mocha/templatedata.js +++ b/tests/mocha/templatedata.js @@ -205,7 +205,6 @@ }).then(function(ret) { api = ret.url; }); - apiServer.exitOnProcessTerm(); return p; }); diff --git a/tests/mockAPI.js b/tests/mockAPI.js index 5285597..97d63b4 100644 --- a/tests/mockAPI.js +++ b/tests/mockAPI.js @@ -475,16 +475,17 @@ handleApiRequest(req.body, res); }); -module.exports = app; - -var port = process.env.PORT || 7001; -var server = app.listen(port, function() { - port = server.address().port; - console.log('Mock MediaWiki API started on: %s', port); - // let parent process know we've started up and are ready to go. - if (process.send) { process.send({ type: 'startup', port: port }); } -}); -app.on('error', function(e) { - if (process.send) { process.send({ type: 'error', code: e.code }); } - console.log('Could not start up:', e); -}); +module.exports = function(options) { + var server; + return new Promise(function(resolve, reject) { + server = app.listen(options.config.port, options.config.iface, resolve); + app.on('error', function(e) { + console.log('Mock MediaWiki API: Error', e); + reject(e); + }); + }).then(function() { + var port = server.address().port; + console.log('Mock MediaWiki API: Started on %s', port); + return { port: port }; + }); +}; diff --git a/tests/testreduce/rtTestWrapper.js b/tests/testreduce/rtTestWrapper.js index 6e62bc3..0697c03 100644 --- a/tests/testreduce/rtTestWrapper.js +++ b/tests/testreduce/rtTestWrapper.js @@ -36,7 +36,6 @@ config.parsoidURL = parsoidURL = ret.url; return _run(test); }); - apiServer.exitOnProcessTerm(); return p; } }; -- To view, visit https://gerrit.wikimedia.org/r/289508 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I41265ecd955259f29af0fc84a53a0af489a8ec79 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/services/parsoid Gerrit-Branch: master Gerrit-Owner: Arlolra <abrea...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits