Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions packages/das/src/webhook/repo-backfill-schedule.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,22 @@ const BACKFILL_TZ = "America/Chicago";
const BACKFILL_DAYS = Number(
process.env.NIGHTLY_BACKFILL_DAYS ?? DEFAULT_BACKFILL_DAYS,
);
const MIN_BACKFILL_DAYS = 1;
const MAX_BACKFILL_DAYS = 365;

function parseBackfillDays(rawValue: number): number {
if (!Number.isFinite(rawValue)) return DEFAULT_BACKFILL_DAYS;
const normalized = Math.floor(rawValue);
if (normalized < MIN_BACKFILL_DAYS || normalized > MAX_BACKFILL_DAYS) {
return DEFAULT_BACKFILL_DAYS;
}
return normalized;
}

@Injectable()
export class RepoBackfillScheduleService implements OnModuleInit {
private readonly logger = new Logger(RepoBackfillScheduleService.name);
private readonly backfillDays = parseBackfillDays(BACKFILL_DAYS);

constructor(
@InjectRepository(Repo)
Expand All @@ -44,6 +56,12 @@ export class RepoBackfillScheduleService implements OnModuleInit {
);
return;
}
if (this.backfillDays !== BACKFILL_DAYS) {
this.logger.warn(
`Invalid NIGHTLY_BACKFILL_DAYS=${String(process.env.NIGHTLY_BACKFILL_DAYS)}; ` +
`using ${this.backfillDays} days`,
);
}
this.logger.log(
`Nightly repo backfill scheduled '${BACKFILL_CRON}' (${BACKFILL_TZ})`,
);
Expand All @@ -62,13 +80,13 @@ export class RepoBackfillScheduleService implements OnModuleInit {

this.logger.log(
`Enqueuing nightly backfill for ${repos.length} repos ` +
`(last ${BACKFILL_DAYS}d)`,
`(last ${this.backfillDays}d)`,
);

for (const repo of repos) {
await this.fetchQueue.add(
FETCH_JOBS.BACKFILL_REPO,
{ repoFullName: repo.repoFullName, days: BACKFILL_DAYS },
{ repoFullName: repo.repoFullName, days: this.backfillDays },
{
// Static per-repo jobId so a still-running nightly backfill isn't
// stacked on by the next tick. Distinct from the admin endpoint's
Expand Down