/
options.go
94 lines (83 loc) · 2.98 KB
/
options.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/*******************************************************************************
*
* Copyright 2023 SAP SE
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You should have received a copy of the License along with this
* program. If not, you may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*******************************************************************************/
package jobloop
import (
"fmt"
"sort"
"strings"
"github.com/prometheus/client_golang/prometheus"
)
// Option is a configuration option for a Job. Currently, only the number of
// goroutines can be configured, but more options could be added in the future.
//
// This type is an implementation of the Functional Options pattern, see e.g.
// <https://github.com/tmrts/go-patterns/blob/master/idiom/functional-options.md>
type Option func(*jobConfig)
type jobConfig struct {
NumGoroutines uint
PrefilledLabels prometheus.Labels
}
func newJobConfig(opts []Option) jobConfig {
// default values
cfg := jobConfig{
NumGoroutines: 1,
}
// apply specific overrides
for _, opt := range opts {
opt(&cfg)
}
return cfg
}
// PrefilledLabelsAsString returns a representation of cfg.PrefilledLabels
// that is suitable for log messages.
func (cfg jobConfig) PrefilledLabelsAsString() string {
if len(cfg.PrefilledLabels) == 0 {
return ""
}
fields := make([]string, 0, len(cfg.PrefilledLabels))
for label, value := range cfg.PrefilledLabels {
fields = append(fields, fmt.Sprintf("%s=%q", label, value))
}
sort.Strings(fields)
return fmt.Sprintf(" (%s)", strings.Join(fields, ", "))
}
// NumGoroutines is an option for a Job that allows the Job to use multiple
// goroutines, up to the specified number. The default value is 1, meaning that
// no concurrency will be employed.
//
// This option is always ignored during ProcessOne(), because a single task
// does not require concurrency on the level of the job runtime.
func NumGoroutines(n uint) Option {
return func(cfg *jobConfig) {
cfg.NumGoroutines = n
}
}
// WithLabel is an option for a Job that prefills one of the CounterLabels
// declared in the job's metadata before each task. This is useful for running
// multiple instances of a job in parallel while reusing the JobMetadata, task
// callbacks, and Prometheus metrics. Task callbacks can inspect the overridden
// label value to discover which particular instance of the job they belong to.
func WithLabel(label, value string) Option {
return func(cfg *jobConfig) {
if cfg.PrefilledLabels == nil {
cfg.PrefilledLabels = make(prometheus.Labels)
}
cfg.PrefilledLabels[label] = value
}
}