Fixed Metrics script with weird ass sleeper function

pull/126/head
tycrek 3 years ago
parent 0abc56c5f7
commit 38501ce433
No known key found for this signature in database
GPG Key ID: 25D74F3943625263

@ -7,6 +7,17 @@ const { bucketSize } = require('./storage');
const { TLog } = require('@tycrek/log'); const { TLog } = require('@tycrek/log');
const log = new TLog({ level: 'debug', timestamp: { enabled: false } }); const log = new TLog({ level: 'debug', timestamp: { enabled: false } });
/**
* Thank you CoPilot for helping write whatever the fuck this is -tycrek, 2022-04-18
*/
function whileWait(expression, timeout = 1000) {
return new Promise(async (resolve, reject) => {
while (expression())
await new Promise((resolve) => setTimeout(resolve, timeout));
resolve();
});
}
module.exports = () => { module.exports = () => {
const data = require('./data').data; const data = require('./data').data;
const { users } = fs.readJsonSync(path.join(process.cwd(), 'auth.json')); const { users } = fs.readJsonSync(path.join(process.cwd(), 'auth.json'));
@ -16,7 +27,8 @@ module.exports = () => {
let oldSize = 0; let oldSize = 0;
let d = []; let d = [];
data.get() whileWait(() => data() === undefined)
.then(() => data().get())
.then((D) => (d = D.map(([, resource]) => resource))) .then((D) => (d = D.map(([, resource]) => resource)))
.then(() => .then(() =>
d.forEach(({ token, size }) => { d.forEach(({ token, size }) => {
@ -47,7 +59,7 @@ module.exports = () => {
Object.values(users).forEach(({ username, count, size }) => log.info(`- ${username}`, formatBytes(size), `${count} files`)); Object.values(users).forEach(({ username, count, size }) => log.info(`- ${username}`, formatBytes(size), `${count} files`));
process.exit(0); process.exit(0);
}) })
.catch(log.c.error); .catch(console.error);
} }
if (require.main === module) module.exports(); if (require.main === module) module.exports();

Loading…
Cancel
Save