diff --git a/src/lib/storage/jobs/helpers/deleteOldFiles.ts b/src/lib/storage/jobs/helpers/deleteOldFiles.ts index d7106c44e..39ff1c543 100644 --- a/src/lib/storage/jobs/helpers/deleteOldFiles.ts +++ b/src/lib/storage/jobs/helpers/deleteOldFiles.ts @@ -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); + }); } diff --git a/src/lib/storage/jobs/helpers/runCleanup.ts b/src/lib/storage/jobs/helpers/runCleanup.ts index e72c13a5b..a26a681ad 100644 --- a/src/lib/storage/jobs/helpers/runCleanup.ts +++ b/src/lib/storage/jobs/helpers/runCleanup.ts @@ -1,3 +1,6 @@ +import path from 'path'; +import os from 'os'; + import { Knex } from 'knex'; import deleteOldFiles from './deleteOldFiles'; @@ -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'"