Harvester pauses user refreshes after AniList stops (BL-0051)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
23a87099c5
commit
552a599475
2 files changed
+116
-13
No files matched your search
+30
-8
@@ -55,7 +55,8 @@ async function runHarvest({ kind, targets, cache, client, budget }) {
|
||||
// With nobody to harvest for, even the global list is not worth a request.
|
||||
if (targets.length) await fetchInto('fresh', 'global', () => client.fresh());
|
||||
} catch (error) {
|
||||
if (!(error instanceof StopRun)) throw error;
|
||||
// The caller records how many requests a local fault had already spent.
|
||||
if (!(error instanceof StopRun)) throw Object.assign(error, { requests });
|
||||
return { requests, budget, status: error.status, stopReason: error.stopReason };
|
||||
}
|
||||
return { requests, budget, status: 'success', stopReason: null };
|
||||
@@ -74,12 +75,20 @@ function shouldRunNightly({ now, harvestAt, lastNightlyDate, enabled }) {
|
||||
|
||||
const httpError = (status, message, extra = {}) => Object.assign(new Error(message), { status }, extra);
|
||||
|
||||
// These reasons mean AniList pushed back: no further calls until a nightly or admin run.
|
||||
const BLOCKING_REASONS = ['rate-limited', 'forbidden', 'challenge'];
|
||||
const blockingReason = record => record?.status === 'stopped' && BLOCKING_REASONS.includes(record.stopReason)
|
||||
? record.stopReason : null;
|
||||
|
||||
class Harvester {
|
||||
constructor({ cache, client, store, targetsFor, onDone, now = Date.now }) {
|
||||
Object.assign(this, { cache, client, store, targetsFor, onDone, now });
|
||||
this.queue = [];
|
||||
this.active = null;
|
||||
this.lastAccepted = new Map();
|
||||
// The pause outlives a restart through the persisted harvest record.
|
||||
try { this.blockedReason = blockingReason(store.load?.()?.harvest); }
|
||||
catch { this.blockedReason = null; }
|
||||
}
|
||||
nextAllowedAt(user) {
|
||||
const last = this.lastAccepted.get(user);
|
||||
@@ -88,16 +97,21 @@ class Harvester {
|
||||
}
|
||||
jobs() { return [this.active, ...this.queue].filter(Boolean); }
|
||||
request({ kind, user }) {
|
||||
if (!(kind in BUDGETS)) throw httpError(400, `Unknown harvest kind: ${kind}`);
|
||||
if (!Object.hasOwn(BUDGETS, kind)) throw httpError(400, `Unknown harvest kind: ${kind}`);
|
||||
if (kind === 'user') {
|
||||
if (!user) throw httpError(400, 'A user refresh needs a user');
|
||||
if (this.jobs().some(job => job.kind === 'user' && job.user === user))
|
||||
throw httpError(409, 'A refresh for this user is already queued or running');
|
||||
if (this.blockedReason)
|
||||
throw httpError(503, `AniList paused the harvest: ${this.blockedReason}`, { blockedReason: this.blockedReason });
|
||||
const nextAllowedAt = this.nextAllowedAt(user);
|
||||
if (nextAllowedAt) throw httpError(429, 'Refresh is cooling down', { nextAllowedAt });
|
||||
this.lastAccepted.set(user, this.now());
|
||||
} else if (this.jobs().length) {
|
||||
throw httpError(409, 'A harvest is already queued or running');
|
||||
} else {
|
||||
// An accepted nightly or admin run is the one allowed retry after a pause.
|
||||
this.blockedReason = null;
|
||||
}
|
||||
const job = { id: crypto.randomBytes(8).toString('hex'), kind, ...(kind === 'user' ? { user } : {}) };
|
||||
const position = this.jobs().length;
|
||||
@@ -107,17 +121,23 @@ class Harvester {
|
||||
}
|
||||
state(userName) {
|
||||
const nextAllowedAt = this.nextAllowedAt(userName);
|
||||
const cooldown = nextAllowedAt ? { nextAllowedAt } : {};
|
||||
const extra = { ...(nextAllowedAt ? { nextAllowedAt } : {}), ...(this.blockedReason ? { blockedReason: this.blockedReason } : {}) };
|
||||
const index = this.jobs().findIndex(job => job.kind === 'user' && job.user === userName);
|
||||
if (index === 0 && this.active) return { state: 'running', ...cooldown };
|
||||
if (index >= 0) return { state: 'queued', position: index, ...cooldown };
|
||||
return { state: 'idle', ...cooldown };
|
||||
if (index === 0 && this.active) return { state: 'running', ...extra };
|
||||
if (index >= 0) return { state: 'queued', position: index, ...extra };
|
||||
return { state: 'idle', ...extra };
|
||||
}
|
||||
current() {
|
||||
if (!this.active) return null;
|
||||
const { id, kind, user, startedAt } = this.active;
|
||||
return { id, kind, ...(user ? { user } : {}), startedAt };
|
||||
}
|
||||
// Queued refreshes are dropped unrun; their users get their cooldown back for after the pause.
|
||||
pause(reason) {
|
||||
this.blockedReason = reason;
|
||||
for (const job of this.queue) if (job.kind === 'user') this.lastAccepted.delete(job.user);
|
||||
this.queue = this.queue.filter(job => job.kind !== 'user');
|
||||
}
|
||||
// Runs synchronously up to the first await, so a job is active before request() returns.
|
||||
async drain() {
|
||||
while (this.queue.length) {
|
||||
@@ -135,13 +155,15 @@ class Harvester {
|
||||
await this.store.patch({ harvest: { ...base, status: 'running', startedAt: job.startedAt, requests: 0, budget } });
|
||||
let result;
|
||||
try {
|
||||
const targets = this.targetsFor(job.kind, job.user);
|
||||
const targets = await this.targetsFor(job.kind, job.user);
|
||||
result = await runHarvest({ kind: job.kind, targets, cache: this.cache, client: this.client, budget });
|
||||
} catch (error) {
|
||||
// A local fault (cache write, bad target) must not leave the record "running" until restart.
|
||||
console.error(`Discovery harvest ${job.id} failed:`, error);
|
||||
result = { requests: 0, budget, status: 'stopped', stopReason: 'error' };
|
||||
result = { requests: error?.requests ?? 0, budget, status: 'stopped', stopReason: 'error' };
|
||||
}
|
||||
const blocked = blockingReason(result);
|
||||
if (blocked) this.pause(blocked);
|
||||
await this.store.patch({ harvest: { ...base, status: result.status, startedAt: job.startedAt,
|
||||
finishedAt: new Date(this.now()).toISOString(), requests: result.requests, budget, stopReason: result.stopReason } });
|
||||
try { await this.onDone(job, result); }
|
||||
|
||||
Reference in new issue
Block a user