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.
pull/3251/head
Jim 3 years ago committed by Timothy Messier
parent b43830e66e
commit 6f78889947
No known key found for this signature in database
GPG Key ID: EFD2F184F7600572

@ -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
}
}
}

@ -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 "),

Loading…
Cancel
Save