// INSTAGRAM'S HALF OF THE NETWORK GUARD (lane instagram; jobs/networkGuard.ts // PLATFORM_GUARDS). Asked of every job aimed at Instagram, at enqueue and // again at its start: // // 1. NOT WHILE ITS ALERT IS UP. A redirect to Instagram's login, challenge // or checkpoint page raises the Instagram alert (lib/serviceCookies.ts): // the next request after one is the one that gets an account locked, so // every Instagram job is refused until a person's Recheck (or Dismiss). // 2. NOT WHILE IT COOLS DOWN. The first 429 (or "wait a few minutes") ends // the run that met it and records a 60-minute `instagram` platform // cooldown (jobs/downloadBackoff.ts recordPlatformCooldown); nothing // Instagram starts before it ends, and nothing retries by itself. // // There is no jar rule here: slice 1 keeps no Instagram jar. import type { Paths } from "../lib/paths"; import { INSTAGRAM_SERVICE, serviceFixText, type ServiceCookieEntry } from "../lib/serviceCookies"; import { raisedServiceAlert } from "../lib/serviceCookies-server"; import { platformCooldownRemainingMs } from "./downloadBackoff"; export const INSTAGRAM_PLATFORM = "instagram"; // How long Instagram is left alone after it answered a rate limit. export const INSTAGRAM_RATE_LIMIT_HOLD_MS = 60 * 60_000; export function instagramCooldownSentence(remainingMs: number, now: number): string { const until = new Date(now + remainingMs).toISOString().slice(11, 16); return ( `Instagram answered a rate limit a short while ago: no Instagram job starts before ${until} UTC ` + `(${Math.ceil(remainingMs / 60_000)} min from now). Ask again then; nothing retries by itself.` ); } export function instagramAlertSentence(raised: Pick): string { const at = (raised.raisedAt ?? raised.since).slice(11, 16); const what = raised.kind === "auth-failure" ? "its login page" : raised.kind === "refusal" ? "a second refusal (401/403) in a row" : "a challenge or checkpoint page"; return ( `Instagram answered with ${what} at ${at} UTC (${raised.evidence.line}) and its alert is up, ` + `so no Instagram job starts: ${serviceFixText(INSTAGRAM_SERVICE)}.` ); } export type InstagramGuardResult = { ok: true } | { ok: false; message: string; alert: boolean }; export type InstagramGuardDeps = { raisedAlert: (paths: Paths) => Promise; cooldownMs: (paths: Paths) => Promise; }; const DEFAULT_DEPS: InstagramGuardDeps = { raisedAlert: (paths) => raisedServiceAlert(paths, INSTAGRAM_SERVICE), cooldownMs: (paths) => platformCooldownRemainingMs(INSTAGRAM_PLATFORM, paths), }; // The cooldown could not be read: the guard FAILS CLOSED. Not knowing whether // Instagram is cooling down is not a reason to ask it. export const INSTAGRAM_COOLDOWN_UNREADABLE = (why: string) => `Whether Instagram is cooling down after a rate limit could not be read (${why}): no Instagram job starts until it can.`; export async function instagramJobGuard(opts: { paths: Paths; now?: number; deps?: Partial; }): Promise { const deps = { ...DEFAULT_DEPS, ...opts.deps }; const now = opts.now ?? Date.now(); const raised = await deps.raisedAlert(opts.paths); if (raised) return { ok: false, message: instagramAlertSentence(raised), alert: true }; let ms: number; try { ms = await deps.cooldownMs(opts.paths); } catch (err) { return { ok: false, message: INSTAGRAM_COOLDOWN_UNREADABLE((err as Error).message.split("\n")[0]!.slice(0, 200)), alert: false }; } if (ms > 0) return { ok: false, message: instagramCooldownSentence(ms, now), alert: false }; return { ok: true }; } // Why no Instagram job starts now, as a sentence, or null — for the scheduler, // which skips every Instagram channel with it rather than queue a fetch the // guard would refuse. export async function instagramHeldReason(paths: Paths, now: number = Date.now()): Promise { const g = await instagramJobGuard({ paths, now }); return g.ok ? null : g.message; }