-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
16 changed files
with
486 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
{ | ||
"apps": [ | ||
{ | ||
"name": "summer", | ||
"script": "./dist/main.js", | ||
"cwd": "./", | ||
"instances": "1", | ||
"exec_mode": "cluster", | ||
"max_restarts": "10", | ||
"error_file": "./logs/err.log", | ||
"out_file": "./logs/out.log", | ||
"log_date_format": "YYYY-MM-DD HH:mm:ss Z", | ||
"watch": ["dist", "config"] | ||
} | ||
] | ||
} | ||
{ | ||
"apps": [ | ||
{ | ||
"name": "summer", | ||
"script": "./dist/main.js", | ||
"cwd": "./", | ||
"instances": "2", | ||
"exec_mode": "cluster", | ||
"max_restarts": "10", | ||
"error_file": "./logs/err.log", | ||
"out_file": "./logs/out.log", | ||
"log_date_format": "YYYY-MM-DD HH:mm:ss Z", | ||
"watch": ["dist", "config"] | ||
} | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
import { ICodeRunBody, IQuestionDto } from '@dtos/code'; | ||
import { Controller, UseGuards, Post, Body, Get, Param } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
import { CodeService } from '@services/code.service'; | ||
import recorder from '@utils/recorder'; | ||
import { sleep } from '@utils/utils'; | ||
import { v4 as uuid } from 'uuid'; | ||
import { ICodeLanguageDto } from '@dtos/code'; | ||
const MAX_WAITING_TIME = 360; | ||
|
||
@UseGuards(AuthGuard('jwt')) | ||
@Controller('code') | ||
export class CodeController { | ||
constructor(private readonly codeService: CodeService) {} | ||
@Post('run/case') | ||
async runByCase(@Body() body: ICodeRunBody): Promise<any> { | ||
const id = uuid(); | ||
let waitTime = 0; | ||
let result = null; | ||
|
||
recorder.add({ | ||
id: id, | ||
fn: async () => await this.runByCaseImplement(body), | ||
}); | ||
|
||
while (!result) { | ||
await sleep(0.2); | ||
result = recorder.get(id); | ||
|
||
waitTime++; | ||
if (waitTime > MAX_WAITING_TIME) { | ||
result = { isSuccess: false, message: '运行超时' }; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
async runByCaseImplement(body: ICodeRunBody) { | ||
const { code, questionId, languageId, once } = body; | ||
|
||
const question = await this.codeService.getQuestion(questionId); | ||
const language = await this.codeService.getLanguage(languageId); | ||
const result = []; | ||
|
||
for (const testcase of question.cases) { | ||
const codeCommand = await this.codeService.prepareRunCommand( | ||
language, | ||
code, | ||
question.entry, | ||
testcase, | ||
); | ||
|
||
const _result = await this.codeService.run( | ||
language, | ||
codeCommand, | ||
language.timeout * 1000, | ||
); | ||
result.push(_result); | ||
|
||
if (once || !_result.isSuccess) { | ||
break; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
@Post('run') | ||
async run(@Body() body: ICodeRunBody): Promise<any> { | ||
const id = uuid(); | ||
let waitTime = 0; | ||
let result = null; | ||
|
||
recorder.add({ | ||
id: id, | ||
fn: async () => await this.runImplement(body), | ||
}); | ||
|
||
while (!result) { | ||
await sleep(0.1); | ||
result = recorder.get(id); | ||
|
||
waitTime++; | ||
if (waitTime > MAX_WAITING_TIME) { | ||
result = { isSuccess: false, data: '执行超时.' }; | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
|
||
async runImplement(body: ICodeRunBody) { | ||
const { code, languageId } = body; | ||
const language = await this.codeService.getLanguage(languageId); | ||
const codeCommand = await this.codeService.prepareCode(language, code); | ||
return await this.codeService.run( | ||
language, | ||
codeCommand, | ||
language.timeout * 1000, | ||
); | ||
} | ||
|
||
@Get('languages') | ||
async getLanguages() { | ||
const data = await this.codeService.getLanguages(); | ||
return data.map((x) => { | ||
return { | ||
id: x.id, | ||
name: x.name, | ||
initialCode: x.initialCode, | ||
version: x.version, | ||
} as ICodeLanguageDto; | ||
}); | ||
} | ||
|
||
@Get('question/:questionId') | ||
async getQuestion(@Param('questionId') questionId: string) { | ||
const data = await this.codeService.getQuestion(questionId); | ||
return { | ||
id: data.id, | ||
name: data.name, | ||
desribe: data.describe, | ||
code: data.code, | ||
} as IQuestionDto; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export interface ICodeRunBody { | ||
once?: boolean; | ||
code: string; | ||
questionId: string; | ||
languageId: number; | ||
} | ||
|
||
export interface ICodeLanguageDto { | ||
id: number; | ||
name: string; | ||
initialCode: string; | ||
version: string; | ||
} | ||
|
||
export interface IQuestionDto { | ||
id: string; | ||
name: string; | ||
desribe: string; | ||
code: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,5 @@ | ||
export * from './data.department.entity'; | ||
export * from './data.permission.entity'; | ||
export * from './timesheet.enetity'; | ||
export * from './language.entity'; | ||
export * from './questionBank.entity'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { Column, Entity, PrimaryColumn } from 'typeorm'; | ||
|
||
@Entity({ name: 'language' }) | ||
export class Language { | ||
@PrimaryColumn({ generated: 'increment' }) | ||
id: number; | ||
@Column('varchar', { unique: true, length: 30 }) | ||
name: string; // 语言名称 node | ||
@Column('varchar', { length: 20 }) | ||
version: string; // 语言版本 v16.13.1 | ||
@Column('varchar', { length: 50 }) | ||
iamge: string; // 镜像名 node | ||
@Column('varchar', { length: 50 }) | ||
file: string; // 保存文件名 index.js | ||
@Column('varchar', { length: 50 }) | ||
cmd: string; // 运行代码的命令 node index.js | ||
@Column('smallint') | ||
timeout: number; // 运行超时时间 秒 60 | ||
@Column('smallint') | ||
memory: number; // 分配容器内存大小 MB 100 | ||
@Column('varchar', { length: 10 }) | ||
cpuset: string; // 允许容器使用CPU核心 0-4 或者 1,2,4 | ||
@Column('text') | ||
captureCode: string; // 数据收集代码 运行时长,占用内存等 | ||
@Column('text', { nullable: true }) | ||
initialCode: string; // 初始代码 | ||
@Column('timestamp') | ||
craeteTime: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { Column, Entity } from 'typeorm'; | ||
|
||
@Entity({ name: 'question_bank' }) | ||
export class QuestionBank { | ||
@Column({ primary: true, generated: 'uuid' }) | ||
id: string; | ||
@Column('varchar', { length: 100 }) | ||
name: string; // 题目名称 | ||
@Column('varchar', { length: 50 }) | ||
entry: string; // 入口方法 | ||
@Column('text') | ||
code: string; // 默认填充代码 | ||
@Column('text') | ||
describe?: string; // 题目描述 | ||
@Column('jsonb', { array: false, default: () => "'[]'", nullable: true }) | ||
cases?: IQuestionCase[]; // 测试cases | ||
@Column('jsonb', { array: false, default: () => "'[]'", nullable: true }) | ||
contributor?: string[]; // 贡献者 可以多个 | ||
@Column('date') | ||
createTime: string; | ||
} | ||
|
||
export class IQuestionCase { | ||
case: any; | ||
assert: any; | ||
} |
Oops, something went wrong.