mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
judge: add cache cleaner
This commit is contained in:
1 parent
c251cf33e8
commit
8c6f9a3b71
3 files changed
+49
-2
No files matched your search
@@ -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);
|
||||
}
|
||||
};
|
||||
Reference in new issue
Block a user