Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(notifier): fix cache usage #355

Merged
merged 5 commits into from
Feb 5, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions lib/utils/time.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@
/**
* Time intervals in ms
*/
export default class Time {
export default class TimeMs {
/** The smallest time interval */
public static MILLISECOND = 1;

public static SECOND = Time.MILLISECOND * 1000;
public static SECOND = TimeMs.MILLISECOND * 1000;

public static MINUTE = Time.SECOND * 60;
public static MINUTE = TimeMs.SECOND * 60;

public static HOUR = Time.MINUTE * 60;
public static HOUR = TimeMs.MINUTE * 60;

public static DAY = Time.HOUR * 24;
public static DAY = TimeMs.HOUR * 24;

public static WEEK = Time.DAY * 7;
public static WEEK = TimeMs.DAY * 7;
}
10 changes: 7 additions & 3 deletions workers/grouper/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { MS_IN_SEC } from '../../../lib/utils/consts';
import DataFilter from './data-filter';
import RedisHelper from './redisHelper';
import levenshtein from 'js-levenshtein';
import TimeMs from '../../../lib/utils/time';

/**
* Error code of MongoDB key duplication error
Expand Down Expand Up @@ -246,8 +247,6 @@ export default class GrouperWorker extends Worker {
* @param count - how many events to return
*/
private findLastEvents(projectId: string, count): Promise<GroupedEventDBScheme[]> {
const msInOneMinute = 60000;

return this.cache.get(`last:${count}:eventsOf:${projectId}`, async () => {
return this.db.getConnection()
.collection(`events:${projectId}`)
Expand All @@ -257,7 +256,12 @@ export default class GrouperWorker extends Worker {
})
.limit(count)
.toArray();
}, msInOneMinute);
},
/**
* TimeMs class stores time intervals in milliseconds, however NodeCache ttl needs to be specified in seconds
*/
/* eslint-disable-next-line @typescript-eslint/no-magic-numbers */
TimeMs.MINUTE / 1000);
}

/**
Expand Down
8 changes: 6 additions & 2 deletions workers/notifier/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { NotifierEvent, NotifierWorkerTask } from '../types/notifier-task';
import { Rule, WhatToReceive } from '../types/rule';
import { SenderWorkerTask } from 'hawk-worker-sender/types/sender-task';
import RuleValidator from './validator';
import Time from '../../../lib/utils/time';
import TimeMs from '../../../lib/utils/time';
import RedisHelper from './redisHelper';

/**
Expand Down Expand Up @@ -113,7 +113,11 @@ export default class NotifierWorker extends Worker {
() => {
return this.getProjectNotificationRules(projectId);
},
Time.MINUTE
/**
* TimeMs class stores time intervals in milliseconds, however NodeCache ttl needs to be specified in seconds
*/
/* eslint-disable-next-line @typescript-eslint/no-magic-numbers */
TimeMs.MINUTE / 1000
);
} catch (e) {
this.logger.warn('Failed to get project notification rules because ', e);
Expand Down
6 changes: 3 additions & 3 deletions workers/sender/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import { DatabaseController } from '../../../lib/db/controller';
import { Worker } from '../../../lib/worker';
import * as pkg from '../package.json';
import './env';
import Time from '../../../lib/utils/time';
import TimeMs from '../../../lib/utils/time';

import { PasswordResetNotification, PaymentSuccessNotification, TemplateEventData, WorkspaceInviteNotification } from '../types/template-variables/';
import NotificationsProvider from './provider';
Expand Down Expand Up @@ -307,7 +307,7 @@ export default abstract class SenderWorker extends Worker {
/**
* Send message not often than once per day
*/
const throttleInterval = Time.DAY;
const throttleInterval = TimeMs.DAY;

const { workspaceId, daysLeft } = task.payload;

Expand Down Expand Up @@ -375,7 +375,7 @@ export default abstract class SenderWorker extends Worker {
/**
* Send message not often than once per day
*/
const throttleInterval = Time.DAY;
const throttleInterval = TimeMs.DAY;

const { workspaceId, eventsCount, eventsLimit } = task.payload;

Expand Down
Loading