Skip to content

Commit

Permalink
feat: add periodic keypackages check and logging [WPB-14727] (#6746)
Browse files Browse the repository at this point in the history
* feat: add periodic keypackages check and logging

* feat: refactor code to have a single place in the future to make changes to the event

* fix: export ts interface

* fix: test
  • Loading branch information
aweiss-dev authored Dec 9, 2024
1 parent 7380c0d commit 38fc333
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -754,10 +754,17 @@ export class MLSService extends TypedEventEmitter<Events> {
* @param clientId id of the client
*/
public schedulePeriodicKeyPackagesBackendSync(clientId: string) {
const task = async () => {
this.logger.log('Executed Periodic check for key packages');
await this.verifyRemoteMLSKeyPackagesAmount(clientId);
};

// Schedule the task to run every day
return this.recurringTaskScheduler.registerTask({
every: TimeUtil.TimeInMillis.DAY,
key: 'try-key-packages-backend-sync',
task: () => this.verifyRemoteMLSKeyPackagesAmount(clientId),
task,
addTaskOnWindowFocusEvent: true,
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,22 @@ interface RecurringTaskSchedulerStorage {
delete: (key: string) => Promise<void>;
}

interface TaskParams {
export interface TaskParams {
every: number;
task: () => Promise<unknown> | unknown;
key: string;
addTaskOnWindowFocusEvent?: boolean;
}

export class RecurringTaskScheduler {
constructor(private readonly storage: RecurringTaskSchedulerStorage) {}

public readonly registerTask = async ({every, task, key}: TaskParams): Promise<void> => {
public readonly registerTask = async ({
every,
task,
key,
addTaskOnWindowFocusEvent = false,
}: TaskParams): Promise<void> => {
const firingDate = (await this.storage.get(key)) || Date.now() + every;
await this.storage.set(key, firingDate);

Expand All @@ -61,6 +67,12 @@ export class RecurringTaskScheduler {
} else {
TaskScheduler.addTask(taskConfig);
}

// If the task should be added on window focus event, we add it here

if (addTaskOnWindowFocusEvent && typeof window !== 'undefined') {
window.addEventListener('focus', taskConfig.task);
}
};

public readonly cancelTask = async (taskKey: string): Promise<void> => {
Expand Down

0 comments on commit 38fc333

Please sign in to comment.