Skip to content

Commit 5385845

Browse files
committed
worker WIP
1 parent 77c1009 commit 5385845

7 files changed

+64
-21
lines changed

dockerfiles/python3.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
FROM python:3.8-slim-buster
22
WORKDIR /src
33
# COPY ./test.py /src
4-
# CMD ["python3", "test.py"]
4+
CMD ["python3", "target.py"]

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import Process from "./process";
1+
import JobWorker from "./workder";
22

3-
const process = new Process();
4-
process
5-
.initContainer("python3", "print('Hi')")
6-
.then(() => console.log("OK"))
3+
const workder = new JobWorker();
4+
workder
5+
.startContainer("python3", "print(10+898786)")
6+
.then((output) => console.log(output))
77
.catch((e) => console.log(e));
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,18 @@ export type container = "python3" | "javascript";
22
export type fileFormat = "py" | "js";
33
export type ContainerInitialization = string;
44

5+
export interface WriteFileStatus {
6+
/**
7+
* Newly created temp file that contain the context if exists
8+
*/
9+
filePath: string;
10+
11+
/**
12+
* The file format of the created file
13+
*/
14+
fileFormat: string;
15+
}
16+
517
export interface JobStatus {
618
/**
719
* A brief message that describes that job status

src/process.ts renamed to src/workder.ts

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import path from "path";
55
import {
66
ContainerInitialization,
77
JobStatus,
8+
WriteFileStatus,
89
container,
910
fileFormat,
10-
} from "./types/process";
11+
} from "./types/workder";
1112

12-
class Process {
13+
class JobWorker {
1314
constructor() {}
1415

1516
private _fileFormats: Record<container, fileFormat> = {
@@ -30,7 +31,7 @@ class Process {
3031
} else if (stderr) {
3132
reject(stderr);
3233
} else {
33-
resolve(containerID);
34+
resolve(containerID.trim());
3435
}
3536
});
3637
});
@@ -39,21 +40,26 @@ class Process {
3940
/**
4041
* writes a code into a temp file
4142
*/
42-
async writeFile(container: container, context: string): Promise<string> {
43+
44+
async writeFile(
45+
container: container,
46+
context: string
47+
): Promise<WriteFileStatus> {
4348
return new Promise((resolve, reject) => {
4449
const fileName = crypto.randomBytes(32).toString("hex");
50+
const fileFormat = this._fileFormats[container];
4551
const filePath = path.join(
4652
__dirname,
4753
"..",
4854
"temp",
49-
`${fileName}.${this._fileFormats[container]}`
55+
`${fileName}.${fileFormat}`
5056
);
5157

5258
fs.writeFile(filePath, context, (error) => {
5359
if (error) {
5460
reject(error.message);
5561
} else {
56-
resolve(filePath);
62+
resolve({ filePath, fileFormat });
5763
}
5864
});
5965
});
@@ -70,9 +76,8 @@ class Process {
7076
): Promise<string> {
7177
return new Promise(async (resolve, reject) => {
7278
this.writeFile(container, context)
73-
.then((filePath) => {
74-
const initCommand = `docker cp ${filePath} ${containerID}:/src`;
75-
console.log(filePath);
79+
.then(({ filePath, fileFormat }) => {
80+
const initCommand = `docker cp ${filePath} ${containerID}:/src/target.${fileFormat}`;
7681
child.exec(initCommand, (error, containerID, stderr) => {
7782
if (error) {
7883
reject(error.message);
@@ -90,9 +95,9 @@ class Process {
9095
}
9196

9297
/**
93-
* Excecutes phase1
98+
* Excecutes phase 1
9499
* 1] Creates a new container
95-
* 2] Copes the code into the contaienr
100+
* 2] Copies the code into the contaienr
96101
*/
97102
initContainer(container: container, context: string): Promise<JobStatus> {
98103
return new Promise(async (resolve, reject) => {
@@ -122,6 +127,35 @@ class Process {
122127
});
123128
});
124129
}
130+
131+
/**
132+
* Executes phase 2
133+
* 1] Spin up the container
134+
* 2] Record the output from the container
135+
*/
136+
startContainer(container: container, context: string): Promise<string> {
137+
return new Promise((resolve, reject) => {
138+
this.initContainer(container, context)
139+
.then((jobStatus: JobStatus) => {
140+
if (!jobStatus.jobFailed && jobStatus.containerID) {
141+
const startContainer = `docker start -a ${jobStatus.containerID}`;
142+
child.exec(startContainer, (error, stdout, stderr) => {
143+
if (error) {
144+
reject(error.message);
145+
} else if (stderr) {
146+
reject(stderr);
147+
} else {
148+
resolve(stdout);
149+
}
150+
});
151+
} else {
152+
// job failed
153+
reject();
154+
}
155+
})
156+
.catch((e) => console.log(e));
157+
});
158+
}
125159
}
126160

127-
export default Process;
161+
export default JobWorker;

temp/5c825032977a4b67d79ceac8dd24f72f5feb09431b6b35f1e9dd052335b0e2e5.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

temp/60f1f0ec0277413a86bb31656c6ccc024f6495c31262fd43b7749d3522da08a4.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

temp/7ca84fa45e083f4d019a6f6df4bc00dad509dc17bb12c591fd9d8094bb57ebea.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)