From 44dda12b2c65d16ab1d52d259532bf3d002d28b2 Mon Sep 17 00:00:00 2001 From: whywaita Date: Mon, 1 Aug 2022 11:40:04 +0900 Subject: [PATCH 1/3] Ephemeral mode is not need runner for queue --- pkg/runner/runner_delete.go | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pkg/runner/runner_delete.go b/pkg/runner/runner_delete.go index 4820fed..fef2789 100644 --- a/pkg/runner/runner_delete.go +++ b/pkg/runner/runner_delete.go @@ -55,10 +55,15 @@ func (m *Manager) removeRunners(ctx context.Context, t datastore.Target) error { return fmt.Errorf("failed to retrieve list of running runner: %w", err) } + _, mode, err := datastore.GetRunnerTemporaryMode(t.RunnerVersion) + if err != nil { + return fmt.Errorf("failed to get runner mode: %w", err) + } + isZero, ghRunners, err := isRegisteredRunnerZeroInGitHub(ctx, t) if err != nil { return fmt.Errorf("failed to check number of registerd runner: %w", err) - } else if isZero && len(runners) == 0 { + } else if isZero && len(runners) == 0 && mode == datastore.RunnerTemporaryOnce { logger.Logf(false, "runner for queueing is not found in %s", t.RepoURL()) if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusErr, "runner for queueing is not found"); err != nil { logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err) @@ -66,6 +71,10 @@ func (m *Manager) removeRunners(ctx context.Context, t datastore.Target) error { return nil } + if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusActive, ""); err != nil { + logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err) + } + sem := semaphore.NewWeighted(config.Config.MaxConcurrencyDeleting) var eg errgroup.Group ConcurrencyDeleting = 0 From 9979dd1cc56e8e140379f653d4a257b08c427ff7 Mon Sep 17 00:00:00 2001 From: whywaita Date: Mon, 1 Aug 2022 11:55:05 +0900 Subject: [PATCH 2/3] Update only self updated message --- pkg/runner/runner_delete.go | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/pkg/runner/runner_delete.go b/pkg/runner/runner_delete.go index fef2789..65de486 100644 --- a/pkg/runner/runner_delete.go +++ b/pkg/runner/runner_delete.go @@ -4,6 +4,7 @@ import ( "context" "errors" "fmt" + "strings" "time" "github.com/google/go-github/v35/github" @@ -65,14 +66,16 @@ func (m *Manager) removeRunners(ctx context.Context, t datastore.Target) error { return fmt.Errorf("failed to check number of registerd runner: %w", err) } else if isZero && len(runners) == 0 && mode == datastore.RunnerTemporaryOnce { logger.Logf(false, "runner for queueing is not found in %s", t.RepoURL()) - if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusErr, "runner for queueing is not found"); err != nil { + if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusErr, ErrDescriptionRunnerForQueueingIsNotFound); err != nil { logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err) } return nil } - if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusActive, ""); err != nil { - logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err) + if t.Status == datastore.TargetStatusErr && t.StatusDescription.Valid && strings.EqualFold(t.StatusDescription.String, ErrDescriptionRunnerForQueueingIsNotFound) { + if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusActive, ""); err != nil { + logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err) + } } sem := semaphore.NewWeighted(config.Config.MaxConcurrencyDeleting) @@ -153,6 +156,11 @@ var ( ErrNotWillDeleteRunner = fmt.Errorf("not will delete runner") ) +const ( + // ErrDescriptionRunnerForQueueingIsNotFound is error message for datastore.StatusDescription "runner for queueing is not found" + ErrDescriptionRunnerForQueueingIsNotFound = "runner for queueing is not found" +) + var ( // StatusWillDelete will delete target in GitHub runners StatusWillDelete = "offline" From 7f5eb17c9877ed17b2cd34afaa15d0b95cef06ea Mon Sep 17 00:00:00 2001 From: whywaita Date: Mon, 1 Aug 2022 19:36:16 +0900 Subject: [PATCH 3/3] Refactoring --- pkg/runner/runner_delete.go | 44 +++++++++++++++++++++++-------------- pkg/starter/starter.go | 2 +- 2 files changed, 28 insertions(+), 18 deletions(-) diff --git a/pkg/runner/runner_delete.go b/pkg/runner/runner_delete.go index 65de486..4445bfc 100644 --- a/pkg/runner/runner_delete.go +++ b/pkg/runner/runner_delete.go @@ -61,21 +61,27 @@ func (m *Manager) removeRunners(ctx context.Context, t datastore.Target) error { return fmt.Errorf("failed to get runner mode: %w", err) } - isZero, ghRunners, err := isRegisteredRunnerZeroInGitHub(ctx, t) + ghRunners, err := isRegisteredRunnerZeroInGitHub(ctx, t) if err != nil { return fmt.Errorf("failed to check number of registerd runner: %w", err) - } else if isZero && len(runners) == 0 && mode == datastore.RunnerTemporaryOnce { - logger.Logf(false, "runner for queueing is not found in %s", t.RepoURL()) - if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusErr, ErrDescriptionRunnerForQueueingIsNotFound); err != nil { - logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err) - } - return nil } - if t.Status == datastore.TargetStatusErr && t.StatusDescription.Valid && strings.EqualFold(t.StatusDescription.String, ErrDescriptionRunnerForQueueingIsNotFound) { - if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusActive, ""); err != nil { - logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err) + if len(ghRunners) == 0 && len(runners) == 0 { + switch mode { + case datastore.RunnerTemporaryOnce: + logger.Logf(false, "runner for queueing is not found in %s", t.RepoURL()) + if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusErr, ErrDescriptionRunnerForQueueingIsNotFound); err != nil { + logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err) + } + default: + if t.Status == datastore.TargetStatusErr && t.StatusDescription.Valid && strings.EqualFold(t.StatusDescription.String, ErrDescriptionRunnerForQueueingIsNotFound) { + if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusActive, ""); err != nil { + logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err) + } + } } + + return nil } sem := semaphore.NewWeighted(config.Config.MaxConcurrencyDeleting) @@ -107,6 +113,12 @@ func (m *Manager) removeRunners(ctx context.Context, t datastore.Target) error { return fmt.Errorf("failed to wait errgroup.Wait(): %w", err) } + if t.Status == datastore.TargetStatusRunning { + if err := datastore.UpdateTargetStatus(ctx, m.ds, t.UUID, datastore.TargetStatusActive, ""); err != nil { + logger.Logf(false, "failed to update target status (target ID: %s): %+v\n", t.UUID, err) + } + } + return nil } @@ -130,25 +142,23 @@ func (m *Manager) removeRunner(ctx context.Context, t datastore.Target, runner d return fmt.Errorf("failed to remove runner (mode ephemeral): %w", err) } } + return nil } -func isRegisteredRunnerZeroInGitHub(ctx context.Context, t datastore.Target) (bool, []*github.Runner, error) { +func isRegisteredRunnerZeroInGitHub(ctx context.Context, t datastore.Target) ([]*github.Runner, error) { owner, repo := t.OwnerRepo() client, err := gh.NewClient(t.GitHubToken, t.GHEDomain.String) if err != nil { - return false, nil, fmt.Errorf("failed to create github client: %w", err) + return nil, fmt.Errorf("failed to create github client: %w", err) } ghRunners, err := gh.ListRunners(ctx, client, owner, repo) if err != nil { - return false, nil, fmt.Errorf("failed to get list of runner in GitHub: %w", err) + return nil, fmt.Errorf("failed to get list of runner in GitHub: %w", err) } - if len(ghRunners) == 0 { - return true, nil, nil - } - return false, ghRunners, nil + return ghRunners, nil } var ( diff --git a/pkg/starter/starter.go b/pkg/starter/starter.go index 40981ac..2ef8ec1 100644 --- a/pkg/starter/starter.go +++ b/pkg/starter/starter.go @@ -147,7 +147,7 @@ func (s *Starter) processJob(ctx context.Context, job datastore.Job) error { // is not ok, save job return nil } - if err := datastore.UpdateTargetStatus(ctx, s.ds, job.TargetID, datastore.TargetStatusRunning, fmt.Sprintf("job id: %s", job.UUID)); err != nil { + if err := datastore.UpdateTargetStatus(ctx, s.ds, job.TargetID, datastore.TargetStatusRunning, ""); err != nil { return fmt.Errorf("failed to update target status (target ID: %s, job ID: %s): %w", job.TargetID, job.UUID, err) }