0
Fork 0
mirror of https://github.com/TryGhost/Ghost.git synced 2025-02-24 23:48:13 -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 // 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 // where it left off on next run
function cancel() { function cancel() {
parentPort.postMessage('Email analytics fetch-latest job cancelled before completion');
if (parentPort) { if (parentPort) {
parentPort.postMessage('Email analytics fetch-latest job cancelled before completion');
parentPort.postMessage('cancelled'); parentPort.postMessage('cancelled');
} else { } else {
setTimeout(() => { setTimeout(() => {
@ -31,14 +30,20 @@ if (parentPort) {
const logging = { const logging = {
info(message) { info(message) {
if (parentPort) {
parentPort.postMessage(message); parentPort.postMessage(message);
}
}, },
warn(message) { warn(message) {
if (parentPort) {
parentPort.postMessage(message); parentPort.postMessage(message);
}
}, },
error(message) { error(message) {
if (parentPort) {
parentPort.postMessage(message); parentPort.postMessage(message);
} }
}
}; };
const settingsRows = await db.knex('settings') const settingsRows = await db.knex('settings')
@ -84,9 +89,8 @@ if (parentPort) {
const aggregateEndDate = new Date(); const aggregateEndDate = new Date();
debug(`Finished aggregating email analytics in ${aggregateEndDate - aggregateStartDate}ms`); 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) { if (parentPort) {
parentPort.postMessage(`Fetched ${eventStats.totalEvents} events and aggregated stats for ${eventStats.emailIds.length} emails in ${aggregateEndDate - fetchStartDate}ms`);
parentPort.postMessage('done'); parentPort.postMessage('done');
} else { } else {
// give the logging pipes time finish writing before exit // give the logging pipes time finish writing before exit

View file

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