Merge pull request #18 from tgpethan/cache-fix1

Stop stats api cache from halting
This commit is contained in:
Leah Nicole 2020-10-16 16:47:02 +01:00 committed by GitHub
commit 7d0291fe2c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 6 deletions

13
EUS.js
View File

@ -66,8 +66,8 @@ if (!fs.existsSync(__dirname + BASE_PATH + "/config.json")) {
// Cache for the file count and space usage, this takes a while to do so it's best to cache the result // Cache for the file count and space usage, this takes a while to do so it's best to cache the result
let cacheIsReady = false; let cacheIsReady = false;
function cacheFilesAndSpace() { async function cacheFilesAndSpace() {
cacheIsReady = false; const startCacheTime = new Date().getTime();
let cachedFilesAndSpace = { let cachedFilesAndSpace = {
files: {} files: {}
}, },
@ -79,7 +79,7 @@ function cacheFilesAndSpace() {
cachedFilesAndSpace["files"][`${eusConfig.acceptedTypes[i2]}`.replace(".", "")] = 0; cachedFilesAndSpace["files"][`${eusConfig.acceptedTypes[i2]}`.replace(".", "")] = 0;
} }
// Read all files from the images directory // Read all files from the images directory
fs.readdir(__dirname + BASE_PATH + "/i", (err, files) => { fs.readdir(__dirname + BASE_PATH + "/i", async (err, files) => {
if (err) throw err; if (err) throw err;
// Loop through all files // Loop through all files
for (var i = 0; i < files.length; i++) { for (var i = 0; i < files.length; i++) {
@ -102,7 +102,7 @@ function cacheFilesAndSpace() {
usage: {} usage: {}
}; };
// Get the space used on the disk // Get the space used on the disk
getSize(__dirname + BASE_PATH + "/i", (err, size) => { getSize(__dirname + BASE_PATH + "/i", async (err, size) => {
if (err) throw err; if (err) throw err;
// Calculate in different units the space taken up on disk // Calculate in different units the space taken up on disk
let sizeOfFolder = (size / 1024 / 1024); let sizeOfFolder = (size / 1024 / 1024);
@ -111,7 +111,7 @@ function cacheFilesAndSpace() {
cachedFilesAndSpace["space"]["usage"]["gb"] = sizeOfFolder; cachedFilesAndSpace["space"]["usage"]["gb"] = sizeOfFolder;
cachedFilesAndSpace["space"]["usage"]["string"] = spaceToLowest(size, true); cachedFilesAndSpace["space"]["usage"]["string"] = spaceToLowest(size, true);
// Get total disk space // Get total disk space
diskUsage.check(__dirname, (err, data) => { diskUsage.check(__dirname, async (err, data) => {
if (err) throw err; if (err) throw err;
cachedFilesAndSpace["space"]["total"] = { cachedFilesAndSpace["space"]["total"] = {
value: spaceToLowest(data["total"], false), value: spaceToLowest(data["total"], false),
@ -123,11 +123,11 @@ function cacheFilesAndSpace() {
cacheIsReady = true; cacheIsReady = true;
cacheJSON = JSON.stringify(cachedFilesAndSpace); cacheJSON = JSON.stringify(cachedFilesAndSpace);
global.modules.consoleHelper.printInfo(emoji.folder, `Stats api cache took ${new Date().getTime() - startCacheTime}ms`);
}); });
}); });
}); });
} }
cacheFilesAndSpace();
function validateConfig(json) { function validateConfig(json) {
let performShutdownAfterValidation = false; let performShutdownAfterValidation = false;
@ -172,6 +172,7 @@ module.exports = {
extras:function() { extras:function() {
// Setup express to use busboy // Setup express to use busboy
global.app.use(busboy()); global.app.use(busboy());
cacheFilesAndSpace();
}, },
get:function(req, res) { get:function(req, res) {
/* /*