|
| 1 | +const { Queue, Worker } = require('bullmq'); |
| 2 | +const jobs = require('../models/jobs'); |
| 3 | +const { executeCpp } = require('./code_execution/execute_cpp'); |
| 4 | +const { executePy } = require('./code_execution/executePy'); |
| 5 | +const Problem = require('../models/problem_model'); |
| 6 | + |
| 7 | +const getRedisClient = require('../config/get_redis_client'); |
| 8 | + |
| 9 | +const redisClient = getRedisClient(); |
| 10 | + |
| 11 | +// Create a new BullMQ queue |
| 12 | +const jobQueue = new Queue('job-queue', { |
| 13 | + connection: redisClient |
| 14 | +}); |
| 15 | + |
| 16 | + |
| 17 | +// Create a worker to process jobs |
| 18 | +const worker = new Worker('job-queue', async job => { |
| 19 | + const jobId = job.data.id; |
| 20 | + console.log(jobId) |
| 21 | + const jobDoc = await jobs.findById(jobId); |
| 22 | + if (!jobDoc) { |
| 23 | + throw new Error(`Cannot find Job with id ${jobId}`); |
| 24 | + } |
| 25 | + const problemId = job.data.problemId; |
| 26 | + |
| 27 | + const problem = await Problem.findOne({ id: problemId }).populate('testCases'); |
| 28 | + |
| 29 | + if (!problem) { |
| 30 | + throw Error(`Cannot find Problem with id ${problemId}`); |
| 31 | + } |
| 32 | + |
| 33 | + console.log("Processing job:", job.data); |
| 34 | + |
| 35 | + const mismatches = []; |
| 36 | + try { |
| 37 | + let output; |
| 38 | + jobDoc.startedAt = new Date(); |
| 39 | + |
| 40 | + if (jobDoc.language === "cpp") { |
| 41 | + for (const testCase of problem.testCases) { |
| 42 | + output = await executeCpp(jobDoc.filepath, testCase.input); |
| 43 | + if (output.trim() !== testCase.output.trim()) { |
| 44 | + mismatches.push({ |
| 45 | + input: testCase.input, |
| 46 | + expectedOutput: testCase.output, |
| 47 | + actualOutput: output |
| 48 | + }); |
| 49 | + } |
| 50 | + } |
| 51 | + } else if (jobDoc.language === "py") { |
| 52 | + output = await executePy(jobDoc.filepath); |
| 53 | + } |
| 54 | + |
| 55 | + jobDoc.completedAt = new Date(); |
| 56 | + jobDoc.output = output; |
| 57 | + jobDoc.status = mismatches.length ? "error" : "success"; |
| 58 | + jobDoc.mismatches = mismatches; |
| 59 | + |
| 60 | + await jobDoc.save(); |
| 61 | + return true; |
| 62 | + } catch (err) { |
| 63 | + jobDoc.completedAt = new Date(); |
| 64 | + jobDoc.output = JSON.stringify(err); |
| 65 | + jobDoc.status = "error"; |
| 66 | + jobDoc.mismatches = mismatches; |
| 67 | + |
| 68 | + await jobDoc.save(); |
| 69 | + throw err; |
| 70 | + } |
| 71 | +}, { |
| 72 | + connection: redisClient, |
| 73 | + concurrency: 5 |
| 74 | +}); |
| 75 | + |
| 76 | + |
| 77 | +worker.on('failed', (job, err) => { |
| 78 | + console.error(`Job ${job.id} failed with error:`, err); |
| 79 | +}); |
| 80 | + |
| 81 | +process.on('SIGINT', async () => { |
| 82 | + console.log('Shutting down gracefully...'); |
| 83 | + await worker.close(); |
| 84 | + await redisClient.quit(); |
| 85 | + process.exit(0); |
| 86 | +}); |
| 87 | + |
| 88 | +// Add jobs to the queue |
| 89 | +const addJobToQueue = async ({ jobId, problemId, language, filepath }) => { |
| 90 | + try { |
| 91 | + await jobQueue.add('job', { |
| 92 | + id: jobId, |
| 93 | + problemId: problemId, |
| 94 | + language: language, |
| 95 | + filepath: filepath |
| 96 | + }); |
| 97 | + console.log(`Added job ${jobId} to the queue.`); |
| 98 | + } catch (err) { |
| 99 | + console.error(`Error adding job ${jobId} to queue:`, err); |
| 100 | + } |
| 101 | +} |
| 102 | + |
| 103 | +module.exports = { |
| 104 | + addJobToQueue |
| 105 | +} |
0 commit comments