From 693ac8ae7293fdbd0dbfaca47b67fe39a950994d Mon Sep 17 00:00:00 2001 From: Vlad Lasky Date: Thu, 18 May 2017 01:28:46 +1000 Subject: [PATCH] In response to hwillson's feedback, added test to webapp_tests for socketPath and made cosmetic fixes to UNIX socket code to conform better with meteor's coding style. --- packages/webapp/webapp_server.js | 59 +++++++++++--------------------- packages/webapp/webapp_tests.js | 5 ++- 2 files changed, 24 insertions(+), 40 deletions(-) diff --git a/packages/webapp/webapp_server.js b/packages/webapp/webapp_server.js index b5fc50abfe..6e3400eaf6 100644 --- a/packages/webapp/webapp_server.js +++ b/packages/webapp/webapp_server.js @@ -817,65 +817,46 @@ var runWebAppServer = function () { var host = process.env.BIND_IP; var localIp = host || '0.0.0.0'; - if (typeof localPort == "number") - { + var startHttpServer = function(listenOptions) { + httpServer.listen(listenOptions, Meteor.bindEnvironment(function() { + if (process.env.METEOR_PRINT_ON_LISTEN) + console.log("LISTENING"); // must match run-app.js + + var callbacks = onListeningCallbacks; + onListeningCallbacks = null; + _.each(callbacks, function (x) { x(); }); + + }, function (e) { + console.error("Error listening:", e); + console.error(e && e.stack); + })); + }; + + if (typeof localPort == "number") { var listenOptions = { port: localPort, host: host }; - } - else - { + } else { var socketPath = localPort; var listenOptions = { path: socketPath }; httpServer.on('error', Meteor.bindEnvironment(function(e) { - if (e.code == 'EADDRINUSE') { - var clientSocket = new net.Socket(); - clientSocket.on('error', Meteor.bindEnvironment(function(e) { - if (e.code == 'ECONNREFUSED') { - console.log("Deleting stale socket file"); fs.unlinkSync(socketPath); - - httpServer.listen(listenOptions, Meteor.bindEnvironment(function() { - if (process.env.METEOR_PRINT_ON_LISTEN) - console.log("LISTENING"); // must match run-app.js - - var callbacks = onListeningCallbacks; - onListeningCallbacks = null; - _.each(callbacks, function (x) { x(); }); - - }, function (e) { - console.error("Error listening:", e); - console.error(e && e.stack); - })); - + startHttpServer(listenOptions); } })); - clientSocket.connect({ path: socketPath }, function() { console.log("Another server is already listening on socket: " + socketPath + ", exiting."); process.exit(); - }); - + }); } })); } - httpServer.listen(listenOptions, Meteor.bindEnvironment(function() { - if (process.env.METEOR_PRINT_ON_LISTEN) - console.log("LISTENING"); // must match run-app.js - - var callbacks = onListeningCallbacks; - onListeningCallbacks = null; - _.each(callbacks, function (x) { x(); }); - - }, function (e) { - console.error("Error listening:", e); - console.error(e && e.stack); - })); + startHttpServer(listenOptions); return 'DAEMON'; }; diff --git a/packages/webapp/webapp_tests.js b/packages/webapp/webapp_tests.js index 2600be7204..663fb3a2c0 100644 --- a/packages/webapp/webapp_tests.js +++ b/packages/webapp/webapp_tests.js @@ -157,15 +157,18 @@ Tinytest.add("webapp - generating boilerplate should not change runtime config", }); // Support 'named pipes' (strings) as ports for support of Windows Server / Azure deployments -Tinytest.add("webapp - port should be parsed as int unless it is a named pipe", function(test){ +Tinytest.add("webapp - port should be parsed as int unless it is a named pipe or path/filename of a Unix domain socket", function(test){ // Named pipes on Windows Server follow the format: \\.\pipe\{randomstring} or \\{servername}\pipe\{randomstring} var namedPipe = "\\\\.\\pipe\\b27429e9-61e3-4c12-8bfe-950fa3295f74"; var namedPipeServer = "\\\\SERVERNAME-1234\\pipe\\6e157e98-faef-49e4-a0cf-241037223308"; + var socketPath = "/var/run/meteor.sock"; test.equal(WebAppInternals.parsePort(namedPipe), "\\\\.\\pipe\\b27429e9-61e3-4c12-8bfe-950fa3295f74"); test.equal(WebAppInternals.parsePort(namedPipeServer), "\\\\SERVERNAME-1234\\pipe\\6e157e98-faef-49e4-a0cf-241037223308"); + test.equal(WebAppInternals.parsePort(socketPath), + "/var/run/meteor.sock"); test.equal(WebAppInternals.parsePort(8080), 8080); test.equal(WebAppInternals.parsePort("8080"),