11import Bull from 'bull' ;
22import { v4 as uuid } from 'uuid' ;
33
4- import { CodeParams , Result } from './models/models' ;
4+ import { CodeParams } from './models/models' ;
55import logger from './utils/logger' ;
66import { extension } from './utils/findExtension' ;
77
@@ -10,35 +10,21 @@ const languages = Object.keys(extension);
1010export default class CodeExecutor {
1111 private queue : Bull . Queue ;
1212
13- private jobs : Map < string , { resolve : Function , reject : Function } > ;
14-
1513 constructor ( name : string , redis : string ) {
1614 this . queue = new Bull ( name , redis ) ;
17-
18- this . jobs = new Map ( ) ;
19-
20- this . queue . on ( 'global:completed' , ( _job : Bull . Job , result : string ) => {
21- const { id } = < Result > JSON . parse ( result ) ;
22-
23- logger . debug ( `Running on complete for id: ${ id } ` ) ;
24-
25- const currentJob = this . jobs . get ( id ) ;
26- if ( currentJob ) {
27- currentJob . resolve ( result ) ;
28- this . jobs . delete ( id ) ;
29- }
30- } ) ;
3115 }
3216
3317 async runCode ( codeOptions : CodeParams ) : Promise < void > {
18+ if ( ! languages . includes ( codeOptions . language ) ) {
19+ return Promise . reject ( new Error ( 'Language not supported!' ) ) ;
20+ }
3421 const id = uuid ( ) ;
3522 const codeObject = { ...codeOptions , id } ;
3623 logger . info ( `Running code with id: ${ id } ` ) ;
3724
38- return new Promise ( ( resolve , reject ) => {
39- this . jobs . set ( id , { resolve, reject } ) ;
40- this . queue . add ( codeObject ) ;
41- } ) ;
25+ const job = await this . queue . add ( codeObject ) ;
26+
27+ return job . finished ( ) ;
4228 }
4329
4430 stop ( ) {
0 commit comments