vjudge: prevent race cond

pull/162/head
undefined 3 years ago
parent 577e100ca6
commit ce3cdc645c

@ -1,6 +1,6 @@
{ {
"name": "@hydrooj/vjudge", "name": "@hydrooj/vjudge",
"version": "1.0.4", "version": "1.0.5",
"description": "Submit problems to remote oj", "description": "Submit problems to remote oj",
"main": "package.json", "main": "package.json",
"repository": "https://github.com/hydro-dev/Hydro.git", "repository": "https://github.com/hydro-dev/Hydro.git",

@ -13,6 +13,7 @@ import { BasicProvider, IBasicProvider, RemoteAccount } from './interface';
const coll = db.collection('vjudge'); const coll = db.collection('vjudge');
const Pool = {}; const Pool = {};
const logger = new Logger('vjudge'); const logger = new Logger('vjudge');
const syncing = {};
class Service { class Service {
api: IBasicProvider; api: IBasicProvider;
@ -37,7 +38,9 @@ class Service {
while (pids.length) { while (pids.length) {
logger.info(`${domainId}: Syncing page ${page}`); logger.info(`${domainId}: Syncing page ${page}`);
for (const pid of pids) { for (const pid of pids) {
if (await ProblemModel.get(domainId, pid)) continue; if (syncing[`${domainId}/${pid}`] || await ProblemModel.get(domainId, pid)) continue;
syncing[`${domainId}/${pid}`] = true;
try {
const res = await this.api.getProblem(pid); const res = await this.api.getProblem(pid);
if (!res) continue; if (!res) continue;
const id = await ProblemModel.add(domainId, pid, res.title, res.content, 1, res.tag, false); const id = await ProblemModel.add(domainId, pid, res.title, res.content, 1, res.tag, false);
@ -48,6 +51,9 @@ class Service {
await ProblemModel.addTestdata(domainId, id, key, res.data[key]); await ProblemModel.addTestdata(domainId, id, key, res.data[key]);
} }
logger.info(`${domainId}: problem ${id} sync done`); logger.info(`${domainId}: problem ${id} sync done`);
} finally {
delete syncing[`${domainId}/${pid}`];
}
await sleep(5000); await sleep(5000);
} }
page++; page++;

Loading…
Cancel
Save