@@ -3,28 +3,30 @@ import crypto from "crypto";
33import fs from "fs" ;
44import path from "path" ;
55import {
6+ CodeContext ,
67 ContainerInitialization ,
78 JobStatus ,
89 WriteFileStatus ,
9- container ,
10+ language ,
1011 fileFormat ,
1112} from "./types/worker" ;
13+ import { mainClassName } from "./config" ;
1214
1315class JobWorker {
1416 constructor ( ) { }
1517
16- private _fileFormats : Record < container , fileFormat > = {
18+ private _fileFormats : Record < language , fileFormat > = {
1719 python3 : "py" ,
1820 javascript : "js" ,
1921 } ;
2022 /**
2123 * Creates an appropriate docker container
2224 * to execute the target code
2325 */
24- createContainer ( container : container ) : Promise < ContainerInitialization > {
26+ createContainer ( language : language ) : Promise < ContainerInitialization > {
2527 return new Promise ( ( resolve , reject ) => {
26- const initCommand = `docker create ${ container } ` ;
27- if ( ! ( container in this . _fileFormats ) ) {
28+ const initCommand = `docker create ${ language } ` ;
29+ if ( ! ( language in this . _fileFormats ) ) {
2830 return reject ( {
2931 error : true ,
3032 errorMessage : "Invalid container name." ,
@@ -53,24 +55,38 @@ class JobWorker {
5355 }
5456
5557 /**
56- * writes a code into a temp file
58+ * Returns a piece of code that executes a function in a class
59+ * language specific, so defined using switch cases
5760 */
61+ transformCodeIntoExecutable ( language : language , context : CodeContext ) {
62+ switch ( language ) {
63+ case "python3" : {
64+ return `\n${ mainClassName } ().${ context . functionName } ()`
65+ }
66+ }
67+ }
5868
69+ /**
70+ * writes a code into a temp file
71+ */
5972 private async writeFile (
60- container : container ,
61- context : string
73+ language : language ,
74+ context : CodeContext
6275 ) : Promise < WriteFileStatus > {
6376 return new Promise ( ( resolve , reject ) => {
6477 const fileName = crypto . randomBytes ( 32 ) . toString ( "hex" ) ;
65- const fileFormat = this . _fileFormats [ container ] ;
78+ const fileFormat = this . _fileFormats [ language ] ;
6679 const filePath = path . join (
6780 __dirname ,
6881 ".." ,
6982 "temp" ,
7083 `${ fileName } .${ fileFormat } `
7184 ) ;
7285
73- fs . writeFile ( filePath , context , ( error ) => {
86+ // appending a line to execute a specific function
87+ context . code += this . transformCodeIntoExecutable ( language , context )
88+
89+ fs . writeFile ( filePath , context . code , ( error ) => {
7490 if ( error ) {
7591 reject ( error . message ) ;
7692 } else {
@@ -104,11 +120,11 @@ class JobWorker {
104120 */
105121 copyContext (
106122 containerID : string ,
107- container : container ,
108- context : string
123+ language : language ,
124+ context : CodeContext
109125 ) : Promise < string > {
110126 return new Promise ( async ( resolve , reject ) => {
111- this . writeFile ( container , context )
127+ this . writeFile ( language , context )
112128 . then ( ( { filePath, fileFormat } ) => {
113129 const initCommand = `docker cp ${ filePath } ${ containerID } :/src/target.${ fileFormat } ` ;
114130 child . exec ( initCommand , ( error , containerID , stderr ) => {
@@ -132,12 +148,12 @@ class JobWorker {
132148 * 1] Creates a new container
133149 * 2] Copies the code into the contaienr
134150 */
135- initContainer ( container : container , context : string ) : Promise < JobStatus > {
151+ initContainer ( language : language , context : CodeContext ) : Promise < JobStatus > {
136152 return new Promise ( async ( resolve , reject ) => {
137- this . createContainer ( container )
153+ this . createContainer ( language )
138154 . then ( async ( { containerID, error, errorMessage } ) => {
139155 if ( error ) return new Error ( errorMessage ) ;
140- return this . copyContext ( containerID , container , context )
156+ return this . copyContext ( containerID , language , context )
141157 . then ( ( ) => {
142158 resolve ( {
143159 message : `Job has succedded.` ,
@@ -167,9 +183,9 @@ class JobWorker {
167183 * 1] Spin up the container
168184 * 2] Record the output from the container
169185 */
170- startContainer ( container : container , context : string ) : Promise < string > {
186+ startContainer ( language : language , context : CodeContext ) : Promise < string > {
171187 return new Promise ( ( resolve , reject ) => {
172- this . initContainer ( container , context )
188+ this . initContainer ( language , context )
173189 . then ( ( jobStatus : JobStatus ) => {
174190 if ( ! jobStatus . jobFailed && jobStatus . containerID ) {
175191 const startContainer = `docker start -a ${ jobStatus . containerID } ` ;
0 commit comments