Browse Source

Add lock on somewhere

pull/6/head
Menci 8 years ago
parent
commit
5ceb14f7f9
  1. 4
      models/contest.js
  2. 12
      models/contest_player.js
  3. 4
      models/judge_state.js
  4. 8
      models/user.js
  5. 11
      modules/api.js
  6. 1
      package.json
  7. 29
      utility.js

4
models/contest.js

@ -120,6 +120,8 @@ class Contest extends Model {
let problems = await this.getProblems(); let problems = await this.getProblems();
if (!problems.includes(judge_state.problem_id)) throw new ErrorMessage('当前比赛中无此题目。'); if (!problems.includes(judge_state.problem_id)) throw new ErrorMessage('当前比赛中无此题目。');
await syzoj.utils.lock('Contest::newSubmission', 'create_player', judge_state.user_id);
let player = await ContestPlayer.findInContest({ let player = await ContestPlayer.findInContest({
contest_id: this.id, contest_id: this.id,
user_id: judge_state.user_id user_id: judge_state.user_id
@ -132,6 +134,8 @@ class Contest extends Model {
}); });
} }
syzoj.utils.unlock('Contest::newSubmission', 'create_player', judge_state.user_id);
await player.updateScore(judge_state); await player.updateScore(judge_state);
await player.save(); await player.save();

12
models/contest_player.js

