From d3859d5882aa65cc264c11ce2710278f8ead595b Mon Sep 17 00:00:00 2001 From: whywaita Date: Wed, 26 May 2021 20:37:57 +0900 Subject: [PATCH] ListTarget not return can not received job --- pkg/datastore/interface.go | 18 ++++++++++++++++++ pkg/runner/runner.go | 4 ++-- pkg/web/target.go | 2 +- 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/pkg/datastore/interface.go b/pkg/datastore/interface.go index 5fce454..86446f9 100644 --- a/pkg/datastore/interface.go +++ b/pkg/datastore/interface.go @@ -81,6 +81,24 @@ func (t *Target) CanReceiveJob() bool { return true } +// ListTargets get list of target that can receive job +func ListTargets(ctx context.Context, ds Datastore) ([]Target, error) { + targets, err := ds.ListTargets(ctx) + if err != nil { + return nil, fmt.Errorf("failed to get targets from datastore: %w", err) + } + + var result []Target + + for _, t := range targets { + if t.CanReceiveJob() { + result = append(result, t) + } + } + + return result, nil +} + // UpdateTargetStatus update datastore func UpdateTargetStatus(ctx context.Context, ds Datastore, targetID uuid.UUID, newStatus TargetStatus, description string) error { target, err := ds.GetTarget(ctx, targetID) diff --git a/pkg/runner/runner.go b/pkg/runner/runner.go index 97183a3..f2f7f70 100644 --- a/pkg/runner/runner.go +++ b/pkg/runner/runner.go @@ -66,7 +66,7 @@ func (m *Manager) Loop(ctx context.Context) error { func (m *Manager) do(ctx context.Context) error { logger.Logf(true, "start runner manager") - targets, err := m.ds.ListTargets(ctx) + targets, err := datastore.ListTargets(ctx, m.ds) if err != nil { return fmt.Errorf("failed to get targets: %w", err) } @@ -86,7 +86,7 @@ func (m *Manager) do(ctx context.Context) error { func (m *Manager) permissionCheck(ctx context.Context) error { logger.Logf(true, "start checking initialize state") - targets, err := m.ds.ListTargets(ctx) + targets, err := datastore.ListTargets(ctx, m.ds) if err != nil { return fmt.Errorf("failed to get targets: %w", err) } diff --git a/pkg/web/target.go b/pkg/web/target.go index d65c9d0..5867c72 100644 --- a/pkg/web/target.go +++ b/pkg/web/target.go @@ -91,7 +91,7 @@ func (t *targetCreateParam) toDS() datastore.Target { func handleTargetList(w http.ResponseWriter, r *http.Request, ds datastore.Datastore) { ctx := r.Context() - ts, err := ds.ListTargets(ctx) + ts, err := datastore.ListTargets(ctx, ds) if err != nil { logger.Logf(false, "failed to retrieve list of target: %+v", err) outputErrorMsg(w, http.StatusInternalServerError, "datastore read error")