Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
whywaita committed Aug 1, 2022
1 parent 9979dd1 commit 7f5eb17
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 18 deletions.
44 changes: 27 additions & 17 deletions pkg/runner/runner_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}

Expand All @@ -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 (
Expand Down
2 changes: 1 addition & 1 deletion pkg/starter/starter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down

0 comments on commit 7f5eb17

Please sign in to comment.