Skip to content

Commit d473200

Browse files
fix(grouper): delete cache if event saved to db (#383) (#384)
* fix(grouper): delete cache if event saved to db * imp(grouper): move event cache creation to the separate method * chore(grouper): add comments Co-authored-by: e11sy <[email protected]>
1 parent b7fb8f6 commit d473200

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

workers/grouper/src/index.ts

Lines changed: 24 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,7 @@ export default class GrouperWorker extends Worker {
8282
/**
8383
* Find event by group hash.
8484
*/
85-
let existedEvent = await this.getEvent(task.projectId, {
86-
groupHash: uniqueEventHash,
87-
});
85+
let existedEvent = await this.getEvent(task.projectId, uniqueEventHash);
8886

8987
/**
9088
* If we couldn't group by group hash (title), try grouping by Levenshtein distance with last N events
@@ -131,6 +129,13 @@ export default class GrouperWorker extends Worker {
131129
usersAffected: incrementAffectedUsers ? 1 : 0,
132130
} as GroupedEventDBScheme);
133131

132+
const eventCacheKey = await this.getEventCacheKey(task.projectId, uniqueEventHash);
133+
134+
/**
135+
* If event is saved, then cached event state is no longer actual, so we should remove it
136+
*/
137+
this.cache.del(eventCacheKey);
138+
134139
/**
135140
* Increment daily affected users for the first event
136141
*/
@@ -372,25 +377,37 @@ export default class GrouperWorker extends Worker {
372377
* Returns finds event by query from project with passed ID
373378
*
374379
* @param projectId - project's identifier
375-
* @param query - mongo query string
380+
* @param groupHash - group hash of the event
376381
*/
377-
private async getEvent(projectId: string, query: Record<string, unknown>): Promise<GroupedEventDBScheme> {
382+
private async getEvent(projectId: string, groupHash: string): Promise<GroupedEventDBScheme> {
378383
if (!mongodb.ObjectID.isValid(projectId)) {
379384
throw new ValidationError('Controller.saveEvent: Project ID is invalid or missed');
380385
}
381386

382-
const eventCacheKey = `${projectId}:${JSON.stringify(query)}`;
387+
const eventCacheKey = await this.getEventCacheKey(projectId, groupHash);
383388

384389
return this.cache.get(eventCacheKey, async () => {
385390
return this.db.getConnection()
386391
.collection(`events:${projectId}`)
387-
.findOne(query)
392+
.findOne({
393+
groupHash,
394+
})
388395
.catch((err) => {
389396
throw new DatabaseReadWriteError(err);
390397
});
391398
});
392399
}
393400

401+
/**
402+
* Method that returns event cache key based on projectId and groupHash
403+
* @param projectId - used for cache key creation
404+
* @param groupHash - used for cache key creation
405+
* @returns cache key
406+
*/
407+
private async getEventCacheKey(projectId: string, groupHash: string): Promise<string> {
408+
return `${projectId}:${JSON.stringify({groupHash: groupHash})}`
409+
}
410+
394411
/**
395412
* Save event to database
396413
*

workers/grouper/tests/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ jest.mock('../../../lib/cache/controller', () => {
2424

2525
// eslint-disable-next-line @typescript-eslint/no-empty-function, jsdoc/require-jsdoc
2626
public set(): void { }
27+
28+
// eslint-disable-next-line @typescript-eslint/no-empty-function, jsdoc/require-jsdoc
29+
public del(): void { }
2730
};
2831
});
2932

0 commit comments

Comments
 (0)