Merge branch 'bjcscat-master'

This commit is contained in:
Essem 2022-06-11 14:30:25 -05:00
commit e211ddf5b9
No known key found for this signature in database
GPG key ID: 7D497397CC3A2A8C
4 changed files with 80 additions and 1 deletions

View file

@ -40,6 +40,8 @@ OUTPUT=
TEMPDIR=
# Put temporary image web server domain
TMP_DOMAIN=
# Threshold where optional space saving methods will be performed
THRESHOLD=
# Port for serving metrics. Metrics served are compatible with Prometheus.
METRICS=

31
app.js
View file

@ -22,10 +22,11 @@ import Shard from "./shard.js";
import ImageWorker from "./utils/services/image.js";
import PrometheusWorker from "./utils/services/prometheus.js";
// some utils
import { readFileSync } from "fs";
import { promises, readFileSync } from "fs";
import winston from "winston";
import { exec as baseExec } from "child_process";
import { promisify } from "util";
const exec = promisify(baseExec);
// database stuff
import database from "./utils/database.js";
@ -172,4 +173,32 @@ if (isMaster) {
});
});
}
// process the threshold into bytes early
if (process.env.TEMPDIR && process.env.THRESHOLD) {
const matched = process.env.THRESHOLD.match(/(\d+)([KMGT])/);
const sizes = {
K: 1024,
M: 1048576,
G: 1073741824,
T: 1099511627776
};
if (matched && matched[1] && matched[2]) {
process.env.THRESHOLD = matched[1] * sizes[matched[2]];
} else {
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 size = await Promise.all(dirstat);
process.env.DIRSIZECACHE = size.reduce((a, b)=>{
return a + b;
}, 0);
}
}

View file

@ -55,6 +55,30 @@ export default async (client, cluster, worker, ipc, interaction) => {
},
}]
});
if (process.env.THRESHOLD) {
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);
});
});
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}`);
});
}
}
} else {
await interaction[interaction.acknowledged ? "editOriginalMessage" : "createMessage"]("The resulting image was more than 8MB in size, so I can't upload it.");
}

View file

@ -142,6 +142,30 @@ export default async (client, cluster, worker, ipc, message) => {
},
}]
}, reference));
if (process.env.THRESHOLD) {
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);
});
});
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}`);
});
}
}
} else {
await client.createMessage(message.channel.id, "The resulting image was more than 8MB in size, so I can't upload it.");
}