-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
126 lines (99 loc) · 3.58 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
const tar = require('tar-stream');
const fs = require('fs');
const commandLineArgs = require('command-line-args');
/**
* Command line arguments definition
* @type {[*]}
*/
const commandLineDef = [
{ name: 'port', alias: 'p', type: Number, defaultValue: 5016},
{ name: 'mongoAddress', alias: 'a', type: String, defaultValue: '127.0.0.1/queue'},
{ name: 'mongoCollection', alias: 'c', type: String, defaultValue: 'agendaJobs'},
{ name: 'defaultConcurrency', alias: 'd', type: Number, defaultValue: 40},
{ name: 'maxConcurrency', alias: 'm', type: Number, defaultValue: 70}
];
/**
* Object that for keys has command line argument names
* and for values its value.
* @type {Object}
*/
const commandLine = commandLineArgs(commandLineDef);
/**
* Server specified in server.js
* @type {Server}
*/
const server = require('./server')(
commandLine.port,
commandLine.mongoAddress,
commandLine.mongoCollection,
commandLine.defaultConcurrency,
commandLine.maxConcurrency);
/**
* Object controlling docker specified in javaBox.js
* @type {EventEmitter}
*/
const javaBox = require('./javaBox');
/**
* Specifying the server and javaBox control flow
*/
server.on('runJunit', runJunit);
server.on('runJava', runJava);
javaBox.on('result', giveFeedBack);
/**
* Given code to execute and info about it,
* creates a tar with the code and passed the info and the tar to javaBox.
*
* @param messageId {String}: id of the given message/request.
* @param main {String}: entry point class name.
* @param files [{name: {String}, data: {String]: array of objects with filename and its content
* @param timeLimitCompileMs {Number}: Compilation timeout.
* @param timeLimitExecutionMs {Number}: Execution timeout.
*/
function runJava(messageId, main, files, timeLimitCompileMs, timeLimitExecutionMs) {
let filesToAdd = files.length;
const pack = tar.pack();
const tryTarAndRun = () => {
filesToAdd--;
if (filesToAdd === 0) {
const tarBuffer = pack.read();
javaBox.emit('runJava', messageId, main, tarBuffer, timeLimitCompileMs, timeLimitExecutionMs);
}
};
files.forEach((file) => {
pack.entry({ name: file.name }, file.data, tryTarAndRun);
});
}
/**
* Given code to execute and to test and info about it,
* creates a tar with the code and passed the info and the tar to javaBox.
*
* @param messageId {String}: id of the given message/request.
* @param tests [{name: {String}, data: {String]: array of objects with filename and its content.
* @param files [{name: {String}, data: {String]: array of objects with filename and its content.
* @param timeLimitCompileMs {Number}: Compilation timeout.
* @param timeLimitExecutionMs {Number}: Execution timeout.
*/
function runJunit(messageId, tests, files, timeLimitCompileMs, timeLimitExecutionMs) {
let filesToAdd = files.length + tests.length;
const pack = tar.pack();
const tryTarAndRun = () => {
filesToAdd--;
if (filesToAdd === 0) {
const tarBuffer = pack.read();
javaBox.emit('runJunit', messageId, tests, tarBuffer, timeLimitCompileMs, timeLimitExecutionMs);
}
};
files.forEach((file) => {
pack.entry({ name: file.name }, file.data, tryTarAndRun);
});
tests.forEach((test) => {
pack.entry({ name: test.name }, test.data, tryTarAndRun);
});
}
/**
* Given the feedback object passes it to the server.
* @param feedback {Object}, feedback/result object as specified in readme or javaBox.js
*/
function giveFeedBack(feedback) {
server.emit('result', feedback);
}