Spaces:
Running
Running
File size: 5,988 Bytes
9f3217a | 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 | const fs = require('fs');
let code = fs.readFileSync('server.js', 'utf8');
const startIndex = code.indexOf(' for (let i = 0; i < jobState.files.length; i++) {');
if (startIndex === -1) {
console.log("Could not find start index");
process.exit(1);
}
const endIndex = code.indexOf(` if (jobState.status !== 'cancelled') {
jobState.status = 'completed';`);
if (endIndex === -1) {
console.log("Could not find end index");
process.exit(1);
}
const beforeLoop = code.slice(0, startIndex);
const loopBody = code.slice(startIndex, endIndex);
const afterLoop = code.slice(endIndex);
const newBeforeLoop = beforeLoop + ` await runUploadQueue(auth);\n\n`;
const modifiedAfterLoop = afterLoop.replace(
` if (jobState.status !== 'cancelled') {
jobState.status = 'completed';
jobState.finishedAt = new Date().toISOString();
addJobLog('All videos in queue processed successfully.', 'success');
broadcastSSE({ type: 'process_completed', message: 'All videos processed successfully.' });
persistJobState();
}
} catch (fatalErr) {
console.error('Fatal background error:', fatalErr);
jobState.status = 'error';
if (isAuthError(fatalErr)) {
updateEnvFile('GOOGLE_REFRESH_TOKEN', '');
addJobLog(\`Google Authentication Error: \${fatalErr.message}. Please re-connect Google account.\`, 'error');
broadcastSSE({ type: 'auth_required', message: \`Authentication expired or invalid. Please click 'Connect Google' to authorize.\` });
} else {
addJobLog(\`Pipeline encountered fatal error: \${fatalErr.message}\`, 'error');
broadcastSSE({ type: 'error', message: fatalErr.message });
}
persistJobState();
} finally {
activeAbortController = null;
}
})();
});`,
` if (jobState.status !== 'cancelled' && jobState.status !== 'paused_quota') {
jobState.status = 'completed';
jobState.finishedAt = new Date().toISOString();
addJobLog('All videos in queue processed successfully.', 'success');
broadcastSSE({ type: 'process_completed', message: 'All videos processed successfully.' });
persistJobState();
}
} catch (fatalErr) {
console.error('Fatal background error:', fatalErr);
jobState.status = 'error';
if (isAuthError(fatalErr)) {
addJobLog(\`Google Authentication Error: \${fatalErr.message}. Please re-connect Google account.\`, 'error');
broadcastSSE({ type: 'auth_required', message: \`Authentication expired or invalid. Please click 'Connect Google' to authorize.\` });
} else {
addJobLog(\`Pipeline encountered fatal error: \${fatalErr.message}\`, 'error');
broadcastSSE({ type: 'error', message: fatalErr.message });
}
persistJobState();
} finally {
activeAbortController = null;
}
})();
});\n\n`
);
let runUploadQueueFunc = `
async function runUploadQueue(auth) {
const { google } = require('googleapis');
const drive = google.drive({ version: 'v3', auth });
const youtube = google.youtube({ version: 'v3', auth });
const { Transform } = require('stream');
` + loopBody + `
}
`;
// Insert the continue condition for completed files
runUploadQueueFunc = runUploadQueueFunc.replace(
` if (jobState.status === 'cancelled') {
addJobLog('Pipeline cancelled during queue execution.', 'warn');
break;
}`,
` if (jobState.status === 'cancelled' || jobState.status === 'paused_quota') {
addJobLog('Pipeline stopped during queue execution.', 'warn');
break;
}
if (jobState.files[i].status === 'completed' || jobState.files[i].status === 'failed') {
continue;
}`
);
// We need to fix the resume endpoint to call runUploadQueue instead of processQueue
let newCode = newBeforeLoop + modifiedAfterLoop + runUploadQueueFunc;
newCode = newCode.replace(
` // Start processing again with the new auth
processQueue(auth).catch(err => {`,
` // Start processing again with the new auth
(async () => {
try {
activeAbortController = new AbortController();
await runUploadQueue(auth);
if (jobState.status !== 'cancelled' && jobState.status !== 'paused_quota') {
jobState.status = 'completed';
jobState.finishedAt = new Date().toISOString();
addJobLog('All videos in queue processed successfully.', 'success');
broadcastSSE({ type: 'process_completed', message: 'All videos processed successfully.' });
persistJobState();
}
} catch(err) {`
);
// also fix the catch block in resume
newCode = newCode.replace(
` processQueue(auth).catch(err => {
console.error('Background process queue error during resume:', err);
jobState.status = 'error';
addJobLog('Fatal error during background processing resume: ' + err.message, 'error');
persistJobState();
broadcastSSE({ type: 'error', message: err.message });
});`,
` (async () => {
try {
activeAbortController = new AbortController();
await runUploadQueue(auth);
if (jobState.status !== 'cancelled' && jobState.status !== 'paused_quota') {
jobState.status = 'completed';
jobState.finishedAt = new Date().toISOString();
addJobLog('All videos in queue processed successfully.', 'success');
broadcastSSE({ type: 'process_completed', message: 'All videos processed successfully.' });
persistJobState();
}
} catch(err) {
console.error('Background process queue error during resume:', err);
jobState.status = 'error';
addJobLog('Fatal error during background processing resume: ' + err.message, 'error');
persistJobState();
broadcastSSE({ type: 'error', message: err.message });
} finally {
activeAbortController = null;
}
})();`
);
fs.writeFileSync('server.js', newCode);
console.log("Patched successfully");
|