Repository navigation
Expand file tree
/
Copy pathworker.js
More file actions
495 lines (424 loc) Β· 17.7 KB
/
Copy pathworker.js
File metadata and controls
495 lines (424 loc) Β· 17.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
const mongoose = require("mongoose");
const Queue = require("bull");
const path = require("path");
const fs = require("fs");
const { exec } = require("child_process");
const axios = require("axios");
require("dotenv").config();
// Models
const Submission = require("./models/Submission");
const Question = require("./models/Question");
// Contest controller for updating progress
const { updateContestProgress } = require("./controllers/user/contest");
// 1. Connect to MongoDB
mongoose.connect(process.env.MONGO_URI)
.then(() => console.log("β
Worker Connected to MongoDB"))
.catch((err) => console.error("β Worker Mongo Error:", err));
// 2. Connect to Redis Queue
const submissionQueue = new Queue("submissions", {
redis: {
port: process.env.REDIS_PORT || 6379,
host: process.env.REDIS_HOST || '127.0.0.1'
},
});
// 3. Create Retry Queue
const retryQueue = new Queue("retry-submissions", {
redis: {
port: process.env.REDIS_PORT || 6379,
host: process.env.REDIS_HOST || '127.0.0.1'
},
});
// --- RETRY CONFIGURATION ---
const MAX_RETRY_ATTEMPTS = 3;
const RETRY_DELAY = 5000; // 5 seconds delay before retry
console.log("π Worker is running and waiting for jobs...");
console.log("π Retry queue is also running and waiting for retry jobs...");
console.log(`π Max retry attempts: ${MAX_RETRY_ATTEMPTS}`);
console.log(`β±οΈ Retry delay: ${RETRY_DELAY}ms`);
// --- CONFIGURATION ---
const PROCESSING_DIR = path.resolve("./processing");
if (!fs.existsSync(PROCESSING_DIR)) {
fs.mkdirSync(PROCESSING_DIR, { recursive: true });
}
// --- HELPER FUNCTIONS ---
// Updated helper to enforce timeout inside Node.js
const execShellCommand = (cmd, timeoutLimit = 0) => {
return new Promise((resolve) => {
// We pass 'timeout' (in milliseconds) to exec options
exec(cmd, { maxBuffer: 1024 * 10000, timeout: timeoutLimit }, (error, stdout, stderr) => {
const timedOut = !!(error && error.killed);
if (timedOut) {
console.log("β οΈ Process killed due to timeout!");
}
resolve({ stdout, stderr, error, timedOut });
});
});
};
const compareFiles = async (file_path1, file_path2) => {
try {
if (!fs.existsSync(file_path2)) return false;
const file1 = fs.readFileSync(file_path1, 'utf-8').trim().replace(/\r\n/g, '\n');
const file2 = fs.readFileSync(file_path2, 'utf-8').trim().replace(/\r\n/g, '\n');
return file1 === file2;
} catch (err) {
return false;
}
};
const downloadFile = (file, file_path, mode) => {
let data;
if (mode === "TEXT") {
data = file;
} else {
data = Buffer.from(file, "base64");
}
return new Promise((resolve, reject) => {
fs.writeFile(file_path, data, (err) => {
if (err) return reject(err);
return resolve();
});
});
};
const updateSubmissionInDB = async (submissionId, result, contest_id, user_id, question_id, isRetry = false, retryInfo = null) => {
const { execution_time, time_limit, compiled, correctAnswer, error_message } = result;
let status = "";
if (!compiled) status = "COMPILATION ERROR";
else if (execution_time > time_limit) status = "TIME LIMIT EXCEEDED";
else if (!correctAnswer) status = "WRONG ANSWER";
else status = "ACCEPTED";
const updateData = {
status: status,
execution_time: execution_time
};
// Add retry information if this is a retry attempt
if (isRetry && retryInfo) {
updateData.retry_count = retryInfo.retry_count;
updateData.last_retry_at = new Date();
updateData.$push = {
retry_history: {
attempt: retryInfo.retry_count,
status: status,
error_message: error_message || null,
timestamp: new Date()
}
};
}
// Update the existing submission doc
await Submission.findByIdAndUpdate(submissionId, updateData);
// π’ LOG: Final Verdict
console.log(`π Verdict for ${submissionId}: ${status}${isRetry ? ` (Retry ${retryInfo?.retry_count})` : ""}`);
// Update contest progress if this is a contest submission
if (contest_id && user_id) {
console.log(`π Updating contest progress for contest ${contest_id}`);
await updateContestProgress(contest_id, user_id, question_id, status);
}
};
// Helper function to determine if a submission should be retried
const shouldRetry = (result) => {
const { compiled, correctAnswer, execution_time, time_limit, error_occurred } = result;
// Don't retry these cases
if (!compiled) return false; // Compilation error
if (correctAnswer) return false; // Accepted
if (!correctAnswer && execution_time <= time_limit && !error_occurred) return false; // Wrong answer but ran successfully
// Retry these cases
if (execution_time > time_limit) return true; // Time limit exceeded (could be system load)
if (error_occurred) return true; // System errors
return false;
};
// Update submission status to IN_RETRY
const markSubmissionForRetry = async (submissionId, retryCount, errorMessage) => {
await Submission.findByIdAndUpdate(submissionId, {
status: "IN_RETRY",
retry_count: retryCount,
last_retry_at: new Date(),
$push: {
retry_history: {
attempt: retryCount,
status: "QUEUED_FOR_RETRY",
error_message: errorMessage,
timestamp: new Date()
}
}
});
console.log(`π Submission ${submissionId} marked for retry (Attempt ${retryCount})`);
};
// Mark submission as failed after max retries
const markSubmissionFailedRetry = async (submissionId) => {
await Submission.findByIdAndUpdate(submissionId, {
status: "FAILED_RETRY",
$push: {
retry_history: {
attempt: MAX_RETRY_ATTEMPTS + 1,
status: "FAILED_MAX_RETRIES",
error_message: "Maximum retry attempts exceeded",
timestamp: new Date()
}
}
});
console.log(`β Submission ${submissionId} failed after ${MAX_RETRY_ATTEMPTS} retry attempts`);
};
// --- MAIN WORKER LOGIC ---
submissionQueue.process(async (job, done) => {
const { question_id, base64_encoded_data, submission_id, contest_id, user_id } = job.data;
const jobId = job.id;
// π’ LOG: Job Received
console.log(`\n---------------------------------------------------`);
console.log(`π₯ Job Received: ID ${jobId} | Submission ${submission_id}`);
try {
const result = await processSubmission({
question_id,
base64_encoded_data,
submission_id,
contest_id,
user_id,
jobId,
isRetry: false
});
// Check if submission should be retried
if (shouldRetry(result)) {
const submission = await Submission.findById(submission_id);
const currentRetryCount = submission.retry_count || 0;
if (currentRetryCount < MAX_RETRY_ATTEMPTS) {
const newRetryCount = currentRetryCount + 1;
const errorMessage = result.error_message || "Execution failed - system error";
// Mark submission for retry in database
await markSubmissionForRetry(submission_id, newRetryCount, errorMessage);
// Add to retry queue with delay
await retryQueue.add({
question_id,
base64_encoded_data,
submission_id,
contest_id,
user_id,
retry_count: newRetryCount,
original_error: errorMessage
}, {
delay: RETRY_DELAY * newRetryCount // Exponential backoff
});
console.log(`π Submission ${submission_id} added to retry queue (Attempt ${newRetryCount})`);
done(null, { ...result, queued_for_retry: true });
return;
} else {
// Max retries exceeded
await markSubmissionFailedRetry(submission_id);
done(null, result);
return;
}
}
// No retry needed, update final status
await updateSubmissionInDB(submission_id, result, contest_id, user_id, question_id);
done(null, result);
} catch (error) {
console.error("Worker Error:", error);
// For unexpected errors, also consider retrying
const submission = await Submission.findById(submission_id);
const currentRetryCount = submission.retry_count || 0;
if (currentRetryCount < MAX_RETRY_ATTEMPTS) {
const newRetryCount = currentRetryCount + 1;
await markSubmissionForRetry(submission_id, newRetryCount, error.message);
await retryQueue.add({
question_id,
base64_encoded_data,
submission_id,
contest_id,
user_id,
retry_count: newRetryCount,
original_error: error.message
}, {
delay: RETRY_DELAY * newRetryCount
});
console.log(`π Submission ${submission_id} added to retry queue due to error (Attempt ${newRetryCount})`);
done(null, { error: true, queued_for_retry: true });
} else {
await markSubmissionFailedRetry(submission_id);
done(new Error(error.message));
}
}
});
// --- SUBMISSION PROCESSING FUNCTION ---
const processSubmission = async ({ question_id, base64_encoded_data, submission_id, contest_id, user_id, jobId, isRetry, retryCount = 0 }) => {
// Unique file names
const submission_file_path = path.join(PROCESSING_DIR, `sub_${jobId || submission_id}_${Date.now()}.cpp`);
const input_file_path = path.join(PROCESSING_DIR, `input_${jobId || submission_id}_${Date.now()}.txt`);
const solution_file_path = path.join(PROCESSING_DIR, `solution_${jobId || submission_id}_${Date.now()}.txt`);
const output_file_path = path.join(PROCESSING_DIR, `output_${jobId || submission_id}_${Date.now()}.txt`);
const executable_file_path = path.join(
PROCESSING_DIR,
process.platform === 'win32' ? `a_${jobId || submission_id}_${Date.now()}.exe` : `a_${jobId || submission_id}_${Date.now()}.out`
);
let error_occurred = false;
let error_message = "";
try {
const question = await Question.findById(question_id);
const { time_limit, solution_file, input_file } = question;
// 1. Setup Files
await downloadFile(base64_encoded_data, submission_file_path, "BUFFER");
if (input_file.startsWith("http")) {
const { data } = await axios.get(input_file);
await downloadFile(String(data), input_file_path, "TEXT");
} else {
fs.copyFileSync(path.resolve(input_file), input_file_path);
}
if (solution_file.startsWith("http")) {
const { data } = await axios.get(solution_file);
await downloadFile(String(data), solution_file_path, "TEXT");
} else {
fs.copyFileSync(path.resolve(solution_file), solution_file_path);
}
console.log("π Files Prepared.");
const cleanupFiles = async () => {
// π΄ FIX: Wait 100ms for Windows to release file locks
await new Promise(resolve => setTimeout(resolve, 100));
const filesToDelete = [
submission_file_path,
input_file_path,
solution_file_path,
output_file_path,
executable_file_path
];
filesToDelete.forEach(file => {
try {
if (fs.existsSync(file)) fs.unlinkSync(file);
} catch (err) {
// Just log warning, don't crash the worker
console.log(`β οΈ Warning: Could not delete ${path.basename(file)} (Locked by Windows)`);
}
});
};
// 2. Compile
console.log("βοΈ Compiling...");
const compileResult = await execShellCommand(`g++ "${submission_file_path}" -o "${executable_file_path}"`);
if (compileResult.error) {
await cleanupFiles();
return {
compiled: false,
time_limit,
execution_time: 0,
correctAnswer: false,
error_occurred: false,
error_message: "Compilation failed"
};
}
console.log("β
Compilation Successful.");
// 3. Run
console.log("π Running...");
let command;
if (process.platform === 'win32') {
command = `"${executable_file_path}" < "${input_file_path}" > "${output_file_path}"`;
} else {
command = `timeout ${time_limit + 1}s "${executable_file_path}" < "${input_file_path}" > "${output_file_path}"`;
}
const startTime = process.hrtime();
const runResult = await execShellCommand(command, time_limit * 1000);
const endTime = process.hrtime(startTime);
const execution_time = (endTime[0] * 1000 + endTime[1] / 1e6) / 1000;
// Check for runtime errors
if (runResult.error && !runResult.timedOut) {
error_occurred = true;
error_message = "Runtime error: " + runResult.stderr;
}
if (runResult.timedOut) {
await cleanupFiles();
return {
compiled: true,
time_limit,
execution_time: execution_time,
correctAnswer: false,
error_occurred: false,
error_message: "Time limit exceeded"
};
}
// 4. Compare
const correctAnswer = await compareFiles(solution_file_path, output_file_path);
await cleanupFiles();
return {
compiled: true,
time_limit,
execution_time,
correctAnswer,
error_occurred,
error_message
};
} catch (error) {
console.error("Processing Error:", error);
return {
compiled: false,
time_limit: 0,
execution_time: 0,
correctAnswer: false,
error_occurred: true,
error_message: error.message
};
}
};
// --- RETRY QUEUE PROCESSOR ---
retryQueue.process(async (job, done) => {
const { question_id, base64_encoded_data, submission_id, contest_id, user_id, retry_count, original_error } = job.data;
const jobId = `retry_${job.id}`;
console.log(`\n---------------------------------------------------`);
console.log(`π Retry Job Received: ID ${jobId} | Submission ${submission_id} | Attempt ${retry_count}`);
try {
const result = await processSubmission({
question_id,
base64_encoded_data,
submission_id,
contest_id,
user_id,
jobId,
isRetry: true,
retryCount: retry_count
});
// Check if this retry attempt should be retried again
if (shouldRetry(result) && retry_count < MAX_RETRY_ATTEMPTS) {
const newRetryCount = retry_count + 1;
const errorMessage = result.error_message || "Retry attempt failed - system error";
// Mark submission for another retry in database
await markSubmissionForRetry(submission_id, newRetryCount, errorMessage);
// Add to retry queue again with increased delay
await retryQueue.add({
question_id,
base64_encoded_data,
submission_id,
contest_id,
user_id,
retry_count: newRetryCount,
original_error: errorMessage
}, {
delay: RETRY_DELAY * newRetryCount
});
console.log(`π Submission ${submission_id} re-queued for retry (Attempt ${newRetryCount})`);
done(null, { ...result, queued_for_retry: true });
return;
}
// Final attempt or success - update with final status
if (shouldRetry(result) && retry_count >= MAX_RETRY_ATTEMPTS) {
// Max retries exceeded
await markSubmissionFailedRetry(submission_id);
} else {
// Success or non-retryable failure
await updateSubmissionInDB(submission_id, result, contest_id, user_id, question_id, true, { retry_count });
}
done(null, result);
} catch (error) {
console.error("Retry Worker Error:", error);
if (retry_count < MAX_RETRY_ATTEMPTS) {
const newRetryCount = retry_count + 1;
await markSubmissionForRetry(submission_id, newRetryCount, error.message);
await retryQueue.add({
question_id,
base64_encoded_data,
submission_id,
contest_id,
user_id,
retry_count: newRetryCount,
original_error: error.message
}, {
delay: RETRY_DELAY * newRetryCount
});
console.log(`π Submission ${submission_id} re-queued due to retry error (Attempt ${newRetryCount})`);
done(null, { error: true, queued_for_retry: true });
} else {
await markSubmissionFailedRetry(submission_id);
done(new Error(error.message));
}
}
});