|
1 | 1 | import { CiBuilds } from '../../model/ciBuilds'; |
| 2 | +import { CiJobs } from '../../model/ciJobs'; |
2 | 3 | import { Discord } from '../../service/discord'; |
3 | 4 | import { Dockerhub } from '../../service/dockerhub'; |
4 | 5 |
|
5 | 6 | export class Cleaner { |
6 | 7 | // Cronjob intentionally has a limited runtime, but it still needs enough |
7 | 8 | // throughput to drain queue backlogs without requiring manual intervention. |
8 | 9 | static readonly maxBuildsProcessedPerRun: number = 25; |
| 10 | + static readonly activeJobWithoutBuildsAfterMinutes: number = 30; |
9 | 11 | static readonly startedBuildPublishProbeAfterMinutes: number = 45; |
10 | 12 | static readonly startedBuildFailureAfterHours: number = 6; |
11 | 13 |
|
12 | 14 | static buildsProcessed: number; |
13 | 15 |
|
14 | 16 | public static async cleanUp(latestRepoVersion: string) { |
15 | 17 | this.buildsProcessed = 0; |
| 18 | + await this.requeueActiveJobsWithoutBuilds(); |
16 | 19 | await this.recoverMaxedOutFailedBuilds(latestRepoVersion); |
17 | 20 | await this.reconcileStartedBuildsThatMayHavePublished(); |
18 | 21 | await this.cleanUpBuildsThatDidntReportBack(); |
19 | 22 | await this.healFailedBuildsAlreadyOnDockerHub(); |
20 | 23 | } |
21 | 24 |
|
| 25 | + /** |
| 26 | + * Jobs can get stuck in "scheduled" or "inProgress" without ever creating a |
| 27 | + * build record when the dispatch path flakes before reportNewBuild. Reset |
| 28 | + * those jobs back to created so the scheduler can dispatch them again. |
| 29 | + */ |
| 30 | + private static async requeueActiveJobsWithoutBuilds() { |
| 31 | + const activeJobs = await CiJobs.getActiveJobs(); |
| 32 | + const staleThresholdMs = this.activeJobWithoutBuildsAfterMinutes * 60 * 1000; |
| 33 | + |
| 34 | + for (const activeJob of activeJobs) { |
| 35 | + if (this.buildsProcessed >= this.maxBuildsProcessedPerRun) return; |
| 36 | + |
| 37 | + const { id: jobId, data: job } = activeJob; |
| 38 | + const lastTouchedSeconds = job.modifiedDate?.seconds || job.addedDate?.seconds; |
| 39 | + if (!lastTouchedSeconds) continue; |
| 40 | + |
| 41 | + const ageMs = Date.now() - lastTouchedSeconds * 1000; |
| 42 | + if (ageMs < staleThresholdMs) continue; |
| 43 | + |
| 44 | + const hasBuilds = await CiBuilds.hasAnyBuildsForJob(jobId); |
| 45 | + if (hasBuilds) continue; |
| 46 | + |
| 47 | + this.buildsProcessed += 1; |
| 48 | + await CiJobs.resetJobToCreated(jobId); |
| 49 | + await Discord.sendAlert( |
| 50 | + `[Cleaner] Reset stale ${job.status} job "${jobId}" back to created because it had no build records after ${this.activeJobWithoutBuildsAfterMinutes} minutes.`, |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | + |
22 | 55 | /** |
23 | 56 | * Automatically recover maxed-out failed builds for the latest repo version. |
24 | 57 | * If the image is already on DockerHub, mark it as published. |
|
0 commit comments