-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.ts
More file actions
127 lines (119 loc) · 2.8 KB
/
process.ts
File metadata and controls
127 lines (119 loc) · 2.8 KB
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
123
124
125
126
127
import * as child from "child_process";
import crypto from "crypto";
import fs from "fs";
import path from "path";
import {
ContainerInitialization,
JobStatus,
container,
fileFormat,
} from "./types/process";
class Process {
constructor() {}
private _fileFormats: Record<container, fileFormat> = {
python3: "py",
javascript: "js",
};
/**
* Creates an appropriate docker container
* to execute the target code
*/
createContainer(container: container): Promise<ContainerInitialization> {
return new Promise((resolve, reject) => {
const initCommand = `docker create ${container}`;
child.exec(initCommand, (error, containerID, stderr) => {
if (error) {
reject(error.message);
} else if (stderr) {
reject(stderr);
} else {
resolve(containerID);
}
});
});
}
/**
* writes a code into a temp file
*/
async writeFile(container: container, context: string): Promise<string> {
return new Promise((resolve, reject) => {
const fileName = crypto.randomBytes(32).toString("hex");
const filePath = path.join(
__dirname,
"..",
"temp",
`${fileName}.${this._fileFormats[container]}`
);
fs.writeFile(filePath, context, (error) => {
if (error) {
reject(error.message);
} else {
resolve(filePath);
}
});
});
}
/**
* copies the target code into the newly created
* isolated container
*/
copyContext(
containerID: string,
container: container,
context: string
): Promise<string> {
return new Promise(async (resolve, reject) => {
this.writeFile(container, context)
.then((filePath) => {
const initCommand = `docker cp ${filePath} ${containerID}:/src`;
console.log(filePath);
child.exec(initCommand, (error, containerID, stderr) => {
if (error) {
reject(error.message);
} else if (stderr) {
reject(stderr);
} else {
resolve(containerID.trim());
}
});
})
.catch((e) => {
reject(e);
});
});
}
/**
* Excecutes phase1
* 1] Creates a new container
* 2] Copes the code into the contaienr
*/
initContainer(container: container, context: string): Promise<JobStatus> {
return new Promise(async (resolve, reject) => {
this.createContainer(container)
.then(async (containerID) => {
return this.copyContext(containerID, container, context)
.then(() => {
resolve({
message: `Job has succedded.`,
jobFailed: false,
retryable: true,
context: context,
containerID: containerID,
});
})
.catch((e) => {
throw new Error(e);
});
})
.catch((e) => {
reject({
message: `Job has failed for the following reason(s): ${e}.`,
jobFailed: true,
retryable: true,
context: context,
});
});
});
}
}
export default Process;