Skip to content

Commit

Permalink
Merge pull request #150 from whywaita/fix/147
Browse files Browse the repository at this point in the history
Set status is active if target hasn't runner for queue in ephemeral mode
  • Loading branch information
whywaita authored Aug 15, 2022
2 parents 45ec539 + 7f5eb17 commit feaa350
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
51 changes: 39 additions & 12 deletions pkg/runner/runner_delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/google/go-github/v35/github"
Expand Down Expand Up @@ -55,14 +56,31 @@ func (m *Manager) removeRunners(ctx context.Context, t datastore.Target) error {
return fmt.Errorf("failed to retrieve list of running runner: %w", err)
}

isZero, ghRunners, err := isRegisteredRunnerZeroInGitHub(ctx, t)
_, mode, err := datastore.GetRunnerTemporaryMode(t.RunnerVersion)
if err != nil {
return fmt.Errorf("failed to get runner mode: %w", err)
}

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 {
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)
}

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
}

Expand Down Expand Up @@ -95,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 @@ -118,32 +142,35 @@ 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 (
// ErrNotWillDeleteRunner is error message for "not will delete runner"
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"
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 feaa350

Please sign in to comment.