mirror of
https://github.com/Unleash/unleash.git
synced 2024-11-01 19:07:38 +01:00
d60e505a40
This PR fixes an issue where events generated during a db transaction would get published before the transaction was complete. This caused errors in some of our services that expected the data to be stored before the transaction had been commited. Refer to [linear issue 1-1049](https://linear.app/unleash/issue/1-1049/event-emitter-should-emit-events-after-db-transaction-is-commited-not) for more info. Fixes 1-1049. ## Changes The most important change here is that the `eventStore` no longer emits events when they happen (because that can be in the middle of a transaction). Instead, events are stored with a new `announced` column. The new event announcer service runs on a schedule (every second) and publishes any new events that have not been published. Parts of the code have largely been lifted from the `client-application-store`, which uses a similar logic. I have kept the emitting of the event within the event store because a lot of other services listen to events from this store, so removing that would require a large rewrite. It's something we could look into down the line, but it seems like too much of a change to do right now. ## Discussion ### Terminology: Published vs announced? We should settle on one or the other. Announced is consistent with the client-application store, but published sounds more fitting for events. ### Publishing and marking events as published The current implementation fetches all events that haven't been marked as announced, sets them as announced, and then emits them. It's possible that Unleash would crash in the interim or something else might happen, causing the events not to get published. Maybe it would make sense to just fetch the events and only mark them as published after the announcement? On the other hand, that might get us into other problems. Any thoughts on this would be much appreciated.
23 lines
672 B
TypeScript
23 lines
672 B
TypeScript
import { IUnleashConfig } from '../types/option';
|
|
import { IUnleashStores } from '../types/stores';
|
|
import { Logger } from '../logger';
|
|
import { IEventStore } from '../types/stores/event-store';
|
|
|
|
export default class EventAnnouncer {
|
|
private logger: Logger;
|
|
|
|
private eventStore: IEventStore;
|
|
|
|
constructor(
|
|
{ eventStore }: Pick<IUnleashStores, 'eventStore'>,
|
|
{ getLogger }: Pick<IUnleashConfig, 'getLogger'>,
|
|
) {
|
|
this.logger = getLogger('services/event-service.ts');
|
|
this.eventStore = eventStore;
|
|
}
|
|
|
|
async publishUnannouncedEvents(): Promise<void> {
|
|
return this.eventStore.publishUnannouncedEvents();
|
|
}
|
|
}
|