-
-
Notifications
You must be signed in to change notification settings - Fork 227
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): Ensure that Ghost was started
closes #472 - added port polling utility - general process manager class offers `ensureStarted` function - systemd extension makes use of `ensureStarted`
- Loading branch information
Showing
7 changed files
with
326 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
'use strict'; | ||
|
||
const net = require('net'); | ||
const merge = require('lodash/merge'); | ||
const errors = require('../errors'); | ||
|
||
module.exports = function portPolling(options) { | ||
options = merge({ | ||
timeoutInMS: 1000, | ||
maxTries: 20, | ||
delayOnConnectInMS: 3 * 1000, | ||
logSuggestion: 'ghost log', | ||
socketTimeoutInMS: 1000 * 30 | ||
}, options || {}); | ||
|
||
if (!options.port) { | ||
return Promise.reject(new errors.CliError({ | ||
message: 'Port is required.' | ||
})); | ||
} | ||
|
||
const connectToGhostSocket = (() => { | ||
return new Promise((resolve, reject) => { | ||
const ghostSocket = net.connect(options.port); | ||
|
||
// inactivity timeout | ||
ghostSocket.setTimeout(options.socketTimeoutInMS); | ||
ghostSocket.on('timeout', (() => { | ||
ghostSocket.destroy(); | ||
|
||
// force retry | ||
const err = new Error(); | ||
err.retry = true; | ||
reject(err); | ||
})); | ||
|
||
ghostSocket.on('connect', (() => { | ||
if (options.delayOnConnectInMS) { | ||
let ghostDied = false; | ||
|
||
// CASE: client closes socket | ||
ghostSocket.on('close', (() => { | ||
ghostDied = true; | ||
})); | ||
|
||
setTimeout(() => { | ||
ghostSocket.destroy(); | ||
|
||
if (ghostDied) { | ||
reject(new Error('Ghost died.')); | ||
} else { | ||
resolve(); | ||
} | ||
}, options.delayOnConnectInMS); | ||
|
||
return; | ||
} | ||
|
||
ghostSocket.destroy(); | ||
resolve(); | ||
})); | ||
|
||
ghostSocket.on('error', ((err) => { | ||
ghostSocket.destroy(); | ||
|
||
err.retry = true; | ||
reject(err); | ||
})); | ||
}); | ||
}); | ||
|
||
const startPolling = (() => { | ||
return new Promise((resolve, reject) => { | ||
let tries = 0; | ||
|
||
(function retry() { | ||
connectToGhostSocket() | ||
.then(() => { | ||
resolve(); | ||
}) | ||
.catch((err) => { | ||
if (err.retry && tries < options.maxTries) { | ||
tries = tries + 1; | ||
setTimeout(retry, options.timeoutInMS); | ||
return; | ||
} | ||
|
||
reject(new errors.GhostError({ | ||
message: 'Ghost did not start.', | ||
suggestion: options.logSuggestion, | ||
err: err | ||
})); | ||
}); | ||
}()); | ||
}); | ||
}); | ||
|
||
return startPolling(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.