-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor_config.go
More file actions
149 lines (118 loc) · 3.23 KB
/
executor_config.go
File metadata and controls
149 lines (118 loc) · 3.23 KB
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
package executor
import (
"errors"
"log/slog"
"time"
"github.com/zalgonoise/cfg"
"go.opentelemetry.io/otel/trace"
"go.opentelemetry.io/otel/trace/noop"
"github.com/zalgonoise/micron/v3/log"
"github.com/zalgonoise/micron/v3/metrics"
"github.com/zalgonoise/micron/v3/schedule"
"github.com/zalgonoise/micron/v3/schedule/cronlex"
)
func defaultExecutable() *Executable {
return &Executable{
timeout: defaultTimeout,
logger: slog.New(log.NoOp()),
metrics: metrics.NoOp(),
tracer: noop.NewTracerProvider().Tracer("micron.executor"),
}
}
// WithScheduler configures the Executor with the input schedule.Scheduler.
//
// This call returns a cfg.NoOp cfg.Option if the input schedule.Scheduler is either nil or a no-op.
//
// Using this option does not require passing WithSchedule nor WithLocation options.
func WithScheduler(sched Scheduler) cfg.Option[*Executable] {
if sched == nil || sched == schedule.NoOp() {
return cfg.NoOp[*Executable]{}
}
return cfg.Register(func(e *Executable) *Executable {
e.cron = sched
return e
})
}
// WithSchedule configures the Executor with a schedule.Scheduler using the input cron string.
//
// This call returns a cfg.NoOp cfg.Option if the cron string is empty.
//
// This option can be followed by a WithLocation option.
func WithSchedule(cron string, loc *time.Location) cfg.Option[*Executable] {
if cron == "" {
return cfg.NoOp[*Executable]{}
}
s, err := cronlex.Parse(cron)
if errors.Is(err, cronlex.ErrRebootException) {
return cfg.Register(func(e *Executable) *Executable {
e.cron = schedule.NewRebootSchedule()
return e
})
}
if err != nil {
return cfg.NoOp[*Executable]{}
}
if loc == nil {
loc = time.Local
}
scheduler, err := schedule.New(
schedule.WithSchedule(s),
schedule.WithLocation(loc),
)
if err != nil {
return cfg.NoOp[*Executable]{}
}
return cfg.Register(func(e *Executable) *Executable {
e.cron = scheduler
return e
})
}
func WithTimeout(dur time.Duration) cfg.Option[*Executable] {
if dur <= 0 {
dur = defaultTimeout
}
return cfg.Register(func(e *Executable) *Executable {
e.timeout = dur
return e
})
}
// WithMetrics decorates the Executor with the input metrics registry.
func WithMetrics(m Metrics) cfg.Option[*Executable] {
if m == nil {
return cfg.NoOp[*Executable]{}
}
return cfg.Register(func(e *Executable) *Executable {
e.metrics = m
return e
})
}
// WithLogger decorates the Executor with the input logger.
func WithLogger(logger *slog.Logger) cfg.Option[*Executable] {
if logger == nil {
return cfg.NoOp[*Executable]{}
}
return cfg.Register(func(e *Executable) *Executable {
e.logger = logger
return e
})
}
// WithLogHandler decorates the Executor with logging using the input log handler.
func WithLogHandler(handler slog.Handler) cfg.Option[*Executable] {
if handler == nil {
return cfg.NoOp[*Executable]{}
}
return cfg.Register(func(e *Executable) *Executable {
e.logger = slog.New(handler)
return e
})
}
// WithTrace decorates the Executor with the input trace.Tracer.
func WithTrace(tracer trace.Tracer) cfg.Option[*Executable] {
if tracer == nil {
return cfg.NoOp[*Executable]{}
}
return cfg.Register(func(config *Executable) *Executable {
config.tracer = tracer
return config
})
}