Never prune queued/running builds; dedup manual triggers
Two defects seen live on vm4006 when a merged branch was deleted from origin while its last build still ran: - The result prune removed the PENDING/RUNNING entries of branches gone from origin, so the executing build vanished from UI and history and the queue looked stuck. Prune now never touches a PENDING or RUNNING entry (worktrees were already protected). The startup recovery closes out an orphaned PENDING of a gone branch as INTERRUPTED, so the new immunity cannot leak entries. - The apparent hang invited restart clicks, and each click stacked another build of the same commit. startBuild now returns the already queued or executing build of the same branch and commit instead of a duplicate; cancel-requested builds do not block re-queueing, and re-running a finished build stays possible. Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -63,12 +63,24 @@ class BuildExecutor(
|
||||
* Persists a PENDING result and queues the build; returns immediately.
|
||||
* A build of the same branch waits until the branch's previous build finished;
|
||||
* builds of other branches run concurrently while slots are free.
|
||||
* While a build of the same branch and commit is already queued or executing (and
|
||||
* not cancel-requested), that build is returned instead of stacking a duplicate —
|
||||
* a double-triggered UI restart must not queue the same commit twice. Re-running
|
||||
* a *finished* build stays possible; this only guards the active queue.
|
||||
*/
|
||||
fun startBuild(
|
||||
branch: String,
|
||||
commit: String,
|
||||
workingDir: Path = Paths.get("."),
|
||||
): RunningBuild {
|
||||
val duplicate =
|
||||
builds.values.firstOrNull {
|
||||
!it.cancelled.get() && it.runningBuild.branch == branch && it.runningBuild.commit == commit
|
||||
}
|
||||
if (duplicate != null) {
|
||||
log.info("build of branch {} at commit {} is already queued or running; not queueing a duplicate", branch, commit)
|
||||
return duplicate.runningBuild
|
||||
}
|
||||
val startedAt = Instant.now()
|
||||
val stagingDir = Files.createTempDirectory("gittally-build-")
|
||||
val runningBuild =
|
||||
|
||||
@@ -43,7 +43,11 @@ interface BuildResultRepository {
|
||||
* cutoff are dropped even within the retention count — except each branch's newest entry,
|
||||
* so dormant branches keep their last status. With [keepLatestGreen], the newest SUCCESS
|
||||
* entry of each surviving branch is kept even beyond both limits, so the permanent
|
||||
* `/branches/…` artifact links stay valid while newer builds fail. Returns the removed entries.
|
||||
* `/branches/…` artifact links stay valid while newer builds fail.
|
||||
* PENDING and RUNNING entries are never removed, regardless of all limits and even
|
||||
* when their branch is gone from [originBranches] — a queued or executing build
|
||||
* belongs to the executor, and pruning its result would make it invisible in UI
|
||||
* and history. Returns the removed entries.
|
||||
*/
|
||||
fun prune(
|
||||
originBranches: Collection<String>,
|
||||
|
||||
@@ -129,26 +129,32 @@ class FileBuildResultRepository(
|
||||
synchronized(lock) {
|
||||
val results = load()
|
||||
val originBranchSet = originBranches.toSet()
|
||||
// a queued or executing build belongs to the executor, never to retention:
|
||||
// pruning its result would make the build invisible in UI and history — seen
|
||||
// live when a merged branch was deleted from origin while its last build ran
|
||||
val active =
|
||||
results.filter { it.status == BuildStatus.PENDING || it.status == BuildStatus.RUNNING }
|
||||
val kept =
|
||||
results
|
||||
.filter { it.branch in originBranchSet }
|
||||
.groupBy { it.branch }
|
||||
.values
|
||||
.flatMap { entries ->
|
||||
val newest =
|
||||
entries
|
||||
.sortedByDescending { it.startedAt }
|
||||
.take(retentionPerBranch.coerceAtLeast(0))
|
||||
.filterIndexed { index, entry ->
|
||||
// the branch's newest entry is never age-pruned
|
||||
index == 0 || retentionCutoff == null || !entry.startedAt.isBefore(retentionCutoff)
|
||||
}
|
||||
val latestGreen =
|
||||
entries
|
||||
.filter { keepLatestGreen && it.status == BuildStatus.SUCCESS }
|
||||
.maxByOrNull { it.startedAt }
|
||||
newest + listOfNotNull(latestGreen)
|
||||
}.toSet()
|
||||
active.toSet() +
|
||||
results
|
||||
.filter { it.branch in originBranchSet }
|
||||
.groupBy { it.branch }
|
||||
.values
|
||||
.flatMap { entries ->
|
||||
val newest =
|
||||
entries
|
||||
.sortedByDescending { it.startedAt }
|
||||
.take(retentionPerBranch.coerceAtLeast(0))
|
||||
.filterIndexed { index, entry ->
|
||||
// the branch's newest entry is never age-pruned
|
||||
index == 0 || retentionCutoff == null || !entry.startedAt.isBefore(retentionCutoff)
|
||||
}
|
||||
val latestGreen =
|
||||
entries
|
||||
.filter { keepLatestGreen && it.status == BuildStatus.SUCCESS }
|
||||
.maxByOrNull { it.startedAt }
|
||||
newest + listOfNotNull(latestGreen)
|
||||
}.toSet()
|
||||
val removed = results.filterNot { it in kept }
|
||||
if (removed.isNotEmpty()) {
|
||||
save(results.filter { it in kept })
|
||||
|
||||
@@ -97,6 +97,11 @@ class Watcher(
|
||||
for (result in restartable) {
|
||||
val commit = gitService.originHeadCommit(result.branch, workingDir)
|
||||
if (commit == null) {
|
||||
if (result.status == BuildStatus.PENDING) {
|
||||
// a PENDING entry is prune-immune (it normally belongs to the executor);
|
||||
// close this orphan out so the gone branch can be pruned
|
||||
repository.updateByArtifactKey(result.artifactKey) { it.copy(status = BuildStatus.INTERRUPTED) }
|
||||
}
|
||||
log.info("not restarting build of branch {}: branch is gone from origin", result.branch)
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user