Skip to content

Commit

Permalink
fix: use environment locations for cleanup
Browse files Browse the repository at this point in the history
We have a bug in production where the upload base is never cleaned up.
  • Loading branch information
aalemayhu committed Sep 29, 2023
1 parent b1e90a2 commit 864110a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 11 deletions.
23 changes: 13 additions & 10 deletions src/lib/storage/jobs/helpers/deleteOldFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import path from 'path';
import findRemoveSync from 'find-remove';
import { TIME_21_MINUTES_AS_SECONDS } from '../../../constants';

export default function deleteOldFiles() {
const locations = ['workspaces', 'uploads'];
for (const loc of locations) {
console.time(`finding & removing ${loc} files older than 21 minutes`);
findRemoveSync(path.join(os.tmpdir(), loc), {
files: '*.*',
age: { seconds: TIME_21_MINUTES_AS_SECONDS },
});
console.timeEnd(`finding & removing ${loc} files older than 21 minutes`);
}
function deleteFile(loc: string) {
console.time(`finding & removing ${loc} files older than 21 minutes`);
findRemoveSync(path.join(os.tmpdir(), loc), {
files: '*.*',
age: { seconds: TIME_21_MINUTES_AS_SECONDS },
});
console.timeEnd(`finding & removing ${loc} files older than 21 minutes`);
}

export default function deleteOldFiles(locations: string[]) {
locations.forEach((loc) => {
deleteFile(loc);
});
}
10 changes: 9 additions & 1 deletion src/lib/storage/jobs/helpers/runCleanup.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import path from 'path';
import os from 'os';

import { Knex } from 'knex';

import deleteOldFiles from './deleteOldFiles';
Expand All @@ -6,7 +9,12 @@ import { getDatabase } from '../../../../data_layer';

export const runCleanup = async (database: Knex) => {
console.time('running cleanup');
deleteOldFiles();
const locations = [
process.env.WORKSPACE_BASE ?? path.join(os.tmpdir(), 'workspaces'),
process.env.UPLOAD_BASE ?? path.join(os.tmpdir(), 'uploads'),
];

deleteOldFiles(locations);
await deleteOldUploads(database);
database.raw(
"DELETE FROM jobs WHERE created_at < NOW() - INTERVAL '14 days' AND status = 'failed'"
Expand Down

0 comments on commit 864110a

Please sign in to comment.