@ -69,10 +69,11 @@ class ContestPlayer extends Model {
} }
async updateScore(judge_state) { async updateScore(judge_state) {
await syzoj.utils.lock('ContestPlayer::updateScore', this.id);
await this.loadRelationships(); await this.loadRelationships();
if (this.contest.type === 'ioi') { if (this.contest.type === 'ioi') {
if (judge_state.pending) return; if (!judge_state.pending) {
if (!this.score_details[judge_state.problem_id]) { if (!this.score_details[judge_state.problem_id]) {
this.score_details[judge_state.problem_id] = { this.score_details[judge_state.problem_id] = {
score: judge_state.score, score: judge_state.score,
@ -105,6 +106,7 @@ class ContestPlayer extends Model {
for (let x in this.score_details) { for (let x in this.score_details) {
this.score += this.score_details[x].score; this.score += this.score_details[x].score;
} }
}
} else if (this.contest.type === 'noi') { } else if (this.contest.type === 'noi') {
this.score_details[judge_state.problem_id] = { this.score_details[judge_state.problem_id] = {
score: judge_state.score, score: judge_state.score,
@ -115,8 +117,7 @@ class ContestPlayer extends Model {
this.score += this.score_details[x].score; this.score += this.score_details[x].score;
} }
} else if (this.contest.type === 'acm') { } else if (this.contest.type === 'acm') {
if (judge_state.pending) return; if (!judge_state.pending) {
if (!this.score_details[judge_state.problem_id]) { if (!this.score_details[judge_state.problem_id]) {
this.score_details[judge_state.problem_id] = { this.score_details[judge_state.problem_id] = {
accepted: false, accepted: false,
@ -160,6 +161,9 @@ class ContestPlayer extends Model {
} }
} }
syzoj.utils.unlock('ContestPlayer::updateScore', this.id);
}
getModel() { return model; } getModel() { return model; }
} }

4
models/judge_state.js

@ -196,6 +196,8 @@ class JudgeState extends Model {
} }
async rejudge() { async rejudge() {
await syzoj.utils.lock('JudgeState::rejudge', this.id);
await this.loadRelationships(); await this.loadRelationships();
let oldStatus = this.status; let oldStatus = this.status;
@ -229,6 +231,8 @@ class JudgeState extends Model {
let contest = await Contest.fromID(this.type_info); let contest = await Contest.fromID(this.type_info);
await contest.newSubmission(this); await contest.newSubmission(this);
} }
syzoj.utils.unlock('JudgeState::rejudge', this.id);
} }
getModel() { return model; } getModel() { return model; }

8
models/user.js

@ -88,6 +88,8 @@ class User extends Model {
} }
async refreshSubmitInfo() { async refreshSubmitInfo() {
await syzoj.utils.lock('User::refreshSubmitInfo', this.id);
let JudgeState = syzoj.model('judge_state'); let JudgeState = syzoj.model('judge_state');
let all = await JudgeState.model.findAll({ let all = await JudgeState.model.findAll({
attributes: ['problem_id'], attributes: ['problem_id'],
@ -95,7 +97,7 @@ class User extends Model {
user_id: this.id, user_id: this.id,
status: 'Accepted', status: 'Accepted',
type: { type: {
$ne: 1 // Not a contest submissio $ne: 1 // Not a contest submission
} }
} }
}); });
@ -107,11 +109,13 @@ class User extends Model {
let cnt = await JudgeState.count({ let cnt = await JudgeState.count({
user_id: this.id, user_id: this.id,
type: { type: {
$ne: 1 // Not a contest submissio $ne: 1 // Not a contest submission
} }
}); });
this.submit_num = cnt; this.submit_num = cnt;
syzoj.utils.unlock('User::refreshSubmitInfo', this.id);
} }
async getACProblems() { async getACProblems() {

11
modules/api.js

@ -95,14 +95,21 @@ app.get('/api/waiting_judge', async (req, res) => {
try { try {
if (req.query.session_id !== syzoj.config.judge_token) return res.status(404).send({ err: 'Permission denied' }); if (req.query.session_id !== syzoj.config.judge_token) return res.status(404).send({ err: 'Permission denied' });
await syzoj.utils.lock('/api/waiting_judge');
let waiting_judge = await WaitingJudge.findOne(); let waiting_judge = await WaitingJudge.findOne();
if (!waiting_judge) return res.send({ have_task: 0 }); if (!waiting_judge) {
syzoj.utils.unlock('/api/waiting_judge');
return res.send({ have_task: 0 });
}
let judge_state = await waiting_judge.getJudgeState(); let judge_state = await waiting_judge.getJudgeState();
await judge_state.loadRelationships(); await judge_state.loadRelationships();
await judge_state.problem.loadRelationships(); await judge_state.problem.loadRelationships();
await waiting_judge.destroy(); await waiting_judge.destroy();
syzoj.utils.unlock('/api/waiting_judge');
res.send({ res.send({
have_task: 1, have_task: 1,
judge_id: judge_state.id, judge_id: judge_state.id,
@ -143,4 +150,4 @@ app.get('/static/uploads/:md5', async (req, res) => {
} catch (e) { } catch (e) {
res.status(500).send(e); res.status(500).send(e);
} }
}) });

1
package.json

@ -25,6 +25,7 @@
"dependencies": { "dependencies": {
"adm-zip": "^0.4.7", "adm-zip": "^0.4.7",
"ansi-to-html": "^0.4.2", "ansi-to-html": "^0.4.2",
"async-lock": "^0.3.9",
"body-parser": "^1.15.2", "body-parser": "^1.15.2",
"cheerio": "^1.0.0-rc.1", "cheerio": "^1.0.0-rc.1",
"cookie-parser": "^1.4.3", "cookie-parser": "^1.4.3",

29
utility.js

@ -46,6 +46,7 @@ let pygmentize = require('pygmentize-bundled-cached');
let gravatar = require('gravatar'); let gravatar = require('gravatar');
let AdmZip = require('adm-zip'); let AdmZip = require('adm-zip');
let filesize = require('file-size'); let filesize = require('file-size');
let AsyncLock = require('async-lock');
function escapeHTML(s) { function escapeHTML(s) {
// Code from http://stackoverflow.com/questions/5251520/how-do-i-escape-some-html-in-javascript/5251551 // Code from http://stackoverflow.com/questions/5251520/how-do-i-escape-some-html-in-javascript/5251551
@ -73,6 +74,30 @@ function highlightPygmentize(code, lang, cb) {
renderer.config.highlight = highlightPygmentize; renderer.config.highlight = highlightPygmentize;
let locks = {};
let lock = function () {
let s = JSON.stringify(Array.from(arguments));
return new Promise((resolve, reject) => {
if (!locks[s]) {
locks[s] = {
lock: new AsyncLock(),
done: null
};
}
locks[s].lock.acquire('', done => {
locks[s].done = done;
resolve();
});
});
};
let unlock = function () {
let s = JSON.stringify(Array.from(arguments));
locks[s].done();
locks[s].done = null;
}
module.exports = { module.exports = {
resolvePath(s) { resolvePath(s) {
let a = Array.from(arguments); let a = Array.from(arguments);
@ -320,5 +345,7 @@ module.exports = {
}, },
isValidUsername(s) { isValidUsername(s) {
return /^[a-zA-Z0-9\-\_]+$/.test(s); return /^[a-zA-Z0-9\-\_]+$/.test(s);
} },
lock: lock,
unlock: unlock
}; };

Loading…
Cancel
Save