Skip to content

Commit 47e8179

Browse files
fix stuck job detection
1 parent cd4695c commit 47e8179

2 files changed

Lines changed: 28 additions & 2 deletions

File tree

GitHubApi.cs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public static async Task<bool> RemoveRunnerFromRepo(string repoName, string orgG
157157

158158
}
159159

160-
public static async Task<GitHubJob> GetJobInfo(long stuckJobGithubJobId,string repoName, string orgGitHubToken)
160+
public static async Task<GitHubJob> GetJobInfoForOrg(long stuckJobGithubJobId,string repoName, string orgGitHubToken)
161161
{
162162
using HttpClient client = new();
163163
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github+json"));
@@ -176,6 +176,27 @@ public static async Task<GitHubJob> GetJobInfo(long stuckJobGithubJobId,string r
176176
}
177177
Log.Warning($"Unable to get GH job info for {repoName}/{stuckJobGithubJobId}: [{response.StatusCode}] {response.ReasonPhrase}");
178178

179+
return null;
180+
}
181+
public static async Task<GitHubJob> GetJobInfoForRepo(long stuckJobGithubJobId,string repoName, string orgGitHubToken)
182+
{
183+
using HttpClient client = new();
184+
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/vnd.github+json"));
185+
client.DefaultRequestHeaders.Add("X-GitHub-Api-Version", "2022-11-28");
186+
client.DefaultRequestHeaders.Authorization = AuthenticationHeaderValue.Parse($"Bearer {orgGitHubToken}");
187+
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue("hetzner-autoscale", "1"));
188+
189+
HttpResponseMessage response = await client.GetAsync(
190+
$"https://api.github.com/repos/{repoName}/actions/jobs/{stuckJobGithubJobId}");
191+
if(response.IsSuccessStatusCode)
192+
{
193+
string content = await response.Content.ReadAsStringAsync();
194+
GitHubJob responseObject = JsonSerializer.Deserialize<GitHubJob>(content);
195+
196+
return responseObject;
197+
}
198+
Log.Warning($"Unable to get GH job info for {repoName}/{stuckJobGithubJobId}: [{response.StatusCode}] {response.ReasonPhrase}");
199+
179200
return null;
180201
}
181202
}

PoolManager.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,12 @@ private async Task CheckForStuckJobs(List<GithubTargetConfiguration> targetConfi
286286
}
287287

288288
// check job on github
289-
GitHubJob ghJob = await GitHubApi.GetJobInfo(stuckJob.GithubJobId, owner.Name, owner.GitHubToken);
289+
GitHubJob ghJob = owner.Target switch
290+
{
291+
TargetType.Repository => await GitHubApi.GetJobInfoForRepo(stuckJob.GithubJobId, owner.Name, owner.GitHubToken),
292+
TargetType.Organization => await GitHubApi.GetJobInfoForOrg(stuckJob.GithubJobId, owner.Name, owner.GitHubToken),
293+
_ => throw new ArgumentOutOfRangeException()
294+
};
290295
if (ghJob == null || ghJob.Status != "queued")
291296
{
292297
_logger.LogWarning($"job info for {stuckJob.JobId} not found or job not queued anymore.");

0 commit comments

Comments
 (0)