mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-01-06 22:40:14 -05:00
Moved bootstap-socket code to @tryghost/bootstrap-socket
- this code has zero deps and is much easier to reason about standalone
This commit is contained in:
parent
d4843b4ea8
commit
b765a30ee9
4 changed files with 7 additions and 86 deletions
85
core/server/bootstrap-socket.js
vendored
85
core/server/bootstrap-socket.js
vendored
|
@ -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});
|
||||
});
|
||||
};
|
|
@ -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
|
||||
|
|
|
@ -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",
|
||||
|
|
|
@ -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"
|
||||
|
|
Loading…
Reference in a new issue