Skip to content

Commit b8c0bd6

Browse files
authored
VAP-1921 Admin job to delete old call logs in onprem db (#2053)
1 parent c6cdaed commit b8c0bd6

File tree

4 files changed

+22
-0
lines changed

4 files changed

+22
-0
lines changed

apps/adminWorker/src/main.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
orgStripeSubscriptionCancel,
6565
orgToSubscriptionMigrate,
6666
} from 'functions/orgFunctions';
67+
import { deleteOldCallLogs } from 'dbFunctions/logsDbFunctions';
6768

6869
const worker = new Worker<AdminQueueJobData, any, keyof typeof ADMIN_QUEUE_JOB>(
6970
'admin',
@@ -293,6 +294,9 @@ const worker = new Worker<AdminQueueJobData, any, keyof typeof ADMIN_QUEUE_JOB>(
293294
case 'statusChecksInit': {
294295
return adminCacheKeySet(CALL_USAGE_CHECKED_AT_KEY, String(Date.now()));
295296
}
297+
case 'cleanUpCallLogs': {
298+
return deleteOldCallLogs();
299+
}
296300
default: {
297301
throw new Error(`Unknown Job: ${job.name}`);
298302
}

libs/core/src/dbFunctions/logsDbFunctions.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,3 +234,15 @@ async function timeLogCreate(log: Log) {
234234
}
235235

236236
export const timeLogCreateSafe = withErrorHandling(timeLogCreate);
237+
238+
export async function deleteOldCallLogs() {
239+
if (!process.env.ENCLAVE_ENABLED || !sea.isSea()) {
240+
return;
241+
}
242+
243+
const deleteBeforeDays = process.env.CALL_LOGS_RETENTION_DAYS ?? 7;
244+
245+
await dbForLogs<CallLogPrivileged[]>/*sql*/ `
246+
DELETE FROM call_logs WHERE time < NOW() - INTERVAL '${deleteBeforeDays} DAY'
247+
`;
248+
}

libs/core/src/queues/adminQueue.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,10 @@ if (process.env.NODE_ENV === 'production') {
3434
},
3535
)
3636
.catch(errorWithClock);
37+
38+
adminQueue
39+
// This job will run every 24 hours
40+
.add('cleanUpCallLogs', {}, { repeat: { every: 24 * 60 * 60 * 1000 } })
41+
.catch(errorWithClock);
3742
}
3843
}

libs/core/src/types/queue.types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,7 @@ export const ADMIN_QUEUE_JOB = {
255255
callPhoneNumberCallStatusUpdate: 'callPhoneNumberCallStatusUpdate',
256256
callTwilioStatusUpdate: 'callTwilioStatusUpdate',
257257
callVonageStatusUpdate: 'callVonageStatusUpdate',
258+
cleanUpCallLogs: 'cleanUpCallLogs',
258259
credentialCreated: 'credentialCreated',
259260
credentialDeleted: 'credentialDeleted',
260261
fileCreated: 'fileCreated',

0 commit comments

Comments
 (0)