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();
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({
contest_id: this.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.save();

12
models/contest_player.js

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

4
models/judge_state.js

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

8
models/user.js

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

11
modules/api.js

@ -95,14 +95,21 @@ app.get('/api/waiting_judge', async (req, res) => {
try {
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();
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();
await judge_state.loadRelationships();
await judge_state.problem.loadRelationships();
await waiting_judge.destroy();
syzoj.utils.unlock('/api/waiting_judge');
res.send({
have_task: 1,
judge_id: judge_state.id,
@ -143,4 +150,4 @@ app.get('/static/uploads/:md5', async (req, res) => {
} catch (e) {
res.status(500).send(e);
}
})
});

1
package.json

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

29
utility.js

@ -46,6 +46,7 @@ let pygmentize = require('pygmentize-bundled-cached');
let gravatar = require('gravatar');
let AdmZip = require('adm-zip');
let filesize = require('file-size');
let AsyncLock = require('async-lock');
function escapeHTML(s) {
// 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;
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 = {
resolvePath(s) {
let a = Array.from(arguments);
@ -320,5 +345,7 @@ module.exports = {
},
isValidUsername(s) {
return /^[a-zA-Z0-9\-\_]+$/.test(s);
}
},
lock: lock,
unlock: unlock
};

Loading…
Cancel
Save