-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub_queue.js
More file actions
269 lines (225 loc) · 8.41 KB
/
Copy pathgithub_queue.js
File metadata and controls
269 lines (225 loc) · 8.41 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
"use strict";
let isQueueProcessing = false;
let queueTimeoutId = null;
let emptyQueueSleepMs = 5000; // 5-second sleep when queue empty
function startQueueProcessor() {
triggerQueueRun();
}
function triggerQueueRun() {
if (queueTimeoutId) {
clearTimeout(queueTimeoutId);
}
// Run processQueue dynamically
queueTimeoutId = setTimeout(() => {
processQueue().catch((err) => console.error("Error processing sync queue:", err));
}, 100);
}
async function processQueue() {
if (isQueueProcessing) return;
isQueueProcessing = true;
try {
const jobs = await getQueueJobs();
const pendingJobs = jobs.filter(j => j.status === "pending" || j.status === "failed");
if (pendingJobs.length === 0) {
isQueueProcessing = false;
// Sleep cycle: schedule next run in 5 seconds
queueTimeoutId = setTimeout(() => {
processQueue().catch((err) => console.error("Error processing sync queue:", err));
}, emptyQueueSleepMs);
return;
}
console.info(`Found ${pendingJobs.length} jobs in CodeSync sync queue. Processing micro-batch.`);
// Micro-batch limit: 3 jobs max per cycle
const batch = pendingJobs.slice(0, 3);
for (const job of batch) {
if (job.retries >= 3) {
job.status = "abandoned";
await updateQueueJob(job);
continue;
}
job.status = "processing";
job.lastAttempt = Date.now();
await updateQueueJob(job);
try {
await executeJob(job.payload);
// Remove from queue on success
await removeQueueJob(job.id);
// Update progress of completed sheets
const problemKey = `${job.payload.platform.toLowerCase()}:${job.payload.slug.toLowerCase()}`;
const sheets = getSheetsForProblem(job.payload.platform, job.payload.slug, job.payload.title);
// Batch progress updates
for (const sheet of sheets) {
await markSolvedInProgress(sheet, problemKey);
}
} catch (err) {
console.warn(`Sync job ${job.id} failed:`, err.message);
job.status = "failed";
job.retries += 1;
job.error = err.message;
await updateQueueJob(job);
}
}
} catch (error) {
console.error("Queue loop failure:", error);
} finally {
isQueueProcessing = false;
// Process next batch soon (1.5 seconds)
queueTimeoutId = setTimeout(() => {
processQueue().catch((err) => console.error("Error processing sync queue:", err));
}, 1500);
}
}
async function executeJob(submission) {
const settings = await getSettings();
validateSettings(settings);
const basePaths = buildSubmissionBasePaths(submission, settings);
const extension = extensionForLanguage(submission.language);
const solutionFileName = `solution.${extension}`;
const solutionHeader = getCommentHeader(submission, extension);
const readmeContent = buildReadme(submission);
const metadataContent = `${JSON.stringify(buildMetadata(submission), null, 2)}\n`;
const solutionContent = `${solutionHeader}${submission.sourceCode.trim()}\n`;
const sheets = getSheetsForProblem(submission.platform, submission.slug, submission.title);
const sheetsLine = sheets.length > 0 ? sheets.join(", ") : "None";
// Clean commit message
const commitMessage = `Solved: ${submission.title} (${submission.platform})\nSheets: ${sheetsLine}`;
const writeContext = {
token: settings.githubToken,
repository: settings.repository,
branch: cleanText(settings.branch),
author: buildCommitAuthor(settings),
message: commitMessage
};
// Sync to all computed base paths sequentially
for (const basePath of basePaths) {
await putGitHubFile({
...writeContext,
path: joinPath(basePath, solutionFileName),
content: solutionContent
});
await putGitHubFile({
...writeContext,
path: joinPath(basePath, "README.md"),
content: readmeContent
});
await putGitHubFile({
...writeContext,
path: joinPath(basePath, "metadata.json"),
content: metadataContent
});
}
// Handle Global/Custom Metadata Folders
await updateSheetMetadata(submission, sheets, writeContext);
// Update repository README.md with latest sheets solved counts
try {
await updateReadmeSolvedCounts(writeContext);
} catch (err) {
console.warn("Failed to update README solved counts:", err.message);
}
if (settings.enableDailyStreak) {
await putGitHubFile({
...writeContext,
path: buildDailyStreakPath(submission, settings),
content: buildDailyStreakContent(submission),
message: `Update CodeSync streak: ${new Date(submission.detectedAt).toISOString().slice(0, 10)}`
});
}
notify("CodeSync synced solution", `${submission.platform}: ${submission.title}`);
}
async function updateSheetMetadata(submission, sheets, writeContext) {
const metadataPath = "metadata/problem_sheets.json";
let existingIndex = {};
try {
const existingFile = await getGitHubFile(writeContext.token, `https://api.github.com/repos/${writeContext.repository}/contents/${metadataPath}`, writeContext.branch);
if (existingFile && existingFile.content) {
const decoded = base64ToUtf8(existingFile.content);
existingIndex = JSON.parse(decoded);
}
} catch (e) {
console.info("Metadata file problem_sheets.json does not exist yet. Creating a new one.");
}
const problemKey = `${submission.platform.toLowerCase()}:${submission.slug.toLowerCase()}`;
existingIndex[problemKey] = sheets;
const content = JSON.stringify(existingIndex, null, 2) + "\n";
await putGitHubFile({
...writeContext,
path: metadataPath,
content: content,
message: `Update sheets metadata for ${problemKey}`
});
}
async function updateReadmeSolvedCounts(writeContext) {
const readmePath = "README.md";
let readmeFile;
try {
readmeFile = await getGitHubFile(writeContext.token, `https://api.github.com/repos/${writeContext.repository}/contents/${readmePath}`, writeContext.branch);
} catch (e) {
console.info("Could not fetch README.md to update solved progress:", e.message);
return;
}
if (!readmeFile || !readmeFile.content) {
return;
}
const progressList = await getAllProgress().catch(() => []);
const solvedCounts = {};
progressList.forEach((p) => {
solvedCounts[p.sheetName] = p.solvedKeys.length;
});
const readmeText = base64ToUtf8(readmeFile.content);
let updatedText = readmeText;
let hasChanges = false;
const hasTable = updatedText.includes("Coding Sheets Progress") || updatedText.includes("Supported Coding Sheets");
if (!hasTable) {
// Generate and append sheets progress table if missing
const initialTable = getInitialProgressTable(solvedCounts);
updatedText = updatedText.trim() + "\n\n" + initialTable;
hasChanges = true;
} else {
for (const [sheetId, sheetMeta] of Object.entries(SUPPORTED_SHEETS)) {
const solvedCount = solvedCounts[sheetId] || 0;
const escapedName = sheetMeta.name.replace(/[-\/\\^$*+?.()|[\]{}]/g, '\\$&');
const regex = new RegExp(`(\\|\\s*${escapedName}\\s*\\|\\s*)\\d+(\\s*\\|\\s*${sheetMeta.total}\\s*\\|)`, "i");
if (regex.test(updatedText)) {
const newText = updatedText.replace(regex, `$1${solvedCount}$2`);
if (newText !== updatedText) {
updatedText = newText;
hasChanges = true;
}
}
}
}
if (hasChanges) {
await putGitHubFile({
...writeContext,
path: readmePath,
content: updatedText,
message: "docs: update sheets solved progress tracker in README"
});
console.info("Successfully updated sheets solved progress tracker in repository README.md");
}
}
function getInitialProgressTable(solvedCounts) {
const lines = [
"## Coding Sheets Progress",
"",
"CodeSync automatically tracks your progress across curated coding sheets. Here is your current progress:",
"",
"| Coding Sheet | Solved | Total |",
"| :--- | :--- | :--- |"
];
for (const [sheetId, sheetMeta] of Object.entries(SUPPORTED_SHEETS)) {
const solvedCount = solvedCounts[sheetId] || 0;
lines.push(`| ${sheetMeta.name} | ${solvedCount} | ${sheetMeta.total} |`);
}
lines.push("");
return lines.join("\n");
}
function base64ToUtf8(str) {
const binary = atob(str.replace(/\s/g, ""));
const len = binary.length;
const bytes = new Uint8Array(len);
for (let i = 0; i < len; i++) {
bytes[i] = binary.charCodeAt(i);
}
return new TextDecoder().decode(bytes);
}