Store directory size cache inside eris-fleet central store

This commit is contained in:
Essem 2022-07-19 23:27:43 -05:00
parent ffa22852bc
commit 1713387ffd
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
4 changed files with 14 additions and 11 deletions

3
app.js
View file

@ -175,8 +175,9 @@ if (isMaster) {
return promises.stat(`${process.env.TEMPDIR}/${file}`).then((stats) => stats.size); return promises.stat(`${process.env.TEMPDIR}/${file}`).then((stats) => stats.size);
}); });
const size = await Promise.all(dirstat); const size = await Promise.all(dirstat);
process.env.DIRSIZECACHE = size.reduce((a, b)=>{ const reduced = size.reduce((a, b) => {
return a + b; return a + b;
}, 0); }, 0);
Admiral.centralStore.set("dirSizeCache", reduced);
} }
} }

View file

@ -40,7 +40,7 @@ export default async (client, cluster, worker, ipc, interaction) => {
} }
if (result.file.length > fileSize) { if (result.file.length > fileSize) {
if (process.env.TEMPDIR && process.env.TEMPDIR !== "") { if (process.env.TEMPDIR && process.env.TEMPDIR !== "") {
await upload(client, result, interaction, true); await upload(client, ipc, result, interaction, true);
} else { } else {
await interaction[replyMethod]("The resulting image was more than 8MB in size, so I can't upload it."); await interaction[replyMethod]("The resulting image was more than 8MB in size, so I can't upload it.");
} }

View file

@ -129,7 +129,7 @@ export default async (client, cluster, worker, ipc, message) => {
} }
if (result.file.length > fileSize) { if (result.file.length > fileSize) {
if (process.env.TEMPDIR && process.env.TEMPDIR !== "") { if (process.env.TEMPDIR && process.env.TEMPDIR !== "") {
await upload(client, result, message); await upload(client, ipc, result, message);
} else { } else {
await client.createMessage(message.channel.id, "The resulting image was more than 8MB in size, so I can't upload it."); await client.createMessage(message.channel.id, "The resulting image was more than 8MB in size, so I can't upload it.");
} }

View file

@ -1,7 +1,7 @@
import * as logger from "../utils/logger.js"; import * as logger from "../utils/logger.js";
import { readdir, lstat, rm, writeFile } from "fs/promises"; import { readdir, lstat, rm, writeFile } from "fs/promises";
export async function upload(client, result, context, interaction = false) { export async function upload(client, ipc, result, context, interaction = false) {
const filename = `${Math.random().toString(36).substring(2, 15)}.${result.name.split(".")[1]}`; const filename = `${Math.random().toString(36).substring(2, 15)}.${result.name.split(".")[1]}`;
await writeFile(`${process.env.TEMPDIR}/${filename}`, result.file); await writeFile(`${process.env.TEMPDIR}/${filename}`, result.file);
const imageURL = `${process.env.TMP_DOMAIN || "https://tmp.projectlounge.pw"}/${filename}`; const imageURL = `${process.env.TMP_DOMAIN || "https://tmp.projectlounge.pw"}/${filename}`;
@ -34,13 +34,14 @@ export async function upload(client, result, context, interaction = false) {
})); }));
} }
if (process.env.THRESHOLD) { if (process.env.THRESHOLD) {
process.env.DIRSIZECACHE = parseInt(process.env.DIRSIZECACHE) + result.file.length; const size = ipc.centralStore.get("dirSizeCache") + result.file.length;
await removeOldImages(); ipc.centralStore.set("dirSizeCache", size);
await removeOldImages(ipc, size);
} }
} }
export async function removeOldImages() { export async function removeOldImages(ipc, size) {
if (process.env.DIRSIZECACHE > process.env.THRESHOLD) { if (size > process.env.THRESHOLD) {
const files = (await readdir(process.env.TEMPDIR)).map((file) => { const files = (await readdir(process.env.TEMPDIR)).map((file) => {
return lstat(`${process.env.TEMPDIR}/${file}`).then((stats) => { return lstat(`${process.env.TEMPDIR}/${file}`).then((stats) => {
if (stats.isSymbolicLink()) return; if (stats.isSymbolicLink()) return;
@ -52,14 +53,15 @@ export async function removeOldImages() {
}); });
}).filter(Boolean); }).filter(Boolean);
const resolvedFiles = await Promise.all(files); const resolvedFiles = await Promise.all(files);
process.env.DIRSIZECACHE = resolvedFiles.reduce((a, b)=>{ let newSize = resolvedFiles.reduce((a, b)=>{
return a + b.size; return a + b.size;
}, 0); }, 0);
const oldestFiles = resolvedFiles.sort((a, b) => a.ctime - b.ctime); const oldestFiles = resolvedFiles.sort((a, b) => a.ctime - b.ctime);
while (process.env.DIRSIZECACHE > process.env.THRESHOLD) { while (newSize > process.env.THRESHOLD) {
if (!oldestFiles[0]) break; if (!oldestFiles[0]) break;
await rm(`${process.env.TEMPDIR}/${oldestFiles[0].name}`); await rm(`${process.env.TEMPDIR}/${oldestFiles[0].name}`);
process.env.DIRSIZECACHE = parseInt(process.env.DIRSIZECACHE) - oldestFiles[0].size; newSize -= oldestFiles[0].size;
ipc.centralStore.set("dirSizeCache", newSize);
logger.log(`Removed oldest image file: ${oldestFiles[0].name}`); logger.log(`Removed oldest image file: ${oldestFiles[0].name}`);
oldestFiles.shift(); oldestFiles.shift();
} }