Skip to content

Commit 6d2e6f2

Browse files
committed
using single execute function
1 parent dd68903 commit 6d2e6f2

18 files changed

+120
-90
lines changed

src/taskmaster.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ const nr = require('newrelic');
22
import * as Raven from 'raven'
33
import * as amqp from 'amqplib/callback_api'
44
import {Connection} from 'amqplib/callback_api'
5-
import { runExecutor, submissionExecutor } from './tasks'
5+
import { execute } from './tasks'
66
import config = require('../config.js')
7+
import { SubmitJob, RunJob } from 'tasks/job';
78

89
// =============== Setup Raven
910
Raven.config(config.SENTRY.DSN, {
@@ -25,13 +26,14 @@ amqp.connect(`amqp://${config.AMQP.USER}:${config.AMQP.PASS}@${config.AMQP.HOST}
2526
channel.assertQueue(jobQ);
2627
channel.consume(jobQ, async (msg) => {
2728
try {
28-
const job = JSON.parse(msg.content.toString())
29-
let jobResult
30-
if (job.testcases) {
31-
jobResult = await submissionExecutor(job)
29+
const payload = JSON.parse(msg.content.toString())
30+
let job
31+
if (payload.testcases) {
32+
job = new SubmitJob(payload)
3233
} else {
33-
jobResult = await runExecutor(job)
34+
job = new RunJob(payload)
3435
}
36+
const jobResult = await execute(job)
3537

3638
// TODO
3739
channel.sendToQueue(successQ, (new Buffer(JSON.stringify(jobResult))))

src/tasks/index.ts

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
1-
import { RunJob, SubmissionJob, Job } from "types/job";
2-
import { RunResult, SubmissionResult, Result } from 'types/result'
1+
import { Result, RunResult, SubmissionResult } from 'types/result'
32
import config = require('../../config.js')
43
import {exec, mkdir, rm} from 'shelljs'
54
import * as path from 'path'
65

76
import RunScenario from './run'
87
import SubmissionScenario from './submission'
9-
import { Scenario } from "types/scenario";
8+
import { RunJob, SubmitJob } from "./job";
109

11-
export const executor = <J, R>(scenario: Scenario) => async (job: Job & J): Promise<Result & R> => {
10+
export function execute(job: RunJob): Promise<RunResult>
11+
export function execute(job: SubmitJob): Promise<SubmissionResult>
12+
export async function execute (job) {
1213
// Create RUNBOX
1314
rm('-rf', config.RUNBOX.DIR)
1415
mkdir('-p', config.RUNBOX.DIR)
@@ -17,6 +18,13 @@ export const executor = <J, R>(scenario: Scenario) => async (job: Job & J): Prom
1718

1819
const LANG_CONFIG = config.LANGS[job.lang]
1920

21+
let scenario
22+
if (job instanceof RunJob) {
23+
scenario = RunScenario
24+
} else if (job instanceof SubmitJob) {
25+
scenario = SubmissionScenario
26+
}
27+
2028
// Setup RUNBOX
2129
await scenario.setup(currentJobDir, job) // TODO:
2230

@@ -33,12 +41,9 @@ export const executor = <J, R>(scenario: Scenario) => async (job: Job & J): Prom
3341
`)
3442

3543
// Get result
36-
const result = <Result & R>(await scenario.result(currentJobDir, job.id))
44+
const result = await scenario.result(currentJobDir, job.id)
3745

3846
rm('-rf', currentJobDir)
3947

4048
return result
4149
}
42-
43-
export const runExecutor = executor<RunJob, RunResult>(RunScenario)
44-
export const submissionExecutor = executor<SubmissionJob, SubmissionResult>(SubmissionScenario)

src/tasks/job.ts

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
type JobConstructorOpts = {
2+
id: number
3+
source: string
4+
lang: string
5+
timelimit?: number
6+
}
7+
type RunJobConstructorOpts = JobConstructorOpts & {
8+
stdin: string
9+
}
10+
type TestcaseOpts = {
11+
id: number,
12+
input: string,
13+
output: string
14+
}
15+
type SubmitJobConstructorOpts = JobConstructorOpts & {
16+
testcases: Array<TestcaseOpts>
17+
}
18+
19+
export class Job {
20+
id: number
21+
source: string
22+
lang: string
23+
timelimit?: number
24+
25+
constructor({ id, source, lang, timelimit = 5 }: JobConstructorOpts) {
26+
this.id = id
27+
this.source = source
28+
this.lang = lang
29+
this.timelimit = timelimit
30+
}
31+
}
32+
33+
export class RunJob extends Job {
34+
stdin: string
35+
36+
constructor({ id, source, lang, timelimit, stdin }: RunJobConstructorOpts) {
37+
super({id, source, lang, timelimit})
38+
this.stdin = stdin
39+
}
40+
}
41+
42+
export class SubmitJob extends Job {
43+
testcases: Array<TestcaseOpts>
44+
45+
constructor({ id, source, lang, timelimit, testcases }: SubmitJobConstructorOpts) {
46+
super({id, source, lang, timelimit})
47+
this.testcases = testcases
48+
}
49+
}

src/tasks/run.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import config = require('../../config.js')
22
import {cat, exec, mkdir, rm, touch, head} from 'shelljs'
3-
import { RunJob } from 'types/job'
3+
import { RunJob } from './job'
44
import { RunResult } from 'types/result'
55
import * as path from 'path'
66
import * as fs from 'fs'

src/tasks/submission.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import config = require('../../config.js')
22
import { cat, ls, mkdir, exec } from 'shelljs'
3-
import { SubmissionJob } from 'types/job'
3+
import { SubmitJob } from './job'
44
import { SubmissionResult } from 'types/result'
55
import * as path from 'path'
66
import * as fs from 'fs'
@@ -23,7 +23,7 @@ export const download = (url: string, dest: string): Promise<ClientRequest> => {
2323
}
2424

2525
class SubmissionScenario implements Scenario {
26-
setup(currentJobDir: string, job: SubmissionJob) {
26+
setup(currentJobDir: string, job: SubmitJob) {
2727
const LANG_CONFIG = config.LANGS[job.lang]
2828

2929
fs.writeFileSync(path.join(currentJobDir, LANG_CONFIG.SOURCE_FILE),

src/types/job.d.ts

Lines changed: 0 additions & 20 deletions
This file was deleted.

src/types/result.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
1-
export interface Result {
1+
export type Result = {
22
id: number,
33
stderr: string
44
}
55

6-
export interface TestcaseResult {
6+
export type TestcaseResult = {
77
id: number,
88
score: number,
99
time: string,
1010
result: string
1111
}
1212

13-
export interface RunResult extends Result {
13+
export type RunResult = Result & {
1414
stdout: string,
1515
time: number,
1616
code: number
1717
}
1818

19-
export interface SubmissionResult extends Result {
19+
export type SubmissionResult = Result & {
2020
testcases: Array<TestcaseResult>
2121
}

src/types/scenario.d.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Job, Result } from "./job";
1+
import { Job } from '../tasks/job';
2+
import { Result } from './result';
23

34
export interface Scenario {
45
setup(currentJobDir: string, job: Job)

test/run/run.c.spec.ts

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import {runExecutor} from '../../src/tasks/'
1+
import {execute} from '../../src/tasks/'
22
import {expect} from 'chai'
3-
4-
3+
import { RunJob } from '../../src/tasks/job'
54
describe('run - c', () => {
65
it('.c file runs correctly', async () => {
7-
const runResult = await runExecutor({
6+
const runResult = await execute(new RunJob({
87
id: 19,
98
lang: 'c',
109
source: (new Buffer(`
1110
#include <stdio.h>
12-
1311
int main () {
1412
char in[10];
1513
scanf("%s", in);
@@ -18,7 +16,7 @@ int main () {
1816
}
1917
`)).toString('base64'),
2018
stdin: (new Buffer('World')).toString('base64')
21-
})
19+
}))
2220
expect(new Buffer(runResult.stdout, 'base64').toString('ascii')).to.eq('Hello World')
2321
})
2422
})

test/run/run.cpp.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import {runExecutor} from '../../src/tasks/'
1+
import {execute} from '../../src/tasks/'
22
import {expect} from 'chai'
3-
3+
import { RunJob } from '../../src/tasks/job'
44

55
describe('run - cpp', () => {
66
it('.cpp file runs correctly', async () => {
7-
const runResult = await runExecutor({
7+
const runResult = await execute(new RunJob({
88
id: 20,
99
lang: 'cpp',
1010
source: (new Buffer(`
@@ -18,7 +18,7 @@ int main () {
1818
}
1919
`)).toString('base64'),
2020
stdin: (new Buffer('World')).toString('base64')
21-
})
21+
}))
2222
expect(new Buffer(runResult.stdout, 'base64').toString('ascii')).to.eq('Hello World')
2323
})
2424
})

0 commit comments

Comments
 (0)