0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-17 23:44:39 -05:00

Added guards against parentPort being null

fixes https://github.com/TryGhost/Team/issues/834

- see referenced issue for context
- there are times when `parentPort` can be null and the job crashes
  because `parentPort.postMessage` won't work
- this commit adds guards around `parentPort`, or moves code inside
  existing guards, to protect against this
This commit is contained in:
Daniel Lockyer 2021-06-29 12:14:48 +01:00
parent 7e9c182dc5
commit 1ff4f6ce7d
No known key found for this signature in database
GPG key ID: D21186F0B47295AD
2 changed files with 32 additions and 21 deletions

View file

@ -6,9 +6,8 @@ const debug = require('@tryghost/debug')('jobs:email-analytics:fetch-latest');
// Exit early when cancelled to prevent stalling shutdown. No cleanup needed when cancelling as everything is idempotent and will pick up
// where it left off on next run
function cancel() {
parentPort.postMessage('Email analytics fetch-latest job cancelled before completion');
if (parentPort) {
parentPort.postMessage('Email analytics fetch-latest job cancelled before completion');
parentPort.postMessage('cancelled');
} else {
setTimeout(() => {
@ -31,13 +30,19 @@ if (parentPort) {
const logging = {
info(message) {
parentPort.postMessage(message);
if (parentPort) {
parentPort.postMessage(message);
}
},
warn(message) {
parentPort.postMessage(message);
if (parentPort) {
parentPort.postMessage(message);
}
},
error(message) {
parentPort.postMessage(message);
if (parentPort) {
parentPort.postMessage(message);
}
}
};
@ -84,9 +89,8 @@ if (parentPort) {
const aggregateEndDate = new Date();
debug(`Finished aggregating email analytics in ${aggregateEndDate - aggregateStartDate}ms`);
parentPort.postMessage(`Fetched ${eventStats.totalEvents} events and aggregated stats for ${eventStats.emailIds.length} emails in ${aggregateEndDate - fetchStartDate}ms`);
if (parentPort) {
parentPort.postMessage(`Fetched ${eventStats.totalEvents} events and aggregated stats for ${eventStats.emailIds.length} emails in ${aggregateEndDate - fetchStartDate}ms`);
parentPort.postMessage('done');
} else {
// give the logging pipes time finish writing before exit

View file

@ -3,13 +3,21 @@ const util = require('util');
let shutdown = false;
parentPort.on('message', (message) => {
parentPort.postMessage(`parent message received: ${message}`);
if (message === 'cancel') {
shutdown = true;
const postParentPortMessage = (message) => {
if (parentPort) {
parentPort.postMessage(message);
}
});
};
if (parentPort) {
parentPort.on('message', (message) => {
parentPort.postMessage(`parent message received: ${message}`);
if (message === 'cancel') {
shutdown = true;
}
});
}
const setTimeoutPromise = util.promisify(setTimeout);
const internalContext = {context: {internal: true}};
@ -19,25 +27,24 @@ const internalContext = {context: {internal: true}};
await models.init();
parentPort.postMessage(`Fetching tags`);
postParentPortMessage(`Fetching tags`);
const tags = await models.Tag.findPage(internalContext);
parentPort.postMessage(`Found ${tags.data.length} tags. First one: ${tags.data[0].toJSON().slug}`);
postParentPortMessage(`Found ${tags.data.length} tags. First one: ${tags.data[0].toJSON().slug}`);
parentPort.postMessage(`Waiting 5 seconds to perform second part of the job`);
postParentPortMessage(`Waiting 5 seconds to perform second part of the job`);
await setTimeoutPromise(5 * 1000);
if (shutdown) {
parentPort.postMessage(`Job shutting down gracefully`);
postParentPortMessage(`Job shutting down gracefully`);
process.exit(0);
}
parentPort.postMessage(`Fetching posts`);
postParentPortMessage(`Fetching posts`);
const posts = await models.Post.findPage(internalContext);
parentPort.postMessage(`Found ${posts.data.length} posts. First one: ${posts.data[0].toJSON().slug}`);
parentPort.postMessage('Graceful job has completed!');
postParentPortMessage(`Found ${posts.data.length} posts. First one: ${posts.data[0].toJSON().slug}`);
postParentPortMessage('Graceful job has completed!');
process.exit(0);
})();