From ff2f7e6939ac0c7f39f3fe6c25519cb863acca2c Mon Sep 17 00:00:00 2001 From: elaineshijie Date: Thu, 14 Nov 2024 07:53:13 +0800 Subject: [PATCH 1/3] fix: no execution of database questions --- .../components/TestCases/TestCasesWrapper.tsx | 1 + run-service/controller.js | 82 ++++++++++++++----- user-service/middlewares/redis.js | 2 +- 3 files changed, 64 insertions(+), 21 deletions(-) diff --git a/peer-prep/src/components/TestCases/TestCasesWrapper.tsx b/peer-prep/src/components/TestCases/TestCasesWrapper.tsx index cd2d642821..598cc3b9e8 100644 --- a/peer-prep/src/components/TestCases/TestCasesWrapper.tsx +++ b/peer-prep/src/components/TestCases/TestCasesWrapper.tsx @@ -281,6 +281,7 @@ const TestCasesWrapper = forwardRef( channelId, firstUserId: userId, secondUserId: otherUserId, + categoriesId: question.categoriesId, }), headers: { "Content-Type": "application/json", diff --git a/run-service/controller.js b/run-service/controller.js index d246328458..5702f56807 100644 --- a/run-service/controller.js +++ b/run-service/controller.js @@ -18,8 +18,10 @@ const startSession = async (req, res) => { // Check if a session already exists by checking redis store const sessionKey = `session:${userA}-${userB}`; - console.log("✅✅✅✅ REDIS CONTENTS UPON REQUESTION TO START SESSION ✅✅✅✅") - printRedisMemory() + console.log( + "✅✅✅✅ REDIS CONTENTS UPON REQUESTION TO START SESSION ✅✅✅✅" + ); + printRedisMemory(); // Get session data from Redis const channelData = await redisClient.hGetAll(sessionKey); if (channelData && channelData.channelId) { @@ -44,7 +46,7 @@ const startSession = async (req, res) => { userA, userB, }); - await redisClient.hSet(`channelId:${channelId}`, { + await redisClient.hSet(`channel:${channelId}`, { sessionKey, [firstUserId]: "disconnected", [secondUserId]: "disconnected", @@ -122,7 +124,6 @@ const subscribeToChannel = async (req, res) => { // res.write(`data: ${JSON.stringify(update)}\n\n`); } else if (update.statusCode === 206) { - // temporarily: set a random timeout from 1 to 5 seconds // comment out later const timeout = Math.floor(Math.random() * 5000) + 1000; @@ -131,7 +132,6 @@ const subscribeToChannel = async (req, res) => { // }, timeout); // res.write(`data: ${JSON.stringify(update)}\n\n`); - } else { res.write(`data: ${JSON.stringify(update)}\n\n`); } @@ -160,7 +160,8 @@ const executeTest = async (req, res) => { try { console.log("executing test started"); const questionId = req.params.questionId; - const { codeAttempt, channelId, firstUserId, secondUserId } = req.body; + const { codeAttempt, channelId, firstUserId, secondUserId, categoriesId } = + req.body; // Check if another execution is in progress const initialChannelData = await redisClient.hGetAll( @@ -199,24 +200,62 @@ const executeTest = async (req, res) => { if (!testcases) { throw new NotFoundError("Testcases not found for the question"); } + const testCaseCount = testcases.length; + + // check if categoriesId include 2 - no execution of test cases for database question + if (categoriesId.includes(2)) { + const results = []; + // for each test case of this question,console.log("hello") + for (let i = 0; i < testCaseCount; i++) { + let testCaseDetailsFinal = { + input: "No input description", + expectedOutput: "No expected output", + testCaseId: testcases[i]._id, + }; + + const testCaseResult = { + stderr: "No execution of test cases for database question", + isPassed: true, + stdout: "No execution of test cases for database question", + testCaseDetails: testCaseDetailsFinal, + memory: 0, + time: "0", + }; + results.push(testCaseResult); + } + + await redisClient.publish( + `channel:${channelId}`, + JSON.stringify({ + statusCode: 200, + data: { results: results, questionId, code }, + }) + ); + + return res.status(200).json({ + statusCode: 200, + message: `No execution of test cases for database question: ${questionId}`, + data: { testCaseCount: testCaseCount }, + }); + } for (let i = 0; i < testcases.length; i++) { console.log("Testcase:", testcases[i]._id); console.log("isPublic:", testcases[i].isPublic); } console.log("Testcases retrieved sucessfully"); - console.log({ channelId }) + console.log({ channelId }); // Indicate that the test cases are being processed - initial message to indicate start of execution const channelData = await redisClient.hGetAll(`channel:${channelId}`); if (channelData && channelData.status !== "processing") { await redisClient.hSet(`channel:${channelId}`, { + ...channelData, status: "processing", questionId, codeAttempt, data: JSON.stringify([]), }); } - const testCaseCount = testcases.length; const toLog = await redisClient.hGetAll(`channel:${channelId}`); console.log("Channel data after setting status:", toLog); @@ -228,10 +267,10 @@ const executeTest = async (req, res) => { res.status(200).json({ statusCode: 200, message: `Executing test cases for questionId: ${questionId}`, - data: { testCaseCount: testCaseCount } + data: { testCaseCount: testCaseCount }, }); } catch (error) { - console.log("ERROR: ", error) + console.log("ERROR: ", error); if (error instanceof BaseError) { console.log("Error while executing testcase:", error.message); return res @@ -335,13 +374,14 @@ async function processTestcases(channelId, testcases, code, questionId) { console.log("============Starting testcases execution=========="); await redisClient.publish( `channel:${channelId}`, - JSON.stringify({ statusCode: 202, message: `Executing test cases of question ${questionId}` }) + JSON.stringify({ + statusCode: 202, + message: `Executing test cases of question ${questionId}`, + }) ); - const results = []; - // Map each test case to a promise const promises = testcases.map(async (testcase) => { try { @@ -371,7 +411,10 @@ async function processTestcases(channelId, testcases, code, questionId) { results.push(errorMessage); hasError = true; - console.log("==========Error while executing testcase:", errorMessage.testcaseId); + console.log( + "==========Error while executing testcase:", + errorMessage.testcaseId + ); console.log("==========Error Message:", errorMessage.message); // Publish only the latest error result @@ -427,14 +470,17 @@ async function processTestcases(channelId, testcases, code, questionId) { console.log("Completed testcases execution for questionId:", questionId); + const channelCurrentData = await redisClient.hGetAll( + `channel:${channelId}` + ); await redisClient.hSet(`channel:${channelId}`, { status: hasError ? "error" : "completed", data: JSON.stringify(results), questionId: questionId, codeAttempt: code, + ...channelCurrentData, }); - // Publish the "complete" status only if no errors occurred if (!hasError) { await redisClient.publish( @@ -446,15 +492,11 @@ async function processTestcases(channelId, testcases, code, questionId) { ); } } catch (error) { - console.log("Some testcases failed:", error); + console.log("Some testcases execution failed:", error); hasError = true; - - } - - printRedisMemory(); } diff --git a/user-service/middlewares/redis.js b/user-service/middlewares/redis.js index ae8d668660..c94bd4ac5a 100644 --- a/user-service/middlewares/redis.js +++ b/user-service/middlewares/redis.js @@ -3,7 +3,7 @@ import dotenv from 'dotenv'; dotenv.config(); console.log("[user-service] connecting to redis with url " + process.env.REDIS_URL); const client = createClient({ - url: process.env.REDIS_URL || 'redis://redis:6379' // Adjust the URL if using Docker or a remote instance + url: process.env.REDIS_URL || 'redis://localhost:6379' // Adjust the URL if using Docker or a remote instance }); client.on("ready", () => console.log("Connected to redis!")); From d1211cdb17fc6eb027b3cd3071a11b6a53393458 Mon Sep 17 00:00:00 2001 From: elaineshijie Date: Thu, 14 Nov 2024 07:54:33 +0800 Subject: [PATCH 2/3] fix: revert non-intended changes --- user-service/middlewares/redis.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/user-service/middlewares/redis.js b/user-service/middlewares/redis.js index c94bd4ac5a..ae8d668660 100644 --- a/user-service/middlewares/redis.js +++ b/user-service/middlewares/redis.js @@ -3,7 +3,7 @@ import dotenv from 'dotenv'; dotenv.config(); console.log("[user-service] connecting to redis with url " + process.env.REDIS_URL); const client = createClient({ - url: process.env.REDIS_URL || 'redis://localhost:6379' // Adjust the URL if using Docker or a remote instance + url: process.env.REDIS_URL || 'redis://redis:6379' // Adjust the URL if using Docker or a remote instance }); client.on("ready", () => console.log("Connected to redis!")); From d186b1197219749ecc3a714cd4fcc3e6000a67a2 Mon Sep 17 00:00:00 2001 From: elaineshijie Date: Thu, 14 Nov 2024 09:26:58 +0800 Subject: [PATCH 3/3] fix: execute multiple times --- run-service/controller.js | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/run-service/controller.js b/run-service/controller.js index 5702f56807..6c3867b1fd 100644 --- a/run-service/controller.js +++ b/run-service/controller.js @@ -228,7 +228,7 @@ const executeTest = async (req, res) => { `channel:${channelId}`, JSON.stringify({ statusCode: 200, - data: { results: results, questionId, code }, + data: { results: results, questionId, code:codeAttempt }, }) ); @@ -249,7 +249,6 @@ const executeTest = async (req, res) => { const channelData = await redisClient.hGetAll(`channel:${channelId}`); if (channelData && channelData.status !== "processing") { await redisClient.hSet(`channel:${channelId}`, { - ...channelData, status: "processing", questionId, codeAttempt, @@ -473,13 +472,16 @@ async function processTestcases(channelId, testcases, code, questionId) { const channelCurrentData = await redisClient.hGetAll( `channel:${channelId}` ); + await redisClient.hSet(`channel:${channelId}`, { status: hasError ? "error" : "completed", data: JSON.stringify(results), questionId: questionId, - codeAttempt: code, - ...channelCurrentData, + codeAttempt: code }); + const channelDateToLog = await redisClient.hGetAll(`channel:${channelId}`); + + console.log("kjvnjeknrvjrwek Channel data after setting status:", channelDateToLog); // Publish the "complete" status only if no errors occurred if (!hasError) {