-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmeta_options.go
128 lines (106 loc) · 4.58 KB
/
meta_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
package process
import (
"context"
"time"
"github.com/derision-test/glock"
)
type metaOptions struct {
health *Health
healthKeys []interface{}
contextFilter func(ctx context.Context) context.Context
name string
metadata map[string]interface{}
priority int
allowEarlyExit bool
initTimeout time.Duration
startupTimeout time.Duration
stopTimeout time.Duration
shutdownTimeout time.Duration
finalizeTimeout time.Duration
logger Logger
initClock glock.Clock
startupClock glock.Clock
stopClock glock.Clock
shutdownClock glock.Clock
finalizeClock glock.Clock
}
type MetaConfigFunc func(meta *metaOptions)
// WithMetaHealth configures a Meta instance to use the given health instance.
func WithMetaHealth(health *Health) MetaConfigFunc {
return func(meta *metaOptions) { meta.health = health }
}
// WithMetaHealthKey configures a Meta instance to use the given keys to search for
// registered health component belonging to this process.
func WithMetaHealthKey(healthKeys ...interface{}) MetaConfigFunc {
return func(meta *metaOptions) { meta.healthKeys = append(meta.healthKeys, healthKeys...) }
}
// WithMetaContext configures a Meta instance to use the context returned by the given
// function when invoking the wrapped value's underlying Init, Run, Stop, or Finalize
// methods.
func WithMetaContext(f func(ctx context.Context) context.Context) MetaConfigFunc {
return func(meta *metaOptions) { meta.contextFilter = f }
}
// WithMetaName tags a Meta instance with the given name.
func WithMetaName(name string) MetaConfigFunc {
return func(meta *metaOptions) { meta.name = name }
}
// WithMetaPriority tags a Meta instance with the given priority.
func WithMetaPriority(priority int) MetaConfigFunc {
return func(meta *metaOptions) { meta.priority = priority }
}
// WithMetadata tags a Meta instance with the given metadata.
func WithMetadata(metadata map[string]interface{}) MetaConfigFunc {
return func(meta *metaOptions) { meta.metadata = metadata }
}
// WithEarlyExit sets the flag that determines if the process is allowed to return from
// the Run method (with a nil error value) before the Stop method is called. The default
// behavior is to treat exiting processes as erroneous.
func WithEarlyExit(allowed bool) MetaConfigFunc {
return func(meta *metaOptions) { meta.allowEarlyExit = allowed }
}
// WithMetaInitTimeout configures a Meta instance with the given timeout for the
// invocation of the wrapped value's Init method.
func WithMetaInitTimeout(timeout time.Duration) MetaConfigFunc {
return func(meta *metaOptions) { meta.initTimeout = timeout }
}
// WithMetaStartupTimeout configures a Meta instance with the given timeout for the
// time between the wrapped value's Run method being invoked and the process becoming
// healthy.
func WithMetaStartupTimeout(timeout time.Duration) MetaConfigFunc {
return func(meta *metaOptions) { meta.startupTimeout = timeout }
}
// WithMetaStopTimeout configures a Meta instance with the given timeout for the
// invocation of the wrapped value's Stop method.
func WithMetaStopTimeout(timeout time.Duration) MetaConfigFunc {
return func(meta *metaOptions) { meta.stopTimeout = timeout }
}
// WithMetaShutdownTimeout configures a Meta instance with the given timeout for the
// time between the wrapped value's Stop method being invoked and the process's Run
// method unblocking.
func WithMetaShutdownTimeout(timeout time.Duration) MetaConfigFunc {
return func(meta *metaOptions) { meta.shutdownTimeout = timeout }
}
// WithMetaFinalizeTimeout configures a Meta instance with the given timeout for the
// invocation of the wrapped value's Finalize method.
func WithMetaFinalizeTimeout(timeout time.Duration) MetaConfigFunc {
return func(meta *metaOptions) { meta.finalizeTimeout = timeout }
}
// WithMetaLogger configures a Meta instance with the given logger instance.
func WithMetaLogger(logger Logger) MetaConfigFunc {
return func(meta *metaOptions) { meta.logger = logger }
}
func withMetaInitClock(clock glock.Clock) MetaConfigFunc {
return func(meta *metaOptions) { meta.initClock = clock }
}
func withMetaStartupClock(clock glock.Clock) MetaConfigFunc {
return func(meta *metaOptions) { meta.startupClock = clock }
}
func withMetaStopClock(clock glock.Clock) MetaConfigFunc {
return func(meta *metaOptions) { meta.stopClock = clock }
}
func withMetaShutdownClock(clock glock.Clock) MetaConfigFunc {
return func(meta *metaOptions) { meta.shutdownClock = clock }
}
func withMetaFinalizeClock(clock glock.Clock) MetaConfigFunc {
return func(meta *metaOptions) { meta.finalizeClock = clock }
}