99 WriteFileStatus ,
1010 language ,
1111 fileFormat ,
12+ ExecuteContainer ,
13+ Stdout ,
1214} from "./types/worker" ;
1315import { mainClassName } from "./config" ;
1416
@@ -19,6 +21,7 @@ class JobWorker {
1921 python3 : "py" ,
2022 javascript : "js" ,
2123 } ;
24+
2225 /**
2326 * Creates an appropriate docker container
2427 * to execute the target code
@@ -55,14 +58,15 @@ class JobWorker {
5558 }
5659
5760 /**
58- * Returns a piece of code that executes a function in a class
59- * language specific, so defined using switch cases
61+ * Returns a piece of code that executes the class method
62+ * this is language specific, so defined using switch cases
6063 */
61- transformCodeIntoExecutable ( language : language , context : CodeContext ) {
64+ transformCodeIntoExecutable ( language : language , context : CodeContext ) : string {
6265 switch ( language ) {
6366 case "python3" : {
64- return `\n ${ mainClassName } ().${ context . functionName } ()`
67+ return `\nprint( ${ mainClassName } ().${ context . functionName } () )`
6568 }
69+ default : return ""
6670 }
6771 }
6872
@@ -99,7 +103,7 @@ class JobWorker {
99103 /**
100104 * Removes a container with the provided containerID
101105 */
102- removeContainer ( containerID : string ) : Promise < string > {
106+ removeContainer ( containerID : string ) : Promise < Stdout > {
103107 return new Promise ( ( resolve , reject ) => {
104108 const removeContainer = `docker rm --force ${ containerID } ` ;
105109 child . exec ( removeContainer , ( error , stdout , stderr ) => {
@@ -157,7 +161,7 @@ class JobWorker {
157161 . then ( ( ) => {
158162 resolve ( {
159163 message : `Job has succedded.` ,
160- jobFailed : false ,
164+ error : false ,
161165 retryable : true ,
162166 context : context ,
163167 containerID : containerID ,
@@ -183,24 +187,37 @@ class JobWorker {
183187 * 1] Spin up the container
184188 * 2] Record the output from the container
185189 */
186- startContainer ( language : language , context : CodeContext ) : Promise < string > {
190+ startContainer ( language : language , context : CodeContext ) : Promise < ExecuteContainer > {
187191 return new Promise ( ( resolve , reject ) => {
188192 this . initContainer ( language , context )
189193 . then ( ( jobStatus : JobStatus ) => {
190- if ( ! jobStatus . jobFailed && jobStatus . containerID ) {
194+ if ( ! jobStatus . error && jobStatus . containerID ) {
191195 const startContainer = `docker start -a ${ jobStatus . containerID } ` ;
192196 child . exec ( startContainer , ( error , stdout , stderr ) => {
193197 if ( error ) {
194- reject ( error . message ) ;
198+ reject ( {
199+ error : true ,
200+ errorMessage : error . message
201+ } as ExecuteContainer ) ;
195202 } else if ( stderr ) {
196- reject ( stderr ) ;
203+ reject ( {
204+ error : true ,
205+ errorMessage : stderr
206+ } as ExecuteContainer ) ;
197207 } else {
198- resolve ( stdout . trim ( ) ) ;
208+ resolve ( {
209+ error : false ,
210+ codeOutput : stdout . trim ( ) ,
211+ containerID : jobStatus . containerID
212+ } as ExecuteContainer ) ;
199213 }
200214 } ) ;
201215 } else {
202216 // job failed
203- reject ( ) ;
217+ reject ( {
218+ error : true ,
219+ errorMessage : `Job has failed in the process of initializing the container for the following reason(s): ${ jobStatus . message } `
220+ } as ExecuteContainer ) ;
204221 }
205222 } )
206223 . catch ( ( e ) => console . log ( e ) ) ;
0 commit comments