-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcron_config.go
160 lines (128 loc) · 3.69 KB
/
cron_config.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
159
160
package micron
import (
"log/slog"
"github.com/zalgonoise/cfg"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
"github.com/zalgonoise/micron/executor"
"github.com/zalgonoise/micron/log"
"github.com/zalgonoise/micron/metrics"
"github.com/zalgonoise/micron/selector"
)
const (
minBufferSize = 64
minAlloc = 64
defaultBufferSize = 1024
)
type Config struct {
errBufferSize int
sel selector.Selector
execs []executor.Executor
handler slog.Handler
metrics Metrics
tracer trace.Tracer
}
func defaultConfig() *Config {
return &Config{
errBufferSize: minBufferSize,
handler: log.NoOp(),
metrics: metrics.NoOp(),
tracer: noop.NewTracerProvider().Tracer("runtime's no-op tracer"),
}
}
// WithSelector configures the Runtime with the input selector.Selector.
//
// This call returns a cfg.NoOp cfg.Option if the input selector.Selector is nil, or if it is a
// selector.NoOp type.
func WithSelector(sel selector.Selector) cfg.Option[*Config] {
if sel == nil || sel == selector.NoOp() {
return cfg.NoOp[*Config]{}
}
return cfg.Register(func(config *Config) *Config {
config.sel = sel
return config
})
}
// WithJob adds a new executor.Executor to the Runtime configuration from the input ID, cron string and
// set of executor.Runner.
//
// This call returns a cfg.NoOp cfg.Option if no executor.Runner is provided, or if creating the executor.Executor
// fails (e.g. due to an invalid cron string).
//
// The gathered executor.Executor are then injected into a new selector.Selector that the Runtime will use.
//
// Note: this call is only valid if when creating a new Runtime via the New function, no WithSelector option is
// supplied; only WithJob. A call to New supports multiple WithJob cfg.Option.
func WithJob(id, cron string, runners ...executor.Runner) cfg.Option[*Config] {
if len(runners) == 0 {
return cfg.NoOp[*Config]{}
}
if id == "" {
id = cron
}
exec, err := executor.New(id,
executor.WithSchedule(cron),
executor.WithRunners(runners...),
)
if err != nil {
return cfg.NoOp[*Config]{}
}
return cfg.Register(func(config *Config) *Config {
if config.execs == nil {
config.execs = make([]executor.Executor, 0, minAlloc)
}
config.execs = append(config.execs, exec)
return config
})
}
// WithErrorBufferSize defines the capacity of the error channel that the Runtime exposes in
// its Runtime.Err method.
func WithErrorBufferSize(size int) cfg.Option[*Config] {
if size < 0 {
size = defaultBufferSize
}
return cfg.Register(func(config *Config) *Config {
config.errBufferSize = size
return config
})
}
// WithMetrics decorates the Runtime with the input metrics registry.
func WithMetrics(m Metrics) cfg.Option[*Config] {
if m == nil {
return cfg.NoOp[*Config]{}
}
return cfg.Register(func(config *Config) *Config {
config.metrics = m
return config
})
}
// WithLogger decorates the Runtime with the input logger.
func WithLogger(logger *slog.Logger) cfg.Option[*Config] {
if logger == nil {
return cfg.NoOp[*Config]{}
}
return cfg.Register(func(config *Config) *Config {
config.handler = logger.Handler()
return config
})
}
// WithLogHandler decorates the Runtime with logging using the input log handler.
func WithLogHandler(handler slog.Handler) cfg.Option[*Config] {
if handler == nil {
return cfg.NoOp[*Config]{}
}
return cfg.Register(func(config *Config) *Config {
config.handler = handler
return config
})
}
// WithTrace decorates the Runtime with the input trace.Tracer.
func WithTrace(tracer trace.Tracer) cfg.Option[*Config] {
if tracer == nil {
return cfg.NoOp[*Config]{}
}
return cfg.Register(func(config *Config) *Config {
config.tracer = tracer
return config
})
}