@@ -4,6 +4,7 @@ import fs from "fs";
44import path from "path" ;
55import {
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}
0 commit comments