From 6f78889947a264bbb9477b3d267e856dd5c559fe Mon Sep 17 00:00:00 2001 From: Jim Date: Sun, 23 Apr 2023 10:14:43 -0400 Subject: [PATCH] refact: add servers.ListWorkers(...) refactor servers.(repository).ListWorkers(...) into just servers.ListWorkers(...) used independently of the repo. The method func is refactor to use the new bits as well. --- internal/server/options.go | 10 ++++++- internal/server/repository_worker.go | 44 ++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/internal/server/options.go b/internal/server/options.go index b497ccb36d..70b338d17a 100644 --- a/internal/server/options.go +++ b/internal/server/options.go @@ -119,7 +119,15 @@ func WithUpdateTags(updateTags bool) Option { // WithWorkerTags provides worker tags. func WithWorkerTags(tags ...*Tag) Option { return func(o *options) { - o.withWorkerTags = tags + newTags := []*Tag{} + for _, tag := range tags { + if tag != nil { + newTags = append(newTags, tag) + } + } + if len(newTags) > 0 { + o.withWorkerTags = tags + } } } diff --git a/internal/server/repository_worker.go b/internal/server/repository_worker.go index 250cf71ad0..0a6262c6cd 100644 --- a/internal/server/repository_worker.go +++ b/internal/server/repository_worker.go @@ -166,15 +166,53 @@ func lookupWorker(ctx context.Context, reader db.Reader, id string) (*Worker, er } // ListWorkers will return a listing of Workers and honor the WithLimit option. +// Supported options: WithWorkerType, WithActiveWorkers, WithLiveness, +// WithWorkerPool, WithLimit +// // If WithLiveness is zero the default liveness value is used, if it is negative // then the last status update time is ignored. // If WithLimit < 0, then unlimited results are returned. If WithLimit == 0, then // default limits are used for results. WithWorkerPool can be provided with a // non-zero length slice of worker ids to restrict the returned workers to only // ones with the ids provided. -// Also supports: WithWorkerType, WithActiveWorkers func (r *Repository) ListWorkers(ctx context.Context, scopeIds []string, opt ...Option) ([]*Worker, error) { const op = "server.(Repository).ListWorkers" + + opts := GetOpts(opt...) + newOpts := []Option{} + // handle the WithLimit default for the repo + switch { + case opts.withLimit != 0: + newOpts = append(newOpts, WithLimit(opts.withLimit)) + default: + newOpts = append(newOpts, WithLimit(r.defaultLimit)) + } + // handle the WithLiveness default + switch { + case opts.withLiveness != 0: + newOpts = append(newOpts, WithLiveness(opts.withLiveness)) + default: + newOpts = append(newOpts, WithLiveness(DefaultLiveness)) + + } + newOpts = append(newOpts, WithWorkerType(opts.withWorkerType)) + newOpts = append(newOpts, WithActiveWorkers(opts.withActiveWorkers)) + newOpts = append(newOpts, WithWorkerPool(opts.withWorkerPool)) + return ListWorkers(ctx, r.reader, scopeIds, newOpts...) +} + +// ListWorkers will return a listing of Workers and honor the WithLimit option. +// Supported options: WithWorkerType, WithActiveWorkers, WithLiveness, +// WithWorkerPool, WithLimit +// +// If WithLiveness is zero the default liveness value is used, if it is negative +// then the last status update time is ignored. +// If WithLimit < 0, then unlimited results are returned. If WithLimit == 0, then +// default limits are used for results. WithWorkerPool can be provided with a +// non-zero length slice of worker ids to restrict the returned workers to only +// ones with the ids provided. +func ListWorkers(ctx context.Context, reader db.Reader, scopeIds []string, opt ...Option) ([]*Worker, error) { + const op = "server.ListWorkers" switch { case len(scopeIds) == 0: return nil, errors.New(ctx, errors.InvalidParameter, op, "no scope ids set") @@ -215,14 +253,14 @@ func (r *Repository) ListWorkers(ctx context.Context, scopeIds []string, opt ... whereArgs = append(whereArgs, opts.withWorkerPool) } - limit := r.defaultLimit + limit := db.DefaultLimit if opts.withLimit != 0 { // non-zero signals an override of the default limit for the repo. limit = opts.withLimit } var wAggs []*workerAggregate - if err := r.reader.SearchWhere( + if err := reader.SearchWhere( ctx, &wAggs, strings.Join(where, " and "),