-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.go
More file actions
45 lines (38 loc) · 1.07 KB
/
options.go
File metadata and controls
45 lines (38 loc) · 1.07 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
package exitplan
import (
"fmt"
"os"
"os/signal"
"time"
)
type opt func(*Exitplan)
// WithExitError sets the callback that will be called when an error occurs during the teardown phase.
// It will be called for every error that occurs during the teardown phase.
func WithExitError(callback func(error)) opt {
return func(l *Exitplan) {
l.errorHandler = callback
}
}
// WithTeardownTimeout sets the timeout for the teardown phase.
func WithTeardownTimeout(timeout time.Duration) opt {
return func(l *Exitplan) {
l.teardownTimeout = timeout
}
}
// WithStartupTimeout sets the timeout for the starting phase.
func WithStartupTimeout(timeout time.Duration) opt {
return func(l *Exitplan) {
l.startingTimeout = timeout
}
}
// WithSignal calls Exitplan.Exit() when the specified signal is received.
func WithSignal(s1 os.Signal, sMany ...os.Signal) opt {
return func(l *Exitplan) {
notify := make(chan os.Signal, 1)
signal.Notify(notify, append([]os.Signal{s1}, sMany...)...)
go func() {
sig := <-notify
_ = l.Exit(fmt.Errorf("%w: %q", ErrSignaled, sig))
}()
}
}