45 lines · 2.0 KB
import { env } from "cloudflare:workers";
import { ANONYMOUS_AUTH_SCOPE } from "../auth-tokens";
import { backfillAccounts, backfillOnce, backfillRepos } from "../backfill";
import { locatedScopes } from "../scoped-db/locators";
import { indexEditorRepos } from "../editor/container-meter";

/**
 * Cron step until done: arms the housekeeping alarm of accounts whose sessions, tokens and digests were written
 * before AccountState expired them itself. New rows arm the alarm on insert.
 */
export async function armExistingAccounts(): Promise<void> {
  const arm = (scopeId: string) =>
    (
      env.ACCOUNT_STATE.get(env.ACCOUNT_STATE.idFromName(scopeId)) as unknown as {
        armHousekeeping(scopeId: string): Promise<void>;
      }
    ).armHousekeeping(scopeId);
  const done = await backfillAccounts("account-housekeeping", arm);
  if (done) await arm(ANONYMOUS_AUTH_SCOPE);
  // Editor sessions opened before the editor-sessions task need their account's alarm to close them, and their
  // repositories indexed for per-repo listings.
  if (done) {
    await backfillAccounts("editor-session-housekeeping", async (accountId) => {
      await arm(accountId);
      await indexEditorRepos(accountId);
    });
  }
}

/** Cron step until done: arms the RepoState alarm of repositories whose schedules, caches and tokens predate it. */
export async function armExistingRepos(): Promise<void> {
  const arm = (scopeId: string) =>
    (
      env.REPO_STATE.get(env.REPO_STATE.idFromName(scopeId)) as unknown as {
        armHousekeeping(scopeId: string): Promise<void>;
      }
    ).armHousekeeping(scopeId);
  if (!(await backfillRepos("repo-housekeeping", arm))) return;
  // Every site scope once more: repo-less ones (`site:<id>`) have no directory row to walk, and pull aliases left
  // from before the closed-pull-aliases task need it to look.