Skip to content

Commit e351779

Browse files
improved stuck job handling
1 parent 0431e2d commit e351779

2 files changed

Lines changed: 42 additions & 1 deletion

File tree

Models/RunnerQueue.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,17 @@ public void Enqueue(CreateRunnerTask task)
6868
context.SaveChanges();
6969
}
7070

71+
public int CountMatchingRunners(string size, string owner, string profile)
72+
{
73+
using var context = new ActionsRunnerContext();
74+
return context.CreateTaskQueues
75+
.Join(context.Runners,
76+
ctq => ctq.RunnerDbId,
77+
r => r.RunnerId,
78+
(ctq, r) => r)
79+
.Count(r => r.Size == size && r.Owner == owner && r.Profile == profile);
80+
}
81+
7182
public bool TryDequeue(out CreateRunnerTask? task)
7283
{
7384
using var context = new ActionsRunnerContext();
@@ -229,6 +240,17 @@ public bool Remove(string hostname, out CreateRunnerTask? task)
229240
{
230241
return TryRemove(hostname, out task);
231242
}
243+
244+
public int CountMatchingRunners(string size, string owner, string profile)
245+
{
246+
using var context = new ActionsRunnerContext();
247+
return context.CreatedRunnersTrackings
248+
.Join(context.Runners,
249+
crt => crt.RunnerDbId,
250+
r => r.RunnerId,
251+
(crt, r) => r)
252+
.Count(r => r.Size == size && r.Owner == owner && r.Profile == profile);
253+
}
232254
}
233255

234256
// Wrapper for CancelledRunnersCounter to mimic ConcurrentDictionary<(string, string, string, string, string), int>

PoolManager.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,26 @@ private async Task CheckForStuckJobs(List<GithubTargetConfiguration> targetConfi
593593
continue;
594594
}
595595

596+
// Count-based check: compare matching runners in pipeline vs stuck jobs needing them
597+
var profile = stuckJob.RequestedProfile ?? "default";
598+
int matchingRunnersInPipeline =
599+
_queues.CreateTasks.CountMatchingRunners(stuckJob.RequestedSize, stuckJob.Owner, profile)
600+
+ _queues.CreatedRunners.CountMatchingRunners(stuckJob.RequestedSize, stuckJob.Owner, profile);
601+
602+
int stuckJobsWithSameRequirements = stuckJobs.Count(j =>
603+
j.RequestedSize == stuckJob.RequestedSize
604+
&& j.Owner == stuckJob.Owner
605+
&& (j.RequestedProfile ?? "default") == profile);
606+
607+
if (matchingRunnersInPipeline >= stuckJobsWithSameRequirements)
608+
{
609+
_logger.LogInformation(
610+
"Enough runners in pipeline ({RunnerCount}) for stuck jobs ({JobCount}) with Size={Size}, Owner={Owner}, Profile={Profile}. Skipping replacement for job {JobId}.",
611+
matchingRunnersInPipeline, stuckJobsWithSameRequirements,
612+
stuckJob.RequestedSize, stuckJob.Owner, profile, stuckJob.JobId);
613+
continue;
614+
}
615+
596616
int replacementsInQueue = _queues.CreateTasks.CountWhere(x => x.IsStuckReplacement);
597617
if (replacementsInQueue > 25)
598618
{
@@ -633,7 +653,6 @@ private async Task CheckForStuckJobs(List<GithubTargetConfiguration> targetConfi
633653
continue;
634654
}
635655

636-
var profile = stuckJob.RequestedProfile ?? "default";
637656
string arch = Program.Config.Sizes.FirstOrDefault(x => x.Name == stuckJob.RequestedSize)?.Arch;
638657
Runner newRunner = new()
639658
{

0 commit comments

Comments
 (0)