phase 2 rework
This commit is contained in:
1 parent
20054198f3
commit
b397ea9256
44 files changed
+5528
-1377
No files matched your search
+12
-6
@@ -5,6 +5,7 @@ const { config } = require('./lib/config');
|
||||
const { getDb, closeDb } = require('./lib/mongo');
|
||||
const { ValidationError, normalizeWatchUrl, normalizeFilename, validateOutputTarget, cookieToken } = require('./lib/jobValidation');
|
||||
const { LOG_LIMIT, sanitizeLog, createLineSanitizer } = require('./lib/logging');
|
||||
const { createWorkerIdentity, toPublicWorkerIdentity } = require('./lib/workerIdentity');
|
||||
|
||||
const RESULT_PREFIX = 'PROXER_RESULT ';
|
||||
const ERROR_CATEGORIES = new Set(['validation', 'authentication', 'resolution', 'network', 'source', 'storage', 'configuration', 'unknown']);
|
||||
@@ -177,7 +178,7 @@ async function processClaimedJob(job, { db, execute = executeJob, now = () => ne
|
||||
// Preserve the complete request for a later worker start; never consume the
|
||||
// queue because an interpreter, dependency or media mount is unavailable.
|
||||
await db.collection('queue').updateOne({ _id: job._id, processing: true },
|
||||
{ $unset: { processing: '', started_at: '' } });
|
||||
{ $unset: { processing: '', started_at: '', worker: '' } });
|
||||
if (result.infrastructureFailure) throw new Error('Worker infrastructure is unavailable. The job remains queued.');
|
||||
return result;
|
||||
}
|
||||
@@ -188,6 +189,8 @@ async function processClaimedJob(job, { db, execute = executeJob, now = () => ne
|
||||
created_at: job.created_at,
|
||||
output_log: sanitizeLog(result.output, jobSecrets(job)),
|
||||
};
|
||||
const worker = toPublicWorkerIdentity(job.worker);
|
||||
if (worker) common.worker = worker;
|
||||
|
||||
if (result.ok) {
|
||||
// History must be durable before removing the queue record. Full atomic job
|
||||
@@ -214,7 +217,7 @@ async function processClaimedJob(job, { db, execute = executeJob, now = () => ne
|
||||
// Preserve the original request and credential for a deliberate retry. The
|
||||
// attempt stays visible in history, while the job remains queued and paused.
|
||||
const release = await db.collection('queue').updateOne({ _id: job._id, processing: true },
|
||||
{ $unset: { processing: '', started_at: '' } });
|
||||
{ $unset: { processing: '', started_at: '', worker: '' } });
|
||||
if (release?.acknowledged !== true) throw new Error('The claim release could not be confirmed. Check the paused queue before resuming.');
|
||||
} else {
|
||||
// Ordinary failures become history rather than looping or pausing all jobs.
|
||||
@@ -230,24 +233,27 @@ async function downloadsPaused(db) {
|
||||
return global?.captcha_blocked === true;
|
||||
}
|
||||
|
||||
async function claimNextJob(db, { shouldStop = () => false, now = () => new Date() } = {}) {
|
||||
async function claimNextJob(db, { shouldStop = () => false, now = () => new Date(), workerIdentity } = {}) {
|
||||
if (shouldStop() || await downloadsPaused(db) || shouldStop()) return null;
|
||||
const worker = toPublicWorkerIdentity(workerIdentity);
|
||||
const job = await db.collection('queue').findOneAndUpdate(
|
||||
{ processing: { $ne: true } },
|
||||
{ $set: { processing: true, started_at: now() } },
|
||||
{ $set: { processing: true, started_at: now(), ...(worker ? { worker } : {}) },
|
||||
...(!worker ? { $unset: { worker: '' } } : {}) },
|
||||
{ sort: { created_at: 1 }, returnDocument: 'after' },
|
||||
);
|
||||
if (!job) return null;
|
||||
if (await downloadsPaused(db) || shouldStop()) {
|
||||
// A concurrent worker paused after our initial read. No child has started.
|
||||
await db.collection('queue').updateOne({ _id: job._id, processing: true },
|
||||
{ $unset: { processing: '', started_at: '' } });
|
||||
{ $unset: { processing: '', started_at: '', worker: '' } });
|
||||
return null;
|
||||
}
|
||||
return job;
|
||||
}
|
||||
|
||||
async function main() {
|
||||
const workerIdentity = createWorkerIdentity();
|
||||
const db = await getDb();
|
||||
let stopping = false;
|
||||
let child = null;
|
||||
@@ -262,7 +268,7 @@ async function main() {
|
||||
|
||||
try {
|
||||
while (!stopping) {
|
||||
const job = await claimNextJob(db, { shouldStop: () => stopping });
|
||||
const job = await claimNextJob(db, { shouldStop: () => stopping, workerIdentity });
|
||||
if (!job) { if (!stopping) await delay(1000); continue; }
|
||||
const result = await processClaimedJob(job, { db,
|
||||
execute: (request) => executeJob(request, {
|
||||
|
||||
Reference in new issue
Block a user