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

Server: Fixes #9931: Add task to delete events older than a week #9988

Closed
wants to merge 4 commits into from
Closed
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
21 changes: 19 additions & 2 deletions 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 { afterAllTests, beforeAllDb, beforeEachDb, models } from '../utils/testing/testUtils';
import { msleep, Week } from '../utils/time';

describe('EventModel', () => {

Expand Down Expand Up @@ -37,4 +37,21 @@ 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;
for (const difference of [-10, -5, 0, 5, 10]) {
await models().event().create(EventType.TaskStarted, 'deleteExpiredTokens', aWeekAgo + difference);
}

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

await models().event().deleteOldEvents(aWeekAgo);
const remainingEvents = (await models().event().all());
expect(remainingEvents.length).toBe(3);
for (const event of remainingEvents) {
expect(event.created_time).toBeGreaterThanOrEqual(aWeekAgo);
}
});
});
12 changes: 9 additions & 3 deletions packages/server/src/models/EventModel.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Event, EventType } from '../services/database/types';
import BaseModel, { UuidType } from './BaseModel';


export default class EventModel extends BaseModel<Event> {

public get tableName(): string {
Expand All @@ -16,11 +15,11 @@ export default class EventModel extends BaseModel<Event> {
return UuidType.Native;
}

public async create(type: EventType, name = '') {
public async create(type: EventType, name = '', created_time = Date.now()) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not necessary and should be avoided. Instead you can use this in the test to simulate time:

jest
.useFakeTimers()
.setSystemTime(new Date('2020-01-01'));

See here for more information: https://stackoverflow.com/a/63377110/561309

await this.save({
name,
type,
created_time: Date.now(),
created_time,
});
}

Expand All @@ -33,4 +32,11 @@ export default class EventModel extends BaseModel<Event> {
.first();
}

public async deleteOldEvents(before: number) {
return this.withTransaction(async () => {
await this.db(this.tableName)
.where('created_time', '<', before)
.delete();
}, 'EventModel::deleteOldEvents');
}
}
1 change: 1 addition & 0 deletions packages/server/src/services/TaskService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export const taskIdToLabel = (taskId: TaskId): string => {
[TaskId.ProcessOrphanedItems]: 'Process orphaned items',
[TaskId.ProcessShares]: 'Process shared items',
[TaskId.ProcessEmails]: 'Process emails',
[TaskId.DeleteOldEvents]: 'Delete old events',
};

const s = strings[taskId];
Expand Down
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 @@ -135,6 +135,7 @@ export enum TaskId {
ProcessOrphanedItems,
ProcessShares,
ProcessEmails,
DeleteOldEvents,
}

// AUTO-GENERATED-TYPES
Expand Down
8 changes: 8 additions & 0 deletions packages/server/src/utils/setupTaskService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { TaskId } from '../services/database/types';
import TaskService, { Task, taskIdToLabel } from '../services/TaskService';
import { Services } from '../services/types';
import { Config, Env } from './types';
import { Week } from './time';

export default async function(env: Env, models: Models, config: Config, services: Services): Promise<TaskService> {
const taskService = new TaskService(env, models, config, services);
Expand Down Expand Up @@ -74,6 +75,13 @@ export default async function(env: Env, models: Models, config: Config, services
schedule: '* * * * *',
run: (_models: Models, services: Services) => services.email.runMaintenance(),
},

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

if (config.USER_DATA_AUTO_DELETE_ENABLED) {
Expand Down
Loading