-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler_config.go
112 lines (88 loc) · 2.29 KB
/
scheduler_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
package schedule
import (
"log/slog"
"time"
"github.com/zalgonoise/cfg"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
"github.com/zalgonoise/micron/log"
"github.com/zalgonoise/micron/metrics"
)
type Config struct {
cron string
loc *time.Location
handler slog.Handler
metrics Metrics
tracer trace.Tracer
}
func defaultConfig() Config {
return Config{
handler: log.NoOp(),
metrics: metrics.NoOp(),
tracer: noop.NewTracerProvider().Tracer("scheduler's no-op tracer"),
}
}
// WithSchedule configures the Scheduler with the input cron string.
//
// This call returns a cfg.NoOp cfg.Option if the input cron string is empty.
func WithSchedule(cron string) cfg.Option[Config] {
if cron == "" {
return cfg.NoOp[Config]{}
}
return cfg.Register(func(config Config) Config {
config.cron = cron
return config
})
}
// WithLocation configures the Scheduler with the input time.Location.
//
// This call returns a cfg.NoOp cfg.Option if the input time.Location is nil.
func WithLocation(loc *time.Location) cfg.Option[Config] {
if loc == nil {
return cfg.NoOp[Config]{}
}
return cfg.Register(func(config Config) Config {
config.loc = loc
return config
})
}
// WithMetrics decorates the Scheduler 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 Scheduler 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 Scheduler 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 Scheduler 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
})
}