Ghost/ghost/domain-events/lib/DomainEvents.js

100 lines
2.6 KiB
JavaScript
Raw Normal View History

const EventEmitter = require('events').EventEmitter;
const logging = require('@tryghost/logging');
/**
* @template T
* @typedef {import('./').ConstructorOf<T>} ConstructorOf<T>
*/
/**
* @template Data
* @typedef {object} IEvent
* @prop {Date} timestamp
* @prop {Data} data
*/
class DomainEvents {
/**
* @private
* @type EventEmitter
*/
static ee = new EventEmitter;
/**
* @template Data
* @template {IEvent<Data>} EventClass
* @param {ConstructorOf<EventClass>} Event
* @param {(event: EventClass) => Promise<void> | void} handler
*
* @returns {void}
*/
static subscribe(Event, handler) {
DomainEvents.ee.on(Event.name, async (event) => {
try {
await handler(event);
} catch (e) {
logging.error('Unhandled error in event handler for event: ' + Event.name);
logging.error(e);
}
if (this.#trackingEnabled) {
this.#onProcessed();
}
});
}
/**
* @template Data
* @param {IEvent<Data>} event
* @returns {void}
*/
static dispatch(event) {
Added new email event processor (#15879) fixes https://github.com/TryGhost/Team/issues/2310 This moves the processing of the events from the event-processor to a new email-event-processor in the email-service package. - The `EmailEventProcessor` only translates events from providerId/emailId to their known emailId, memberId and recipientId, and dispatches the corresponding events. - Since `EmailEventProcessor` runs in a separate worker thread, we can't listen for the dispatched events on the main thread. To accomplish this communication, the events dispatched from the `EmailEventProcessor` class are 'posted' via the postMessage method and redispatched on the main thread. - A new `EmailEventStorage` class reacts to the email events and stores it in the database. This code mostly corresponds to the (now deleted) subclass of the old `EmailEventProcessor` - Updating a members last_seen_at timestamp has moved to the lastSeenAtUpdater. - Email events no longer store `ObjectID` because these are not encodable across threads via postMessage - Includes new E2E tests that test the storage of all supported Mailgun events. Note that in these tests we run the processing on the main thread instead of on a separate thread (couldn't do this because stubbing is not possible across threads) There are some missing pieces that will get added in later PRs (this PR focuses on porting the existing functionality): - Handling temporary failures/bounces - Capturing the error messages of bounce events
2022-11-29 13:15:19 +03:00
DomainEvents.dispatchRaw(event.constructor.name, event);
}
/**
* Dispatch an event in case you don't have an instance of the event class, but you do have the event name and event data.
* @template Data
* @param {string} name
* @param {Data} data
* @returns {void}
*/
static dispatchRaw(name, data) {
if (this.#trackingEnabled) {
this.#dispatchCount += DomainEvents.ee.listenerCount(name);
}
Added new email event processor (#15879) fixes https://github.com/TryGhost/Team/issues/2310 This moves the processing of the events from the event-processor to a new email-event-processor in the email-service package. - The `EmailEventProcessor` only translates events from providerId/emailId to their known emailId, memberId and recipientId, and dispatches the corresponding events. - Since `EmailEventProcessor` runs in a separate worker thread, we can't listen for the dispatched events on the main thread. To accomplish this communication, the events dispatched from the `EmailEventProcessor` class are 'posted' via the postMessage method and redispatched on the main thread. - A new `EmailEventStorage` class reacts to the email events and stores it in the database. This code mostly corresponds to the (now deleted) subclass of the old `EmailEventProcessor` - Updating a members last_seen_at timestamp has moved to the lastSeenAtUpdater. - Email events no longer store `ObjectID` because these are not encodable across threads via postMessage - Includes new E2E tests that test the storage of all supported Mailgun events. Note that in these tests we run the processing on the main thread instead of on a separate thread (couldn't do this because stubbing is not possible across threads) There are some missing pieces that will get added in later PRs (this PR focuses on porting the existing functionality): - Handling temporary failures/bounces - Capturing the error messages of bounce events
2022-11-29 13:15:19 +03:00
DomainEvents.ee.emit(name, data);
}
// Methods and properties below are only for testing purposes
static #awaitQueue = [];
static #dispatchCount = 0;
static #processedCount = 0;
static #trackingEnabled = process.env.NODE_ENV?.startsWith('test');
/**
* Waits for all the events in the queue to be dispatched and fully processed (async).
*/
static allSettled() {
return new Promise((resolve) => {
if (this.#processedCount >= this.#dispatchCount) {
// Resolve immediately if there are no events in the queue
resolve();
return;
}
this.#awaitQueue.push({resolve});
});
}
static #onProcessed() {
this.#processedCount += 1;
if (this.#processedCount >= this.#dispatchCount) {
for (const item of this.#awaitQueue) {
item.resolve();
}
this.#awaitQueue = [];
}
}
}
module.exports = DomainEvents;