Skip to content

Commit f2934eb

Browse files
Sam GotoSam Goto
authored andcommitted
Process - Phase 1 done: Container initialization & file migration
1 parent 31614b1 commit f2934eb

File tree

4 files changed

+96
-22
lines changed

4 files changed

+96
-22
lines changed

src/index.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,6 @@ import Process from "./process";
22

33
const process = new Process();
44
process
5-
.prepTransfer("print('Hi')", "python3")
6-
.then((containerID) => console.log(containerID))
5+
.initContainer("python3", "print('Hi')")
6+
.then(() => console.log("OK"))
77
.catch((e) => console.log(e));
8-
// process
9-
// .initContainer("python3")
10-
// .then((containerID) => console.log(containerID))
11-
// .catch((e) => console.log(e));

src/process.ts

Lines changed: 62 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import fs from "fs";
44
import path from "path";
55
import {
66
ContainerInitialization,
7+
JobStatus,
78
container,
89
fileFormat,
910
} from "./types/process";
@@ -19,8 +20,8 @@ class Process {
1920
* Creates an appropriate docker container
2021
* to execute the target code
2122
*/
22-
async initContainer(container: container): Promise<ContainerInitialization> {
23-
return new Promise((reject, resolve) => {
23+
createContainer(container: container): Promise<ContainerInitialization> {
24+
return new Promise((resolve, reject) => {
2425
const initCommand = `docker create ${container}`;
2526

2627
child.exec(initCommand, (error, containerID, stderr) => {
@@ -38,8 +39,8 @@ class Process {
3839
/**
3940
* writes a code into a temp file
4041
*/
41-
async prepTransfer(context: string, container: container): Promise<string> {
42-
return new Promise((reject, resolve) => {
42+
async writeFile(container: container, context: string): Promise<string> {
43+
return new Promise((resolve, reject) => {
4344
const fileName = crypto.randomBytes(32).toString("hex");
4445
const filePath = path.join(
4546
__dirname,
@@ -62,18 +63,63 @@ class Process {
6263
* copies the target code into the newly created
6364
* isolated container
6465
*/
65-
async copyContext(containerID: string): Promise<void> {
66-
return new Promise(async (reject, resolve) => {
67-
// const initCommand = `docker cp ${} ${containerID}/src`;
68-
// child.exec(initCommand, (error, containerID, stderr) => {
69-
// if (error) {
70-
// reject(error.message);
71-
// } else if (stderr) {
72-
// reject(stderr);
73-
// } else {
74-
// resolve(containerID);
75-
// }
76-
// });
66+
copyContext(
67+
containerID: string,
68+
container: container,
69+
context: string
70+
): Promise<string> {
71+
return new Promise(async (resolve, reject) => {
72+
this.writeFile(container, context)
73+
.then((filePath) => {
74+
const initCommand = `docker cp ${filePath} ${containerID}:/src`;
75+
console.log(filePath);
76+
child.exec(initCommand, (error, containerID, stderr) => {
77+
if (error) {
78+
reject(error.message);
79+
} else if (stderr) {
80+
reject(stderr);
81+
} else {
82+
resolve(containerID.trim());
83+
}
84+
});
85+
})
86+
.catch((e) => {
87+
reject(e);
88+
});
89+
});
90+
}
91+
92+
/**
93+
* Excecutes phase1
94+
* 1] Creates a new container
95+
* 2] Copes the code into the contaienr
96+
*/
97+
initContainer(container: container, context: string): Promise<JobStatus> {
98+
return new Promise(async (resolve, reject) => {
99+
this.createContainer(container)
100+
.then(async (containerID) => {
101+
return this.copyContext(containerID, container, context)
102+
.then(() => {
103+
resolve({
104+
message: `Job has succedded.`,
105+
jobFailed: false,
106+
retryable: true,
107+
context: context,
108+
containerID: containerID,
109+
});
110+
})
111+
.catch((e) => {
112+
throw new Error(e);
113+
});
114+
})
115+
.catch((e) => {
116+
reject({
117+
message: `Job has failed for the following reason(s): ${e}.`,
118+
jobFailed: true,
119+
retryable: true,
120+
context: context,
121+
});
122+
});
77123
});
78124
}
79125
}

src/types/process.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,35 @@
11
export type container = "python3" | "javascript";
22
export type fileFormat = "py" | "js";
33
export type ContainerInitialization = string;
4+
5+
export interface JobStatus {
6+
/**
7+
* A brief message that describes that job status
8+
*/
9+
message: string;
10+
11+
/**
12+
* Whether the job failed
13+
*/
14+
jobFailed: boolean;
15+
16+
/**
17+
* indicates if the job is retryable
18+
*/
19+
retryable: boolean;
20+
21+
/**
22+
* the raw data of the target code
23+
*/
24+
context: string;
25+
26+
/**
27+
* Newly created ontainer ID if exists
28+
*/
29+
containerID?: string;
30+
31+
/**
32+
* Newly created temp file that contain the context if exists
33+
*/
34+
filePath?: string;
35+
}

temp/32f541a5cdf30200086fcd6bd7d059797e18bf9b51fcbeda37db5c5d990de8a6.py renamed to temp/60f1f0ec0277413a86bb31656c6ccc024f6495c31262fd43b7749d3522da08a4.py

File renamed without changes.

0 commit comments

Comments
 (0)