Skip to content

src/backfill-continue.ts

30 lines · 1.1 KB · TypeScript
Raw
import { workflows } from 'kody:runtime'
import backfill from './backfill.ts'

/**
 * Keep backfilling countable commits until the tracked branch is fully stored.
 * Each run ingests one batch, then enqueues the next batch while it is still
 * storing new commits.
 *
 * @returns The batch that just finished and whether another run was queued
 *
 * @example
 * import continueBackfill from 'kody:@kentcdodds/lineage/backfill-continue'
 *
 * const progress = await continueBackfill()
 */
export default async function continueBackfill() {
	const result = await backfill({ limit: 80, deadlineMs: 55_000 })
	let continued = false
	// Only chain when this batch made progress; a deadline-shortened listing
	// with nothing new to ingest would otherwise re-enqueue the same batch.
	if (!result.done && !result.deferred && result.ingested > 0) {
		await workflows.create({
			exportName: './backfill-continue',
			params: {},
			idempotencyKey: `lineage:backfill:${result.ingest.storedCommits}:${result.ingest.newest?.sha ?? 'none'}`,
		})
		continued = true
	}
	return { ...result, continued }
}