phase 2 rework

This commit is contained in:
admin_rb committed 2026-10-23 18:19:44 +02:00
1 parent 20054198f3
commit b397ea9256
44 files changed
+5528 -1377

No files matched your search

+44 -7
View File
@@ -7,6 +7,7 @@ const os = require('node:os');
const path = require('node:path');
const { executeJob, processClaimedJob, claimNextJob, RESULT_PREFIX } = require('../queue_worker');
const { LOG_LIMIT } = require('../lib/logging');
const { createWorkerIdentity } = require('../lib/workerIdentity');
const now = new Date('2026-01-01T12:00:00.000Z');
const successfulResult = `${RESULT_PREFIX}${JSON.stringify({ version: 1, status: 'completed' })}\n`;
@@ -14,6 +15,10 @@ const makeJob = (changes = {}) => ({
_id: 'fixture-job', filename: 'Fixture Episode', url: 'https://proxer.me/watch/42/1/engsub',
token: 'fixture_cookie=fixture-secret==', created_at: now, processing: true, ...changes,
});
const workerIdentity = createWorkerIdentity({
env: { name: 'proxer-worker-7' }, pid: 12345, host: 'download-host', now: () => now,
uuid: () => '7b3c9e3a-8b0b-41c6-988c-8e9e01dbbf4a',
});
async function temporarySettings(t) {
const outputDir = await fs.mkdtemp(path.join(os.tmpdir(), 'proxer-worker-'));
@@ -267,7 +272,7 @@ test('the final pause gate releases a never-started claim without history or del
assert.equal(result.errorCategory, 'paused');
assert.equal(spawns, 0);
assert.deepEqual(operations, [{ collection: 'queue', method: 'updateOne',
args: [{ _id: 'fixture-job', processing: true }, { $unset: { processing: '', started_at: '' } }],
args: [{ _id: 'fixture-job', processing: true }, { $unset: { processing: '', started_at: '', worker: '' } }],
}]);
});
@@ -449,7 +454,7 @@ test('authentication and resolution pause the pool before history and preserve t
assert.doesNotMatch(operations[1].args[0].output_log, /fixture-secret/);
assert.equal(operations[1].args[0].token, undefined);
assert.deepEqual(operations[2].args, [
{ _id: job._id, processing: true }, { $unset: { processing: '', started_at: '' } },
{ _id: job._id, processing: true }, { $unset: { processing: '', started_at: '', worker: '' } },
]);
assert.equal(job.token, 'fixture_cookie=fixture-secret==');
}
@@ -496,6 +501,7 @@ test('unacknowledged attention writes halt without consuming the request', async
function claimDatabase({ pauseReads = [false, false], readFailureAt = -1, afterClaim = () => {} } = {}) {
const operations = [];
const claims = [];
let readCount = 0;
const job = makeJob();
const db = { collection(name) {
@@ -507,18 +513,49 @@ function claimDatabase({ pauseReads = [false, false], readFailureAt = -1, afterC
} };
assert.equal(name, 'queue');
return {
findOneAndUpdate: async () => { operations.push('queue.claim'); afterClaim(); return job; },
findOneAndUpdate: async (...args) => { operations.push('queue.claim'); claims.push(args); afterClaim(); return job; },
updateOne: async (filter, update) => {
operations.push('queue.release');
assert.deepEqual(filter, { _id: job._id, processing: true });
assert.deepEqual(update, { $unset: { processing: '', started_at: '' } });
assert.deepEqual(update, { $unset: { processing: '', started_at: '', worker: '' } });
return { acknowledged: true };
},
};
} };
return { db, operations, job };
return { db, operations, job, claims };
}
test('a worker identity is recorded in the atomic claim alongside its start time', async () => {
const fixture = claimDatabase();
await claimNextJob(fixture.db, { workerIdentity: { ...workerIdentity, token: 'must-not-be-copied' }, now: () => now });
assert.deepEqual(fixture.claims, [[
{ processing: { $ne: true } },
{ $set: { processing: true, started_at: now, worker: workerIdentity } },
{ sort: { created_at: 1 }, returnDocument: 'after' },
]]);
});
test('a claim without valid identity cannot retain stale worker telemetry from an earlier attempt', async () => {
const fixture = claimDatabase();
await claimNextJob(fixture.db, { workerIdentity: { ...workerIdentity, pid: '12345' }, now: () => now });
assert.deepEqual(fixture.claims[0][1], {
$set: { processing: true, started_at: now }, $unset: { worker: '' },
});
});
test('history retains only validated worker metadata without changing claim transitions', async () => {
for (const ok of [true, false]) {
const { db, operations } = recordingDatabase();
await processClaimedJob(makeJob({ worker: { ...workerIdentity, token: 'private-worker-data' } }), {
db, execute: async () => ({ ok, exitCode: ok ? 0 : 1, errorCategory: 'resolution', output: 'fixture' }),
});
const history = operations.find(entry => entry.method === 'insertOne').args[0];
assert.deepEqual(history.worker, workerIdentity);
assert.doesNotMatch(JSON.stringify(history), /private-worker-data/);
if (!ok) assert.deepEqual(operations.at(-1).args[1], { $unset: { processing: '', started_at: '', worker: '' } });
}
});
test('a paused pool does not claim a new job', async () => {
const fixture = claimDatabase({ pauseReads: [true] });
assert.equal(await claimNextJob(fixture.db), null);
@@ -567,7 +604,7 @@ test('interrupted jobs release only their own claim without creating history or
assert.equal(result.errorCategory, 'interrupted');
assert.deepEqual(operations, [{
collection: 'queue', method: 'updateOne',
args: [{ _id: job._id, processing: true }, { $unset: { processing: '', started_at: '' } }],
args: [{ _id: job._id, processing: true }, { $unset: { processing: '', started_at: '', worker: '' } }],
}]);
});
@@ -580,7 +617,7 @@ test('infrastructure failures preserve the request, release its claim, and rejec
}) }), /infrastructure is unavailable.*remains queued/);
assert.deepEqual(operations, [{
collection: 'queue', method: 'updateOne',
args: [{ _id: job._id, processing: true }, { $unset: { processing: '', started_at: '' } }],
args: [{ _id: job._id, processing: true }, { $unset: { processing: '', started_at: '', worker: '' } }],
}]);
}
});