@@ -5,11 +5,12 @@ import path from "path";
55import {
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 ;
0 commit comments