Make THRESHOLD functionality more aggressive

This commit is contained in:
Essem 2022-07-17 12:46:52 -05:00
parent 91ad4a01cc
commit 1948c32277
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
3 changed files with 36 additions and 38 deletions

8
app.js
View file

@ -171,12 +171,8 @@ if (isMaster) {
logger.error("Invalid THRESHOLD config."); logger.error("Invalid THRESHOLD config.");
process.env.THRESHOLD = undefined; process.env.THRESHOLD = undefined;
} }
const dirstat = (await promises.readdir(process.env.TEMPDIR)).map(async (file) => { const dirstat = (await promises.readdir(process.env.TEMPDIR)).map((file) => {
return new Promise((resolve) => { return promises.stat(`${process.env.TEMPDIR}/${file}`).then((stats) => stats.size);
promises.stat(`${process.env.TEMPDIR}/${file}`).then((stats) => {
resolve(stats.size);
});
});
}); });
const size = await Promise.all(dirstat); const size = await Promise.all(dirstat);
process.env.DIRSIZECACHE = size.reduce((a, b)=>{ process.env.DIRSIZECACHE = size.reduce((a, b)=>{

View file

@ -59,24 +59,25 @@ export default async (client, cluster, worker, ipc, interaction) => {
process.env.DIRSIZECACHE += result.file.length; process.env.DIRSIZECACHE += result.file.length;
if (process.env.DIRSIZECACHE > process.env.THRESHOLD) { if (process.env.DIRSIZECACHE > process.env.THRESHOLD) {
const files = (await promises.readdir(process.env.TEMPDIR)).map((file) => { const files = (await promises.readdir(process.env.TEMPDIR)).map((file) => {
return new Promise((resolve, reject) => { return promises.stat(`${process.env.TEMPDIR}/${file}`).then((stats) => {
promises.stat(`${process.env.TEMPDIR}/${file}`).then((fstats)=>{ return {
resolve({
name: file, name: file,
size: fstats.size, size: stats.size,
ctime: fstats.ctime ctime: stats.ctime
}); };
}).catch(reject);
}); });
}); });
Promise.all(files).then((files) => { const resolvedFiles = await Promise.all(files);
process.env.DIRSIZECACHE = files.reduce((a, b)=>{ process.env.DIRSIZECACHE = resolvedFiles.reduce((a, b)=>{
return a + b.size; return a + b.size;
}, 0); }, 0);
const oldestFile = files.sort((a, b) => a.ctime - b.ctime)[0].name; const oldestFiles = resolvedFiles.sort((a, b) => a.ctime - b.ctime);
promises.rm(`${process.env.TEMPDIR}/${oldestFile}`); while (process.env.DIRSIZECACHE > process.env.THRESHOLD) {
logger.log(`Removed oldest image file: ${oldestFile}`); await promises.rm(`${process.env.TEMPDIR}/${oldestFiles[0].name}`);
}); process.env.DIRSIZECACHE -= oldestFiles[0].size;
logger.log(`Removed oldest image file: ${oldestFiles[0].name}`);
oldestFiles.shift();
}
} }
} }
} else { } else {

View file

@ -149,24 +149,25 @@ export default async (client, cluster, worker, ipc, message) => {
process.env.DIRSIZECACHE += result.file.length; process.env.DIRSIZECACHE += result.file.length;
if (process.env.DIRSIZECACHE > process.env.THRESHOLD) { if (process.env.DIRSIZECACHE > process.env.THRESHOLD) {
const files = (await promises.readdir(process.env.TEMPDIR)).map((file) => { const files = (await promises.readdir(process.env.TEMPDIR)).map((file) => {
return new Promise((resolve, reject) => { return promises.stat(`${process.env.TEMPDIR}/${file}`).then((stats) => {
promises.stat(`${process.env.TEMPDIR}/${file}`).then((fstats)=>{ return {
resolve({
name: file, name: file,
size: fstats.size, size: stats.size,
ctime: fstats.ctime ctime: stats.ctime
}); };
}).catch(reject);
}); });
}); });
Promise.all(files).then((files) => { const resolvedFiles = await Promise.all(files);
process.env.DIRSIZECACHE = files.reduce((a, b)=>{ process.env.DIRSIZECACHE = resolvedFiles.reduce((a, b)=>{
return a + b.size; return a + b.size;
}, 0); }, 0);
const oldestFile = files.sort((a, b) => a.ctime - b.ctime)[0].name; const oldestFiles = resolvedFiles.sort((a, b) => a.ctime - b.ctime);
promises.rm(`${process.env.TEMPDIR}/${oldestFile}`); while (process.env.DIRSIZECACHE > process.env.THRESHOLD) {
log(`Removed oldest image file: ${oldestFile}`); await promises.rm(`${process.env.TEMPDIR}/${oldestFiles[0].name}`);
}); process.env.DIRSIZECACHE -= oldestFiles[0].size;
log(`Removed oldest image file: ${oldestFiles[0].name}`);
oldestFiles.shift();
}
} }
} }
} else { } else {