Skip to content

Commit df55c19

Browse files
committed
integrating javascript compiler
1 parent 2f621f3 commit df55c19

File tree

1 file changed

+81
-9
lines changed

1 file changed

+81
-9
lines changed

Compiler_Engine/worker-js/index.js

Lines changed: 81 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,10 +71,67 @@
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+
74131
import { Worker } from 'bullmq';
75132
import IORedis from 'ioredis';
76133
import fs from 'fs';
77-
import { spawnSync } from 'child_process';
134+
import { spawnSync, spawn } from 'child_process';
78135
import { randomUUID } from 'crypto';
79136

80137
const 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
104164
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-
}
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

114184
new Worker(
@@ -126,3 +196,5 @@ new Worker(
126196
},
127197
{ connection }
128198
);
199+
200+
console.log('Worker started and waiting for jobs...');

0 commit comments

Comments
 (0)