judge: add cache cleaner

This commit is contained in:
undefined committed 2021-02-28 14:36:46 +08:00
1 parent c251cf33e8
commit 8c6f9a3b71
3 files changed
+49 -2

No files matched your search

+4 -1
View File
@@ -1,3 +1,6 @@
#!/usr/bin/env node #!/usr/bin/env node
require('../dist/daemon')(); const { argv } = require('yargs');
if (argv._[0] == 'cache') require('../dist/cache')()
else if (!argv._) require('../dist/daemon')();
+1 -1
View File
@@ -1,7 +1,7 @@
{ {
"name": "@hydrooj/hydrojudge", "name": "@hydrooj/hydrojudge",
"bin": "bin/hydrojudge.js", "bin": "bin/hydrojudge.js",
"version": "2.3.16", "version": "2.3.17",
"main": "package.json", "main": "package.json",
"author": "undefined <i@undefined.moe>", "author": "undefined <i@undefined.moe>",
"repository": "https://github.com/hydro-dev/Hydro.git", "repository": "https://github.com/hydro-dev/Hydro.git",
+44
View File
@@ -0,0 +1,44 @@
import path from 'path';
import fs from 'fs-extra';
import { argv } from 'yargs';
import { Logger } from './log';
import { getConfig } from './config';
const logger = new Logger('cache');
export = function main() {
const CACHE_DIR = getConfig('cache_dir');
if (argv._[1] === 'clean') {
fs.emptyDirSync(CACHE_DIR);
logger.info('Cleaned cache.');
} else if (argv._[1] === 'prune') {
const duration = +argv.duration || 30;
const now = new Date().getTime();
const hosts = fs.readdirSync(CACHE_DIR);
let cnt = 0;
for (const host of hosts) {
const hostdir = path.resolve(CACHE_DIR, host);
if (!fs.statSync(hostdir).isDirectory()) continue;
const domains = fs.readdirSync(hostdir);
for (const domain of domains) {
const domaindir = path.resolve(hostdir, domain);
if (!fs.statSync(domaindir).isDirectory()) continue;
const problems = fs.readdirSync(domaindir);
for (const problem of problems) {
const problemdir = path.resolve(domaindir, problem);
if (!fs.statSync(problem).isDirectory()) continue;
let lastUsage = 0;
if (fs.existsSync(path.resolve(problemdir, 'lastUsage'))) {
const content = fs.readFileSync(path.resolve(problemdir, 'lastUsage')).toString();
lastUsage = +content || 0;
}
if (lastUsage + duration * 24 * 3600 * 1000 > now) continue;
fs.removeSync(problemdir);
cnt++;
logger.info('Removed cache %s', problemdir);
}
}
}
logger.info('Done! %d items deleted.', cnt);
}
};