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.");
process.env.THRESHOLD = undefined;
}
const dirstat = (await promises.readdir(process.env.TEMPDIR)).map(async (file) => {
return new Promise((resolve) => {
promises.stat(`${process.env.TEMPDIR}/${file}`).then((stats) => {
resolve(stats.size);
});
});
const dirstat = (await promises.readdir(process.env.TEMPDIR)).map((file) => {
return promises.stat(`${process.env.TEMPDIR}/${file}`).then((stats) => stats.size);
});
const size = await Promise.all(dirstat);
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;
if (process.env.DIRSIZECACHE > process.env.THRESHOLD) {
const files = (await promises.readdir(process.env.TEMPDIR)).map((file) => {
return new Promise((resolve, reject) => {
promises.stat(`${process.env.TEMPDIR}/${file}`).then((fstats)=>{
resolve({
name: file,
size: fstats.size,
ctime: fstats.ctime
});
}).catch(reject);
return promises.stat(`${process.env.TEMPDIR}/${file}`).then((stats) => {
return {
name: file,
size: stats.size,
ctime: stats.ctime
};
});
});
Promise.all(files).then((files) => {
process.env.DIRSIZECACHE = files.reduce((a, b)=>{
return a+b.size;
}, 0);
const oldestFile = files.sort((a, b) => a.ctime - b.ctime)[0].name;
promises.rm(`${process.env.TEMPDIR}/${oldestFile}`);
logger.log(`Removed oldest image file: ${oldestFile}`);
});
const resolvedFiles = await Promise.all(files);
process.env.DIRSIZECACHE = resolvedFiles.reduce((a, b)=>{
return a + b.size;
}, 0);
const oldestFiles = resolvedFiles.sort((a, b) => a.ctime - b.ctime);
while (process.env.DIRSIZECACHE > process.env.THRESHOLD) {
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 {

View file

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