mirror of
https://github.com/TryGhost/Ghost.git
synced 2024-12-21 09:52:06 +03:00
a29ac2691a
no issue - the 4.2.0 version of `sqlite3` that we're using is not compatible with `worker_threads` - 5.0.0 should add support this but there are other errors - 5.0.1 is released but not published (https://github.com/mapbox/node-sqlite3/issues/1386)
72 lines
2.5 KiB
JavaScript
72 lines
2.5 KiB
JavaScript
const logging = require('../../../../shared/logging');
|
|
const {parentPort} = require('bthreads');
|
|
const debug = require('ghost-ignition').debug('jobs:email-analytics:fetch-latest');
|
|
|
|
// recurring job to fetch analytics since the most recently seen event timestamp
|
|
|
|
// 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() {
|
|
logging.info('Email analytics fetch-latest job cancelled before completion');
|
|
|
|
if (parentPort) {
|
|
parentPort.postMessage('cancelled');
|
|
} else {
|
|
setTimeout(() => {
|
|
process.exit(0);
|
|
}, 1000);
|
|
}
|
|
}
|
|
|
|
if (parentPort) {
|
|
parentPort.once('message', (message) => {
|
|
if (message === 'cancel') {
|
|
return cancel();
|
|
}
|
|
});
|
|
}
|
|
|
|
(async () => {
|
|
try {
|
|
const models = require('../../../models');
|
|
const settingsService = require('../../settings');
|
|
|
|
// must be initialized before emailAnalyticsService is required otherwise
|
|
// requires are in the wrong order and settingsCache will always be empty
|
|
await models.init();
|
|
await settingsService.init();
|
|
|
|
const emailAnalyticsService = require('../');
|
|
|
|
const fetchStartDate = new Date();
|
|
debug('Starting email analytics fetch of latest events');
|
|
const eventStats = await emailAnalyticsService.fetchLatest();
|
|
const fetchEndDate = new Date();
|
|
debug(`Finished fetching ${eventStats.totalEvents} analytics events in ${fetchEndDate - fetchStartDate}ms`);
|
|
|
|
const aggregateStartDate = new Date();
|
|
debug(`Starting email analytics aggregation for ${eventStats.emailIds.length} emails`);
|
|
await emailAnalyticsService.aggregateStats(eventStats);
|
|
const aggregateEndDate = new Date();
|
|
debug(`Finished aggregating email analytics in ${aggregateEndDate - aggregateStartDate}ms`);
|
|
|
|
logging.info(`Fetched ${eventStats.totalEvents} events and aggregated stats for ${eventStats.emailIds.length} emails in ${aggregateEndDate - fetchStartDate}ms`);
|
|
|
|
if (parentPort) {
|
|
parentPort.postMessage('done');
|
|
} else {
|
|
// give the logging pipes time finish writing before exit
|
|
setTimeout(() => {
|
|
process.exit(0);
|
|
}, 1000);
|
|
}
|
|
} catch (error) {
|
|
logging.error(error);
|
|
|
|
// give the logging pipes time finish writing before exit
|
|
setTimeout(() => {
|
|
process.exit(1);
|
|
}, 1000);
|
|
}
|
|
})();
|