Change dump deletion logic

This commit is contained in:
Ajay Ramachandran
2021-04-18 12:28:47 -04:00
parent 911ebddd69
commit 2814ce7b7f

View File

@@ -41,6 +41,11 @@ interface TableDumpList {
}; };
let latestDumpFiles: TableDumpList[] = []; let latestDumpFiles: TableDumpList[] = [];
interface TableFile {
file: string,
timestamp: number
};
if (tables.length === 0) { if (tables.length === 0) {
Logger.warn('[dumpDatabase] No tables configured'); Logger.warn('[dumpDatabase] No tables configured');
} }
@@ -51,42 +56,37 @@ function removeOutdatedDumps(exportPath: string): Promise<void> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// Get list of table names // Get list of table names
// Create array for each table // Create array for each table
const tableFiles = tableNames.reduce((obj: any, tableName) => { const tableFiles: Record<string, TableFile[]> = tableNames.reduce((obj: any, tableName) => {
obj[tableName] = []; obj[tableName] = [];
return obj; return obj;
}, {}); }, {});
// read files in export directory // read files in export directory
fs.readdir(exportPath, (err: any, files: string[]) => { fs.readdir(exportPath, async (err: any, files: string[]) => {
if (err) Logger.error(err); if (err) Logger.error(err);
if (err) return resolve(); if (err) return resolve();
files.forEach(file => { files.forEach(file => {
// we only care about files that start with "<tablename>_" and ends with .csv // we only care about files that start with "<tablename>_" and ends with .csv
tableNames.forEach(tableName => { tableNames.forEach(tableName => {
if (file.startsWith(`${tableName}_`) && file.endsWith('.csv')) { if (file.startsWith(`${tableName}`) && file.endsWith('.csv')) {
// extract the timestamp from the filename const filePath = path.join(exportPath, file);
// we could also use the fs.stat mtime
const timestamp = Number(file.split('_')[1].replace('.csv', ''));
tableFiles[tableName].push({ tableFiles[tableName].push({
file: path.join(exportPath, file), file: filePath,
timestamp, timestamp: fs.statSync(filePath).mtime.getTime()
}); });
} }
}); });
}); });
const outdatedTime = Math.floor(Date.now() - (MILLISECONDS_BETWEEN_DUMPS * 1.5));
for (let tableName in tableFiles) { for (let tableName in tableFiles) {
const files = tableFiles[tableName]; const files = tableFiles[tableName].sort((a, b) => b.timestamp - a.timestamp);
files.forEach(async (item: any) => { for (let i = 2; i < files.length; i++) {
if (item.timestamp < outdatedTime) { // remove old file
// remove old file await unlink(files[i].file).catch((error: any) => {
await unlink(item.file).catch((error: any) => { Logger.error(`[dumpDatabase] Garbage collection failed ${error}`);
Logger.error(`[dumpDatabase] Garbage collection failed ${error}`); });
}); }
}
});
} }
resolve(); resolve();