Browse Source

Fix lock

pull/6/head
Menci 8 years ago
parent
commit
4a7d1ea141
  1. 9
      models/contest.js
  2. 6
      models/contest_player.js
  3. 6
      models/judge_state.js
  4. 6
      models/user.js
  5. 17
      modules/api.js
  6. 32
      utility.js

9
models/contest.js

@ -120,9 +120,9 @@ 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({
let player;
await syzoj.utils.lock(['Contest::newSubmission', 'create_player', judge_state.user_id], async () => {
player = await ContestPlayer.findInContest({
contest_id: this.id,
user_id: judge_state.user_id
});
@ -133,8 +133,7 @@ class Contest extends Model {
user_id: judge_state.user_id
});
}
syzoj.utils.unlock('Contest::newSubmission', 'create_player', judge_state.user_id);
});
await player.updateScore(judge_state);
await player.save();

6
models/contest_player.js

@ -69,8 +69,7 @@ class ContestPlayer extends Model {
}
async updateScore(judge_state) {
await syzoj.utils.lock('ContestPlayer::updateScore', this.id);
await syzoj.utils.lock(['ContestPlayer::updateScore', this.id], async () => {
await this.loadRelationships();
if (this.contest.type === 'ioi') {
if (!judge_state.pending) {
@ -161,8 +160,7 @@ class ContestPlayer extends Model {
}
}
}
syzoj.utils.unlock('ContestPlayer::updateScore', this.id);
});
}
getModel() { return model; }

6
models/judge_state.js

@ -191,8 +191,7 @@ class JudgeState extends Model {
}
async rejudge() {
await syzoj.utils.lock('JudgeState::rejudge', this.id);
await syzoj.utils.lock(['JudgeState::rejudge', this.id], async () => {
await this.loadRelationships();
let oldStatus = this.status;
@ -226,8 +225,7 @@ 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; }

6
models/user.js

@ -88,8 +88,7 @@ class User extends Model {
}
async refreshSubmitInfo() {
await syzoj.utils.lock('User::refreshSubmitInfo', this.id);
await syzoj.utils.lock(['User::refreshSubmitInfo', this.id], async () => {
let JudgeState = syzoj.model('judge_state');
let all = await JudgeState.model.findAll({
attributes: ['problem_id'],
@ -114,8 +113,7 @@ class User extends Model {
});
this.submit_num = cnt;
syzoj.utils.unlock('User::refreshSubmitInfo', this.id);
});
}
async getACProblems() {

17
modules/api.js

@ -95,21 +95,20 @@ 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 judge_state;
await syzoj.utils.lock('/api/waiting_judge', async () => {
let waiting_judge = await WaitingJudge.findOne();
if (!waiting_judge) {
syzoj.utils.unlock('/api/waiting_judge');
return res.send({ have_task: 0 });
return;
}
let judge_state = await waiting_judge.getJudgeState();
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');
if (judge_state) {
res.send({
have_task: 1,
judge_id: judge_state.id,
@ -122,8 +121,10 @@ app.get('/api/waiting_judge', async (req, res) => {
file_io_input_name: judge_state.problem.file_io_input_name,
file_io_output_name: judge_state.problem.file_io_output_name
});
} else {
res.send({ have_task: 0 });
}
} catch (e) {
syzoj.log(e);
res.status(500).send(e);
}
});

32
utility.js

@ -74,30 +74,6 @@ 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);
@ -346,6 +322,10 @@ module.exports = {
isValidUsername(s) {
return /^[a-zA-Z0-9\-\_]+$/.test(s);
},
lock: lock,
unlock: unlock
locks: [],
lock(key, cb) {
let s = JSON.stringify(key);
if (!this.locks[s]) this.locks[s] = new AsyncLock();
return this.locks[s].acquire(s, cb);
}
};

Loading…
Cancel
Save