Queue the smallest backfill jobs first and show queued state

This commit is contained in:
Fisto
2026-09-17 09:05:22 +02:00
parent 6ae2ff5ab3
commit c6a16a63f7
4 changed files with 22 additions and 8 deletions
+12 -4
View File
@@ -40,7 +40,8 @@ export interface BackfillMember {
export interface BackfillJobStatus {
chatId: string;
guildId: string | null;
state: "idle" | "running" | "paused" | "done";
/** `queued` means started, but another job holds the queue. */
state: "idle" | "queued" | "running" | "paused" | "done";
members: BackfillMember[];
pending: number;
cached: number;
@@ -188,6 +189,8 @@ export class BackfillQueue {
const groups = this.plan();
const jobs: BackfillJobStatus[] = [];
const anyClaimed = [...this.jobs.values()].some(candidate => candidate.claimed);
for (const [chatId, group] of groups) {
// Hide chats that are fully backfilled unless their job did work this session.
const tracked = this.jobs.get(chatId);
@@ -196,11 +199,13 @@ export class BackfillQueue {
const perMinute = tracked?.perMinute ?? this.estimatedPerMinute(settings, gaps);
const etaMs = pending > 0 && perMinute > 0 ? Math.round((pending / perMinute) * 60_000) : null;
const state =
tracked?.state === "running" && anyClaimed && !tracked.claimed ? "queued" : (tracked?.state ?? "idle");
jobs.push({
chatId,
guildId: group.guildId,
state: tracked?.state ?? "idle",
state,
members: group.members,
pending,
cached: group.members.reduce((sum, member) => sum + member.cached, 0),
@@ -280,11 +285,14 @@ export class BackfillQueue {
return perRow > 0 ? 60_000 / perRow : 6000;
}
/** Start every job with pending rows, or one chat's job. */
/** Start every job with pending rows, or one chat's job. Smallest jobs first: they finish. */
start(chatId?: string): void {
const status = this.list();
const order = chatId
? status.jobs
: [...status.jobs].sort((a, b) => a.pending - b.pending);
for (const job of status.jobs) {
for (const job of order) {
if (chatId && job.chatId !== chatId) continue;
if (job.pending <= 0) continue;