Skip to content

Commit 7ff4f70

Browse files
add job vanish detection
1 parent d9a6d6f commit 7ff4f70

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

GitHubApi.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ public static async Task<bool> RemoveRunnerFromRepo(string repoName, string orgG
135135
return response.IsSuccessStatusCode;
136136
}
137137

138-
public static async Task<GitHubApiWorkflowRun> GetJobInfoForOrg(long stuckJobGithubJobId,string repoName, string orgGitHubToken)
138+
public static async Task<(GitHubApiWorkflowRun Job, System.Net.HttpStatusCode StatusCode)> GetJobInfoForOrg(long stuckJobGithubJobId,string repoName, string orgGitHubToken)
139139
{
140140
using var request = CreateRequest(HttpMethod.Get, $"https://api.github.com/orgs/{repoName}/actions/jobs/{stuckJobGithubJobId}", orgGitHubToken);
141141
HttpResponseMessage response = await SharedClient.SendAsync(request);
@@ -144,13 +144,13 @@ public static async Task<GitHubApiWorkflowRun> GetJobInfoForOrg(long stuckJobGit
144144
string content = await response.Content.ReadAsStringAsync();
145145
GitHubApiWorkflowRun responseObject = JsonSerializer.Deserialize<GitHubApiWorkflowRun>(content);
146146

147-
return responseObject;
147+
return (responseObject, response.StatusCode);
148148
}
149149
Log.Warning($"Unable to get GH job info for {repoName}/{stuckJobGithubJobId}: [{response.StatusCode}] {response.ReasonPhrase}");
150150

151-
return null;
151+
return (null, response.StatusCode);
152152
}
153-
public static async Task<GitHubApiWorkflowRun> GetJobInfoForRepo(long stuckJobGithubJobId,string repoName, string orgGitHubToken)
153+
public static async Task<(GitHubApiWorkflowRun Job, System.Net.HttpStatusCode StatusCode)> GetJobInfoForRepo(long stuckJobGithubJobId,string repoName, string orgGitHubToken)
154154
{
155155
using var request = CreateRequest(HttpMethod.Get, $"https://api.github.com/repos/{repoName}/actions/jobs/{stuckJobGithubJobId}", orgGitHubToken);
156156
HttpResponseMessage response = await SharedClient.SendAsync(request);
@@ -159,10 +159,10 @@ public static async Task<GitHubApiWorkflowRun> GetJobInfoForRepo(long stuckJobGi
159159
string content = await response.Content.ReadAsStringAsync();
160160
GitHubApiWorkflowRun responseObject = JsonSerializer.Deserialize<GitHubApiWorkflowRun>(content);
161161

162-
return responseObject;
162+
return (responseObject, response.StatusCode);
163163
}
164164
Log.Warning($"Unable to get GH job info for {repoName}/{stuckJobGithubJobId}: [{response.StatusCode}] {response.ReasonPhrase}");
165165

166-
return null;
166+
return (null, response.StatusCode);
167167
}
168168
}

PoolManager.cs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -607,12 +607,19 @@ private async Task CheckForStuckJobs(List<GithubTargetConfiguration> targetConfi
607607
}
608608

609609
// Verify job is still queued on GitHub before treating it as stuck
610-
GitHubApiWorkflowRun ghJob = await GitHubApi.GetJobInfoForRepo(stuckJob.GithubJobId, stuckJob.Repository , owner.GitHubToken);
610+
var (ghJob, ghStatusCode) = await GitHubApi.GetJobInfoForRepo(stuckJob.GithubJobId, stuckJob.Repository , owner.GitHubToken);
611611
if (ghJob == null || ghJob.Status != "queued")
612612
{
613-
if (ghJob == null)
613+
if (ghJob == null && ghStatusCode == System.Net.HttpStatusCode.NotFound)
614614
{
615-
_logger.LogWarning($"GHjob for {stuckJob.JobId} is null - not actually stuck");
615+
_logger.LogWarning($"Job {stuckJob.GithubJobId} not found on GitHub (404) - marking as vanished.");
616+
stuckJob.State = JobState.Vanished;
617+
stuckJob.CompleteTime = DateTime.UtcNow;
618+
await db.SaveChangesAsync();
619+
}
620+
else if (ghJob == null)
621+
{
622+
_logger.LogWarning($"Unable to verify job {stuckJob.JobId} on GitHub (HTTP {(int)ghStatusCode}) - skipping this cycle");
616623
}
617624
else if (ghJob.Status == "completed")
618625
{
@@ -1137,7 +1144,7 @@ private async Task CleanUpRunners(List<GithubTargetConfiguration> targetConfigs)
11371144
var targetConfig = Program.Config.TargetConfigs.FirstOrDefault(x => x.Name == runner.Job.Owner);
11381145
if (targetConfig != null)
11391146
{
1140-
var githubJob = await GitHubApi.GetJobInfoForOrg(runner.Job.GithubJobId, runner.Job.Repository, targetConfig.GitHubToken);
1147+
var (githubJob, _) = await GitHubApi.GetJobInfoForOrg(runner.Job.GithubJobId, runner.Job.Repository, targetConfig.GitHubToken);
11411148

11421149
if (githubJob?.Status == "running" || githubJob?.Status == "in_progress")
11431150
{

0 commit comments

Comments
 (0)