|
|
@ -2,6 +2,7 @@ import http = require('http'); |
|
|
|
import socketio = require('socket.io'); |
|
|
|
import socketio = require('socket.io'); |
|
|
|
import diff = require('jsondiffpatch'); |
|
|
|
import diff = require('jsondiffpatch'); |
|
|
|
import jwt = require('jsonwebtoken'); |
|
|
|
import jwt = require('jsonwebtoken'); |
|
|
|
|
|
|
|
import winston = require('winston'); |
|
|
|
|
|
|
|
|
|
|
|
import { globalConfig as Cfg } from './config'; |
|
|
|
import { globalConfig as Cfg } from './config'; |
|
|
|
import { convertResult } from '../judgeResult'; |
|
|
|
import { convertResult } from '../judgeResult'; |
|
|
@ -46,6 +47,7 @@ export function initializeSocketIO(s: http.Server) { |
|
|
|
throw new Error("Request type in token mismatch."); |
|
|
|
throw new Error("Request type in token mismatch."); |
|
|
|
} |
|
|
|
} |
|
|
|
} catch (err) { |
|
|
|
} catch (err) { |
|
|
|
|
|
|
|
winston.info('The client has an incorrect token.'); |
|
|
|
cb({ |
|
|
|
cb({ |
|
|
|
ok: false, |
|
|
|
ok: false, |
|
|
|
message: err.toString() |
|
|
|
message: err.toString() |
|
|
@ -53,15 +55,18 @@ export function initializeSocketIO(s: http.Server) { |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
const taskId = req.taskId; |
|
|
|
const taskId = req.taskId; |
|
|
|
|
|
|
|
winston.verbose(`A client trying to get detailed progress for ${taskId}.`); |
|
|
|
socket.join(taskId.toString()); |
|
|
|
socket.join(taskId.toString()); |
|
|
|
if (finishedJudgeList[taskId]) { |
|
|
|
if (finishedJudgeList[taskId]) { |
|
|
|
|
|
|
|
winston.debug(`Judge task #${taskId} has been finished, ${JSON.stringify(currentJudgeList[taskId])}`); |
|
|
|
cb({ |
|
|
|
cb({ |
|
|
|
ok: true, |
|
|
|
ok: true, |
|
|
|
finished: true, |
|
|
|
finished: true, |
|
|
|
result: currentJudgeList[taskId], |
|
|
|
result: currentJudgeList[taskId] && currentJudgeList[taskId].current, |
|
|
|
roughResult: finishedJudgeList[taskId] |
|
|
|
roughResult: finishedJudgeList[taskId] |
|
|
|
}); |
|
|
|
}); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
|
|
|
|
winston.debug(`Judge task #${taskId} has not been finished`); |
|
|
|
cb({ |
|
|
|
cb({ |
|
|
|
ok: true, |
|
|
|
ok: true, |
|
|
|
finished: false, |
|
|
|
finished: false, |
|
|
@ -70,7 +75,6 @@ export function initializeSocketIO(s: http.Server) { |
|
|
|
} |
|
|
|
} |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
}); |
|
|
|
|
|
|
|
|
|
|
|
roughProgressNamespace.on('connection', (socket) => { |
|
|
|
roughProgressNamespace.on('connection', (socket) => { |
|
|
|
socket.on('join', (reqJwt, cb) => { |
|
|
|
socket.on('join', (reqJwt, cb) => { |
|
|
|
let req; |
|
|
|
let req; |
|
|
@ -153,6 +157,7 @@ export function initializeSocketIO(s: http.Server) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function createTask(taskId: number) { |
|
|
|
export function createTask(taskId: number) { |
|
|
|
|
|
|
|
winston.debug(`Judge task #${taskId} has started`); |
|
|
|
detailProgressNamespace.to(taskId.toString()).emit("start", { taskId: taskId }); |
|
|
|
detailProgressNamespace.to(taskId.toString()).emit("start", { taskId: taskId }); |
|
|
|
roughProgressNamespace.to(taskId.toString()).emit("start", { taskId: taskId }); |
|
|
|
roughProgressNamespace.to(taskId.toString()).emit("start", { taskId: taskId }); |
|
|
|
compileProgressNamespace.to(taskId.toString()).emit("start", { taskId: taskId }); |
|
|
|
compileProgressNamespace.to(taskId.toString()).emit("start", { taskId: taskId }); |
|
|
@ -160,6 +165,7 @@ export function createTask(taskId: number) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function updateCompileStatus(taskId: number, result: CompilationResult) { |
|
|
|
export function updateCompileStatus(taskId: number, result: CompilationResult) { |
|
|
|
|
|
|
|
winston.debug(`Updating compilation status for #${taskId}`); |
|
|
|
compileProgressNamespace.to(taskId.toString()).emit('compiled', { |
|
|
|
compileProgressNamespace.to(taskId.toString()).emit('compiled', { |
|
|
|
taskId: taskId, |
|
|
|
taskId: taskId, |
|
|
|
result: { |
|
|
|
result: { |
|
|
@ -170,7 +176,7 @@ export function updateCompileStatus(taskId: number, result: CompilationResult) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function updateProgress(taskId: number, data: OverallResult) { |
|
|
|
export function updateProgress(taskId: number, data: OverallResult) { |
|
|
|
// currentJudgeList[taskId].current = data;
|
|
|
|
winston.debug(`Updating progress for #${taskId}, data: ${JSON.stringify(data)}`); |
|
|
|
const original = currentJudgeList[taskId].current; |
|
|
|
const original = currentJudgeList[taskId].current; |
|
|
|
const delta = diff.diff(original, data); |
|
|
|
const delta = diff.diff(original, data); |
|
|
|
detailProgressNamespace.to(taskId.toString()).emit('update', { |
|
|
|
detailProgressNamespace.to(taskId.toString()).emit('update', { |
|
|
@ -181,6 +187,8 @@ export function updateProgress(taskId: number, data: OverallResult) { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export function updateResult(taskId: number, data: OverallResult) { |
|
|
|
export function updateResult(taskId: number, data: OverallResult) { |
|
|
|
|
|
|
|
currentJudgeList[taskId].running = false; |
|
|
|
|
|
|
|
currentJudgeList[taskId].current = data; |
|
|
|
const finalResult = convertResult(taskId, data); |
|
|
|
const finalResult = convertResult(taskId, data); |
|
|
|
const roughResult = { |
|
|
|
const roughResult = { |
|
|
|
result: finalResult.statusString, |
|
|
|
result: finalResult.statusString, |
|
|
@ -202,5 +210,5 @@ export function updateResult(taskId: number, data: OverallResult) { |
|
|
|
|
|
|
|
|
|
|
|
export function cleanupProgress(taskId: number) { |
|
|
|
export function cleanupProgress(taskId: number) { |
|
|
|
// Prevent race condition
|
|
|
|
// Prevent race condition
|
|
|
|
setTimeout(() => delete currentJudgeList[taskId], 10000); |
|
|
|
setTimeout(() => { delete currentJudgeList[taskId]; }, 10000); |
|
|
|
} |
|
|
|
} |