From a0a344ba99097fb7a1be959c11c0458558f3b600 Mon Sep 17 00:00:00 2001 From: Mario Constanti Date: Tue, 4 Jun 2024 14:30:50 +0200 Subject: [PATCH] chore: restructure fetching runner details via gh Signed-off-by: Mario Constanti --- runner/pool/pool.go | 46 ++++++++++++++++++++++++++++----------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/runner/pool/pool.go b/runner/pool/pool.go index 4650e05c..19818982 100644 --- a/runner/pool/pool.go +++ b/runner/pool/pool.go @@ -213,6 +213,20 @@ func (r *basePoolManager) HandleWorkflowJob(job params.WorkflowJob) error { return errors.Wrap(err, "converting job to params") } + // Runner name was not set in WorkflowJob by github. We can still attempt to fetch the info we need, + // using the workflow run ID, from the API. + if job.WorkflowJob.RunnerName == "" { + // We may still get no runner name. In situations such as jobs being cancelled before a runner had the chance + // to pick up the job, the runner name is not available from the API. + if job.WorkflowJob.Conclusion != "skipped" && job.WorkflowJob.Conclusion != "canceled" { + runnerInfo, err := r.getRunnerDetailsFromJob(job) + if err != nil && !errors.Is(err, runnerErrors.ErrNotFound) { + return errors.Wrap(err, "fetching runner details") + } + jobParams.RunnerName = runnerInfo.Name + } + } + // update instance workload state. if _, err := r.setInstanceRunnerStatus(jobParams.RunnerName, params.RunnerTerminated); err != nil { if errors.Is(err, runnerErrors.ErrNotFound) { @@ -249,6 +263,20 @@ func (r *basePoolManager) HandleWorkflowJob(job params.WorkflowJob) error { return errors.Wrap(err, "converting job to params") } + // Runner name was not set in WorkflowJob by github. We can still attempt to fetch the info we need, + // using the workflow run ID, from the API. + if job.WorkflowJob.RunnerName == "" { + // We may still get no runner name. In situations such as jobs being cancelled before a runner had the chance + // to pick up the job, the runner name is not available from the API. + if job.WorkflowJob.Conclusion != "skipped" && job.WorkflowJob.Conclusion != "canceled" { + runnerInfo, err := r.getRunnerDetailsFromJob(job) + if err != nil && !errors.Is(err, runnerErrors.ErrNotFound) { + return errors.Wrap(err, "fetching runner details") + } + jobParams.RunnerName = runnerInfo.Name + } + } + // update instance workload state. instance, err := r.setInstanceRunnerStatus(jobParams.RunnerName, params.RunnerActive) if err != nil { @@ -957,6 +985,7 @@ func (r *basePoolManager) paramsWorkflowJobToParamsJob(job params.WorkflowJob) ( StartedAt: job.WorkflowJob.StartedAt, CompletedAt: job.WorkflowJob.CompletedAt, Name: job.WorkflowJob.Name, + RunnerName: job.WorkflowJob.RunnerName, GithubRunnerID: job.WorkflowJob.RunnerID, RunnerGroupID: job.WorkflowJob.RunnerGroupID, RunnerGroupName: job.WorkflowJob.RunnerGroupName, @@ -965,23 +994,6 @@ func (r *basePoolManager) paramsWorkflowJobToParamsJob(job params.WorkflowJob) ( Labels: job.WorkflowJob.Labels, } - runnerName := job.WorkflowJob.RunnerName - if job.Action != "queued" && runnerName == "" { - if job.WorkflowJob.Conclusion != "skipped" && job.WorkflowJob.Conclusion != "canceled" { - // Runner name was not set in WorkflowJob by github. We can still attempt to fetch the info we need, - // using the workflow run ID, from the API. - // We may still get no runner name. In situations such as jobs being cancelled before a runner had the chance - // to pick up the job, the runner name is not available from the API. - runnerInfo, err := r.getRunnerDetailsFromJob(job) - if err != nil && !errors.Is(err, runnerErrors.ErrNotFound) { - return jobParams, errors.Wrap(err, "fetching runner details") - } - runnerName = runnerInfo.Name - } - } - - jobParams.RunnerName = runnerName - switch r.entity.EntityType { case params.GithubEntityTypeEnterprise: jobParams.EnterpriseID = &asUUID