From e073b273b3e95ee90b3555edc2e3757f2901d7d0 Mon Sep 17 00:00:00 2001 From: Vasil Rangelov Date: Wed, 17 May 2017 18:20:36 +0300 Subject: [PATCH] Fixed standalone server closing, and made the index.js promise resolve only after the standalone server has closed. --- index.js | 14 ++++++++------ lib/servers.js | 24 ++++++++++++++++++++---- 2 files changed, 28 insertions(+), 10 deletions(-) diff --git a/index.js b/index.js index 933aafa..408bc87 100644 --- a/index.js +++ b/index.js @@ -84,13 +84,13 @@ module.exports.run = function (args) { if (servers) { if (args.tlsSni01Port) { - servers = servers.startServers( + servers.startServers( [], args.tlsSni01Port , { debug: args.debug, httpsOptions: le.httpsOptions } ); } else { - servers = servers.startServers( + servers.startServers( args.http01Port || [80], [] , { debug: args.debug } ); @@ -117,10 +117,6 @@ module.exports.run = function (args) { console.log("Renewing them now"); return certs._renewing; }).then(function (certs) { - if (servers) { - servers.closeServers(); - } - console.log(""); console.log("Got certificate(s) for " + certs.altnames.join(', ')); console.log("\tIssued at " + new Date(certs.issuedAt).toISOString() + ""); @@ -147,6 +143,12 @@ module.exports.run = function (args) { ); console.log(""); + if (servers) { + return servers.closeServers({ debug: args.debug }).then(function() { + return 0; + }); + } + return 0; }, function (err) { console.error('[Error]: greenlock-cli'); diff --git a/lib/servers.js b/lib/servers.js index fcae91f..9bb5c51 100644 --- a/lib/servers.js +++ b/lib/servers.js @@ -77,11 +77,27 @@ module.exports.create = function (challenge) { } - , closeServers: function () { - servers._servers.forEach(function (server) { - server.close(); + , closeServers: function (opts) { + opts = opts || {}; + return new Promise(function (done) { + var closedServers = 0; + var serversToClose = servers._servers.length; + if (0 === serversToClose) { + done(); + } + + servers._servers.forEach(function (server) { + server.close(function () { + if (serversToClose === ++closedServers) { + if (opts.debug) { + console.info('Closed all servers'); + } + servers._servers = []; + done(); + } + }); + }); }); - servers._servers = []; } };