diff --git a/core/server/bootstrap-socket.js b/core/server/bootstrap-socket.js deleted file mode 100644 index 66fc1cc5cd..0000000000 --- a/core/server/bootstrap-socket.js +++ /dev/null @@ -1,85 +0,0 @@ -module.exports = (socketAddress, logging, message) => { - // Very basic guard against bad calls - if (!socketAddress || !socketAddress.host || !socketAddress.port || !logging || !logging.info || !logging.warn || !message) { - return Promise.resolve(); - } - - const net = require('net'); - const client = new net.Socket(); - - return new Promise((resolve) => { - const connect = (options = {}) => { - let wasResolved = false; - - const waitTimeout = setTimeout(() => { - logging.info('Bootstrap socket timed out.'); - - if (!client.destroyed) { - client.destroy(); - } - - if (wasResolved) { - return; - } - - wasResolved = true; - resolve(); - }, 1000 * 5); - - client.connect(socketAddress.port, socketAddress.host, () => { - if (waitTimeout) { - clearTimeout(waitTimeout); - } - - client.write(JSON.stringify(message)); - - if (wasResolved) { - return; - } - - wasResolved = true; - resolve(); - }); - - client.on('close', () => { - logging.info('Bootstrap client was closed.'); - - if (waitTimeout) { - clearTimeout(waitTimeout); - } - }); - - client.on('error', (err) => { - logging.warn(`Can't connect to the bootstrap socket (${socketAddress.host} ${socketAddress.port}) ${err.code}.`); - - client.removeAllListeners(); - - if (waitTimeout) { - clearTimeout(waitTimeout); - } - - if (options.tries < 3) { - logging.warn(`Tries: ${options.tries}`); - - // retry - logging.warn('Retrying...'); - - options.tries = options.tries + 1; - const retryTimeout = setTimeout(() => { - clearTimeout(retryTimeout); - connect(options); - }, 150); - } else { - if (wasResolved) { - return; - } - - wasResolved = true; - resolve(); - } - }); - }; - - connect({tries: 0}); - }); -}; diff --git a/core/server/ghost-server.js b/core/server/ghost-server.js index 4825b225ec..8e22003f7c 100644 --- a/core/server/ghost-server.js +++ b/core/server/ghost-server.js @@ -12,7 +12,7 @@ const errors = require('@tryghost/errors'); const {events, i18n} = require('./lib/common'); const logging = require('../shared/logging'); const moment = require('moment'); -const connectToBootstrapSocket = require('./bootstrap-socket'); +const connectToBootstrapSocket = require('@tryghost/bootstrap-socket'); /** * ## GhostServer diff --git a/package.json b/package.json index 06c63ee1f3..143d0fe04f 100644 --- a/package.json +++ b/package.json @@ -43,6 +43,7 @@ "@nexes/nql": "0.4.0", "@sentry/node": "5.20.1", "@tryghost/adapter-manager": "0.1.8", + "@tryghost/bootstrap-socket": "0.1.0", "@tryghost/errors": "0.2.0", "@tryghost/helpers": "1.1.28", "@tryghost/image-transform": "1.0.0", diff --git a/yarn.lock b/yarn.lock index 60ee3dd1d1..1e1d4efb54 100644 --- a/yarn.lock +++ b/yarn.lock @@ -367,6 +367,11 @@ dependencies: "@tryghost/errors" "^0.2.1" +"@tryghost/bootstrap-socket@0.1.0": + version "0.1.0" + resolved "https://registry.yarnpkg.com/@tryghost/bootstrap-socket/-/bootstrap-socket-0.1.0.tgz#bd92bb4fe084a9f3b1c6801d23631710f4534d4e" + integrity sha512-/fibvaj34co1uxMONJIIHYLqLamE0ClN57Z1YiBd10YlKDavxpwKoX0Pg7+Trk/UKfc7Owq2O+ZrCs0M/I1A8A== + "@tryghost/errors@0.2.0", "@tryghost/errors@^0.2.0": version "0.2.0" resolved "https://registry.yarnpkg.com/@tryghost/errors/-/errors-0.2.0.tgz#850fe93db8aceadfe1f9e21b8c3a42599de17808"