1+ // import express from 'express';
2+ // import bodyParser from 'body-parser';
3+ // import { Queue, QueueEvents } from 'bullmq';
4+ // import IORedis from 'ioredis';
5+ // import dotenv from 'dotenv';
6+
7+ // dotenv.config();
8+
9+ // const PORT = process.env.PORT | 4000;
10+ // const SUPPORTED_LANGUAGES = ['javascript', 'python'];
11+
12+ // const app = express();
13+ // app.use(bodyParser.json());
14+
15+
16+ // const redisHost = process.env.REDIS_HOST || 'redis';
17+ // const redisPort = process.env.REDIS_PORT || 6379;
18+ // const connection = new IORedis(`redis://${redisHost}:${redisPort}`, {
19+ // maxRetriesPerRequest: null
20+ // });
21+
22+ // const queue = new Queue('code-execution', { connection });
23+ // const queueEvents = new QueueEvents('code-execution', { connection });
24+ // await queueEvents.waitUntilReady();
25+
26+ // //HAVE TO THINK ABOUT THIS BECAUSE PYTHON INTEGRATION IS HARD
27+
28+ // // Dynamically get supported languges from Redis
29+ // // const keys = await connection.keys('lang:*');
30+ // // const activeLanguages = keys.map(k => k.split(':')[1]);
31+
32+ // app.post('/run', async (req, res) => {
33+ // const { language, code, input } = req.body;
34+
35+ // if (!SUPPORTED_LANGUAGES.includes(language)) {
36+ // return res.status(400).json({
37+ // success: false,
38+ // error: `Language "${language}" not supported currently. Available: ${SUPPORTED_LANGUAGES.join(', ')}`
39+ // });
40+ // }
41+
42+ // let jobName;
43+ // switch (language) {
44+ // case 'python':
45+ // jobName = 'runpy';
46+ // break;
47+ // case 'javascript':
48+ // jobName = 'runjs';
49+ // break;
50+ // default:
51+ // break;
52+ // }
53+
54+ // const job = await queue.add(jobName, { language, code, input });
55+
56+ // try {
57+ // const result = await job.waitUntilFinished(queueEvents, 10000);
58+ // res.send(result);
59+ // }
60+ // catch (error) {
61+ // res.status(500).json({ success: false, error: error.message || 'Job failed' });
62+ // }
63+ // });
64+
65+ // app.listen(PORT, () => console.log(`Producer running on port ${PORT}`));
66+
67+
68+
69+ // import express from 'express';
70+ // import { Queue, QueueEvents } from 'bullmq';
71+ // import IORedis from 'ioredis';
72+ // import dotenv from 'dotenv';
73+ // dotenv.config();
74+
75+ // const app = express();
76+ // app.use(express.json());
77+
78+ // // Redis connection for BullMQ (maxRetriesPerRequest must be null)
79+ // const connection = new IORedis('redis://redis:6379', {
80+ // maxRetriesPerRequest: null
81+ // });
82+
83+ // const queue = new Queue('code-execution', { connection });
84+ // const queueEvents = new QueueEvents('code-execution', { connection });
85+
86+ // // API endpoint
87+ // app.post('/run', async (req, res) => {
88+ // const { code, input, language } = req.body;
89+ // try {
90+ // const job = await queue.add('runjs', { code, input, language });
91+ // const result = await job.waitUntilFinished(queueEvents);
92+ // res.json({ jobId: job.id, result });
93+ // } catch (err) {
94+ // res.status(500).json({ error: err.message });
95+ // }
96+ // });
97+
98+ // app.listen(process.env.PORT || 4000, () => {
99+ // console.log('Producer running on port 4000');
100+ // });
101+
102+
1103import express from 'express' ;
2- import bodyParser from 'body-parser' ;
3104import { Queue , QueueEvents } from 'bullmq' ;
4105import IORedis from 'ioredis' ;
5106import dotenv from 'dotenv' ;
6-
7107dotenv . config ( ) ;
8108
9- const PORT = process . env . PORT | 4000 ;
10- const SUPPORTED_LANGUAGES = [ 'javascript' , 'python' ] ;
11-
12109const app = express ( ) ;
13- app . use ( bodyParser . json ( ) ) ;
110+ app . use ( express . json ( ) ) ;
14111
112+ const PORT = process . env . PORT || 4000 ;
15113
16- const redisHost = process . env . REDIS_HOST || 'localhost' ;
17- const redisPort = process . env . REDIS_PORT || 6379 ;
18- const connection = new IORedis ( `redis://${ redisHost } :${ redisPort } ` , {
114+ // Supported languages
115+ const SUPPORTED_LANGUAGES = [ 'javascript' , 'python' ] ;
116+
117+ // Redis connection
118+ const connection = new IORedis ( 'redis://redis:6379' , {
19119 maxRetriesPerRequest : null
20120} ) ;
21121
122+ // BullMQ queue & events
22123const queue = new Queue ( 'code-execution' , { connection } ) ;
23124const queueEvents = new QueueEvents ( 'code-execution' , { connection } ) ;
24125await queueEvents . waitUntilReady ( ) ;
25126
26- //HAVE TO THINK ABOUT THIS BECAUSE PYTHON INTEGRATION IS HARD
27-
28- // Dynamically get supported languges from Redis
29- // const keys = await connection.keys('lang:*');
30- // const activeLanguages = keys.map(k => k.split(':')[1]);
31-
127+ // POST /run endpoint
32128app . post ( '/run' , async ( req , res ) => {
33129 const { language, code, input } = req . body ;
34130
131+ // Validate language
35132 if ( ! SUPPORTED_LANGUAGES . includes ( language ) ) {
36133 return res . status ( 400 ) . json ( {
37134 success : false ,
38- error : `Language "${ language } " not supported currently . Available: ${ SUPPORTED_LANGUAGES . join ( ', ' ) } `
135+ error : `Language "${ language } " not supported. Available: ${ SUPPORTED_LANGUAGES . join ( ', ' ) } `
39136 } ) ;
40137 }
41138
42- let jobName ;
43- switch ( language ) {
44- case 'python' :
45- jobName = 'runpy' ;
46- break ;
47- case 'javascript' :
48- jobName = 'runjs' ;
49- break ;
50- default :
51- break ;
52- }
53-
54- const job = await queue . add ( jobName , { language, code, input } ) ;
139+ // Determine job name based on language
140+ const jobName = language === 'python' ? 'runpy' : 'runjs' ;
55141
56142 try {
57- const result = await job . waitUntilFinished ( queueEvents , 10000 ) ;
58- res . send ( result ) ;
59- }
60- catch ( error ) {
61- res . status ( 500 ) . json ( { success : false , error : error . message || 'Job failed' } ) ;
143+ // Add job to queue
144+ const job = await queue . add ( jobName , { language, code, input } ) ;
145+
146+ // Wait for worker to finish job
147+ const result = await job . waitUntilFinished ( queueEvents , 10000 ) ; // 10 sec timeout
148+
149+ res . json ( {
150+ success : true ,
151+ jobId : job . id ,
152+ result
153+ } ) ;
154+ } catch ( err ) {
155+ res . status ( 500 ) . json ( {
156+ success : false ,
157+ error : err . message || 'Job execution failed'
158+ } ) ;
62159 }
63160} ) ;
64161
65- app . listen ( PORT , ( ) => console . log ( `Producer running on port ${ PORT } ` ) ) ;
162+ app . listen ( PORT , ( ) => {
163+ console . log ( `Producer running on port ${ PORT } ` ) ;
164+ } ) ;
0 commit comments