-
Notifications
You must be signed in to change notification settings - Fork 27
/
options.go
158 lines (134 loc) · 3.52 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
package bokchoy
import (
"strings"
"time"
"github.com/thoas/bokchoy/logging"
)
// Options is the bokchoy options.
type Options struct {
Tracer Tracer
Logger logging.Logger
Concurrency int
MaxRetries int
TTL time.Duration
Countdown *time.Duration
Timeout time.Duration
RetryIntervals []time.Duration
Serializer Serializer
Initialize bool
Queues []string
DisableOutput bool
Servers []Server
Broker Broker
}
// RetryIntervalsDisplay returns a string representation of the retry intervals.
func (o Options) RetryIntervalsDisplay() string {
intervals := make([]string, len(o.RetryIntervals))
for i := range o.RetryIntervals {
intervals[i] = o.RetryIntervals[i].String()
}
return strings.Join(intervals, ", ")
}
// newOptions returns default options.
func newOptions() *Options {
opts := &Options{}
options := []Option{
WithConcurrency(defaultConcurrency),
WithMaxRetries(defaultMaxRetries),
WithTTL(defaultTTL),
WithTimeout(defaultTimeout),
WithRetryIntervals(defaultRetryIntervals),
WithInitialize(true),
}
for i := range options {
options[i](opts)
}
return opts
}
// Option is an option unit.
type Option func(opts *Options)
// WithDisableOutput defines if the output (logo, queues information)
// should be disabled.
func WithDisableOutput(disableOutput bool) Option {
return func(opts *Options) {
opts.DisableOutput = disableOutput
}
}
// WithBroker registers new broker.
func WithBroker(broker Broker) Option {
return func(opts *Options) {
opts.Broker = broker
}
}
// WithServers registers new servers to be run.
func WithServers(servers []Server) Option {
return func(opts *Options) {
opts.Servers = servers
}
}
// WithQueues allows to override queues to run.
func WithQueues(queues []string) Option {
return func(opts *Options) {
opts.Queues = queues
}
}
// WithSerializer defines the Serializer.
func WithSerializer(serializer Serializer) Option {
return func(opts *Options) {
opts.Serializer = serializer
}
}
// WithInitialize defines if the broker needs to be initialized.
func WithInitialize(initialize bool) Option {
return func(opts *Options) {
opts.Initialize = initialize
}
}
// WithTracer defines the Tracer.
func WithTracer(tracer Tracer) Option {
return func(opts *Options) {
opts.Tracer = tracer
}
}
// WithLogger defines the Logger.
func WithLogger(logger logging.Logger) Option {
return func(opts *Options) {
opts.Logger = logger
}
}
// WithTimeout defines the timeout used to execute a task.
func WithTimeout(timeout time.Duration) Option {
return func(opts *Options) {
opts.Timeout = timeout
}
}
// WithCountdown defines the countdown to launch a delayed task.
func WithCountdown(countdown time.Duration) Option {
return func(opts *Options) {
opts.Countdown = &countdown
}
}
// WithConcurrency defines the number of concurrent consumers.
func WithConcurrency(concurrency int) Option {
return func(opts *Options) {
opts.Concurrency = concurrency
}
}
// WithMaxRetries defines the number of maximum retries for a failed task.
func WithMaxRetries(maxRetries int) Option {
return func(opts *Options) {
opts.MaxRetries = maxRetries
}
}
// WithRetryIntervals defines the retry intervals for a failed task.
func WithRetryIntervals(retryIntervals []time.Duration) Option {
return func(opts *Options) {
opts.RetryIntervals = retryIntervals
}
}
// WithTTL defines the duration to keep the task in the broker.
func WithTTL(ttl time.Duration) Option {
return func(opts *Options) {
opts.TTL = ttl
}
}