Skip to content

Commit

Permalink
Server: Resolves #9931: Add task to delete events older than a week
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrienPoupa committed Nov 11, 2024
1 parent 3887010 commit 0ada87b
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
28 changes: 27 additions & 1 deletion packages/server/src/models/EventModel.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EventType } from '../services/database/types';
import { beforeAllDb, afterAllTests, beforeEachDb, models } from '../utils/testing/testUtils';
import { msleep } from '../utils/time';
import { msleep, Week } from '../utils/time';

describe('EventModel', () => {

Expand Down Expand Up @@ -37,4 +37,30 @@ describe('EventModel', () => {
expect(latest.id).toBe(allEvents[1].id);
});

test('should delete events older than a week', async () => {
const now = Date.now();
const aWeekAgo = now - Week;
jest.useFakeTimers();

for (const difference of [-10, -5, 0, 5, 10]) {
jest.setSystemTime(aWeekAgo + difference);
await models().event().create(EventType.TaskStarted, 'deleteExpiredTokens');
}

const allEvents = (await models().event().all());
expect(allEvents.length).toBe(5);

jest.setSystemTime(now);
await models().event().deleteOldEvents();

const remainingEvents = (await models().event().all());
expect(remainingEvents.length).toBe(3);

for (const event of remainingEvents) {
expect(event.created_time).toBeGreaterThanOrEqual(aWeekAgo);
}

jest.useRealTimers();
});

});
11 changes: 11 additions & 0 deletions packages/server/src/models/EventModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import BaseModel, { UuidType } from './BaseModel';

export default class EventModel extends BaseModel<Event> {

private eventTtl_: number = 7 * 24 * 60 * 60 * 1000;

public get tableName(): string {
return 'events';
}
Expand Down Expand Up @@ -33,4 +35,13 @@ export default class EventModel extends BaseModel<Event> {
.first();
}

public async deleteOldEvents() {
const cutOffDate = Date.now() - this.eventTtl_;
return this.withTransaction(async () => {
await this.db(this.tableName)
.where('created_time', '<', cutOffDate)
.delete();
}, 'EventModel::deleteOldEvents');
}

}
1 change: 1 addition & 0 deletions packages/server/src/services/database/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ export enum TaskId {
ProcessShares,
ProcessEmails,
LogHeartbeatMessage,
DeleteOldEvents,
}

// AUTO-GENERATED-TYPES
Expand Down
7 changes: 7 additions & 0 deletions packages/server/src/utils/setupTaskService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,13 @@ export default async function(env: Env, models: Models, config: Config, services
schedule: config.HEARTBEAT_MESSAGE_SCHEDULE,
run: (_models: Models, _services: Services) => logHeartbeatMessage(),
},

{
id: TaskId.DeleteOldEvents,
description: taskIdToLabel(TaskId.DeleteOldEvents),
schedule: '0 0 * * *',
run: (models: Models) => models.event().deleteOldEvents(),
},
];

if (config.USER_DATA_AUTO_DELETE_ENABLED) {
Expand Down

0 comments on commit 0ada87b

Please sign in to comment.