Removed models require from analytics job (#12689)

refs https://github.com/TryGhost/Ghost/issues/12496

By requiring the models layer the shared logging util was being required as a side-effect causing the open file descriptors problem to continue. Removing logging from the models layer isn't feasible due to deep require chains spreading across the codebase, it's much quicker to remove the need for models in the analytics job.

- models layer was only needed because it's used by the session service
- updated analytics job to create it's own instance of `EmailAnalyticsService` rather than the default instance in order to pass in custom dependencies
- pass in custom `logging` object that uses `parentPort.postMessage` as a way of writing log output
- pass in custom `settings` object that returns settings that have been manually fetched and cached during job instantiation
This commit is contained in:
Kevin Ansfield 2021-02-22 12:10:19 +00:00 committed by GitHub
parent f106de99f6
commit 42e452b127
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -26,15 +26,44 @@ if (parentPort) {
}
(async () => {
const models = require('../../../models');
const settingsService = require('../../settings');
const config = require('../../../../shared/config');
const db = require('../../../data/db');
// 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 logging = {
info(message) {
parentPort.postMessage(message);
},
warn(message) {
parentPort.postMessage(message);
},
error(message) {
parentPort.postMessage(message);
}
};
const emailAnalyticsService = require('../');
const settingsRows = await db.knex('settings')
.whereIn('key', ['mailgun_api_key', 'mailgun_domain', 'mailgun_base_url']);
const settingsCache = {};
settingsRows.forEach((row) => {
settingsCache[row.key] = row.value;
});
const settings = {
get(key) {
return settingsCache[key];
}
};
const EmailAnalyticsService = require('../email-analytics');
const emailAnalyticsService = new EmailAnalyticsService({
config,
db,
settings,
logging
});
const fetchStartDate = new Date();
debug('Starting email analytics fetch of latest events');