Arlolra has uploaded a new change for review.

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

Change subject: Let the OS randomize ports
......................................................................

Let the OS randomize ports

 * Set the port to 0 indicating that the OS should pick a free port.
   We're sending messages back up the chain anyways to indicate that
   it's ready so just tack the choice on there.

 * This follow on I89cf62fd838f0300724d0f0dc53c39d89a75b57f and
   Id50d2be28d4cd7c22d111bb1c4d0a72c10c88650.

Change-Id: I8fba8f83a78ba97527dc3908591c24d0334d178c
---
M api/ParsoidService.js
M tests/apiServer.js
M tests/mocha/api.js
M tests/mockAPI.js
4 files changed, 27 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/services/parsoid 
refs/changes/23/234023/1

diff --git a/api/ParsoidService.js b/api/ParsoidService.js
index e460ebe..34dcdcf 100644
--- a/api/ParsoidService.js
+++ b/api/ParsoidService.js
@@ -162,7 +162,8 @@
        // default bind all
        var host = parsoidConfig.serverInterface || process.env.INTERFACE;
 
-       app.listen(port, host, function() {
+       var server = app.listen(port, host, function() {
+               port = server.address().port;
                processLogger.log("info", util.format("ready on %s:%s", host || 
"", port));
                if (process.send) {
                        // let cluster master know we've started & are ready to 
go.
diff --git a/tests/apiServer.js b/tests/apiServer.js
index 6470b62..50e0e17 100644
--- a/tests/apiServer.js
+++ b/tests/apiServer.js
@@ -55,7 +55,7 @@
  * Starts a server on passed port or a random port if none passed.
  * The callback will get the URL of the started server.
  */
-var startServer = function(opts, retrying, cb) {
+var startServer = function(opts, cb) {
        // Don't create callback chains when invoked recursively
        if (!cb || !cb.promise) { cb = JSUtils.mkPromised(cb); }
 
@@ -66,17 +66,10 @@
        var forkedServer = { opts: opts };
        var port = opts.port;
 
-       // For now, we always assume that retries are due to port conflicts
-       if (!port) {
-               // XXX we should use a more reliable way to find an open port
-               // (for this, and also for debugPort, below)
-               port = opts.portBase + Math.floor(Math.random() * 100);
-       }
-
-       var url = 'http://' + opts.iface + ':' + port.toString() + opts.urlPath;
-       if (opts.port && forkedServers.has(url)) {
-               // We already have a server there!
-               return cb("There's already a server running at that port.");
+       if (port === undefined) {
+               // Let the OS choose a random open port.  We'll forward it up 
the chain
+               // with the startup message.
+               port = 0;
        }
 
        // Handle debug port (borrowed from 'createWorkerProcess' in node's
@@ -96,7 +89,7 @@
        });
 
        if (!opts.quiet) {
-               console.log("Starting %s server at %s", opts.serverName, url);
+               console.log("Starting %s server.", opts.serverName);
        }
 
        forkedServer.child = childProcess.fork(
@@ -113,24 +106,29 @@
                }
        );
 
-       forkedServers.set(url, forkedServer);
+       var url;
 
-       // If it dies on its own, restart it. The most common cause will be 
that the
-       // port was already in use, so if no port was specified then a new 
random
-       // one will be selected.
+       // If it dies on its own, restart it.
        forkedServer.child.on('exit', function() {
                if (exiting) {
                        return;
                }
-               console.warn('Restarting server at', url);
-               forkedServers.delete(url);
-               startServer(opts, true, cb);
+               if (url) {
+                       console.warn('Restarting server at: ', url);
+                       forkedServers.delete(url);
+               }
+               startServer(opts, cb);
        });
 
        forkedServer.child.on('message', function(m) {
-               if (m && m.type && m.type === 'startup' && cb) {
-                       cb(null, { url: url, child: forkedServer.child });
-                       cb = null; // prevent invoking cb again on restart
+               if (m && m.type === 'startup') {
+                       url = 'http://' + opts.iface + ':' + m.port.toString() 
+ opts.urlPath;
+                       opts.port = m.port;
+                       forkedServers.set(url, forkedServer);
+                       if (typeof cb === 'function') {
+                               cb(null, { url: url, child: forkedServer.child 
});
+                               cb = null; // prevent invoking cb again on 
restart
+                       }
                }
        });
 
diff --git a/tests/mocha/api.js b/tests/mocha/api.js
index 4ee5d6b..bac2ee4 100644
--- a/tests/mocha/api.js
+++ b/tests/mocha/api.js
@@ -18,7 +18,7 @@
                        return apiServer.startParsoidServer({
                                mockUrl: ret.url,
                                serverArgv: [
-                                       '--num-workers', '0',
+                                       '--num-workers', '1',
                                        '--config', path.resolve(__dirname, 
'./apitest.localsettings.js'),
                                ],
                        });
diff --git a/tests/mockAPI.js b/tests/mockAPI.js
index f29714d..27bd2ed 100644
--- a/tests/mockAPI.js
+++ b/tests/mockAPI.js
@@ -422,9 +422,9 @@
 module.exports = app;
 
 var port = process.env.PORT || 7001;
-console.log('Mock MediaWiki API starting.... listening to ' + port);
-app.listen(port, function() {
-       console.log('Started.');
+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 }); }
 });

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I8fba8f83a78ba97527dc3908591c24d0334d178c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/services/parsoid
Gerrit-Branch: master
Gerrit-Owner: Arlolra <[email protected]>

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

Reply via email to