Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run service database #129

Draft
wants to merge 3 commits into
base: main_backup
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions peer-prep/src/components/TestCases/TestCasesWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ const TestCasesWrapper = forwardRef(
channelId,
firstUserId: userId,
secondUserId: otherUserId,
categoriesId: question.categoriesId,
}),
headers: {
"Content-Type": "application/json",
Expand Down
84 changes: 64 additions & 20 deletions run-service/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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",
Expand Down Expand Up @@ -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;
Expand All @@ -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`);
}
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -199,13 +200,51 @@ 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:codeAttempt },
})
);

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") {
Expand All @@ -216,7 +255,6 @@ const executeTest = async (req, res) => {
data: JSON.stringify([]),
});
}
const testCaseCount = testcases.length;

const toLog = await redisClient.hGetAll(`channel:${channelId}`);
console.log("Channel data after setting status:", toLog);
Expand All @@ -228,10 +266,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
Expand Down Expand Up @@ -335,13 +373,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 {
Expand Down Expand Up @@ -371,7 +410,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
Expand Down Expand Up @@ -427,13 +469,19 @@ 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,
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) {
Expand All @@ -446,15 +494,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();
}

Expand Down