7171// );
7272
7373
74+ // import { Worker } from 'bullmq';
75+ // import IORedis from 'ioredis';
76+ // import fs from 'fs';
77+ // import { spawnSync } from 'child_process';
78+ // import { randomUUID } from 'crypto';
79+
80+ // const SUPPORTED_LANGUAGES = ['javascript', 'python'];
81+ // const QUEUE_NAME = 'code-execution';
82+
83+ // const connection = new IORedis('redis://redis:6379', {
84+ // maxRetriesPerRequest: null,
85+ // });
86+
87+ // // Helper: Run Python code
88+ // function runPython(code, input) {
89+ // const filename = `temp_${randomUUID()}.py`;
90+ // fs.writeFileSync(filename, code);
91+ // const result = spawnSync('python3', [filename], {
92+ // input: input || '',
93+ // encoding: 'utf-8',
94+ // timeout: 10000
95+ // });
96+ // fs.unlinkSync(filename);
97+
98+ // if (result.error) return { success: false, error: result.error.message };
99+ // if (result.status !== 0) return { success: false, error: result.stderr.trim() || 'Non-zero exit code' };
100+ // return { success: true, output: result.stdout.trim() };
101+ // }
102+
103+ // // Helper: Run JS code
104+ // function runJS(code, input) {
105+ // try {
106+ // const fn = new Function('input', code);
107+ // const output = fn(input);
108+ // return { success: true, output: output?.toString() };
109+ // } catch (err) {
110+ // return { success: false, error: err.message };
111+ // }
112+ // }
113+
114+ // new Worker(
115+ // QUEUE_NAME,
116+ // async (job) => {
117+ // const { language, code, input } = job.data;
118+ // console.log(`Processing job ${job.id} for language: ${language}`);
119+
120+ // if (!SUPPORTED_LANGUAGES.includes(language)) {
121+ // return { success: false, error: `Unsupported language: ${language}` };
122+ // }
123+
124+ // if (language === 'javascript') return runJS(code, input);
125+ // if (language === 'python') return runPython(code, input);
126+ // },
127+ // { connection }
128+ // );
129+
130+
74131import { Worker } from 'bullmq' ;
75132import IORedis from 'ioredis' ;
76133import fs from 'fs' ;
77- import { spawnSync } from 'child_process' ;
134+ import { spawnSync , spawn } from 'child_process' ;
78135import { randomUUID } from 'crypto' ;
79136
80137const SUPPORTED_LANGUAGES = [ 'javascript' , 'python' ] ;
@@ -97,18 +154,31 @@ function runPython(code, input) {
97154
98155 if ( result . error ) return { success : false , error : result . error . message } ;
99156 if ( result . status !== 0 ) return { success : false , error : result . stderr . trim ( ) || 'Non-zero exit code' } ;
157+
158+ // Print output to container logs
159+ if ( result . stdout . trim ( ) ) console . log ( result . stdout . trim ( ) ) ;
100160 return { success : true , output : result . stdout . trim ( ) } ;
101161}
102162
103- // Helper: Run JS code
163+ // Helper: Run JS code safely and show console.log output
104164function runJS ( code , input ) {
105- try {
106- const fn = new Function ( 'input' , code ) ;
107- const output = fn ( input ) ;
108- return { success : true , output : output ?. toString ( ) } ;
109- } catch ( err ) {
110- return { success : false , error : err . message } ;
111- }
165+ const filename = `temp_${ randomUUID ( ) } .js` ;
166+ fs . writeFileSync ( filename , code ) ;
167+
168+ // Use spawnSync to run JS file with Node
169+ const result = spawnSync ( 'node' , [ filename ] , {
170+ input : input || '' ,
171+ encoding : 'utf-8' ,
172+ timeout : 10000
173+ } ) ;
174+ fs . unlinkSync ( filename ) ;
175+
176+ if ( result . error ) return { success : false , error : result . error . message } ;
177+ if ( result . status !== 0 ) return { success : false , error : result . stderr . trim ( ) || 'Non-zero exit code' } ;
178+
179+ // Print console output to container logs
180+ if ( result . stdout . trim ( ) ) console . log ( result . stdout . trim ( ) ) ;
181+ return { success : true , output : result . stdout . trim ( ) } ;
112182}
113183
114184new Worker (
@@ -126,3 +196,5 @@ new Worker(
126196 } ,
127197 { connection }
128198) ;
199+
200+ console . log ( 'Worker started and waiting for jobs...' ) ;
0 commit comments