-
-
Notifications
You must be signed in to change notification settings - Fork 109
/
onexit.go
57 lines (49 loc) · 987 Bytes
/
onexit.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
// Copyright © 2023 Ory Corp
// SPDX-License-Identifier: Apache-2.0
package dockertest
import (
"os"
"os/signal"
"sync"
"syscall"
)
const interruptedExitCode = 130
// OnExit helps with cleaning up docker test.
type OnExit struct {
sync.Mutex
once sync.Once
handlers []func()
}
// NewOnExit create a new OnExit instance.
func NewOnExit() *OnExit {
return &OnExit{
handlers: make([]func(), 0),
}
}
// Add adds a task that is executed on SIGINT, SIGKILL, SIGTERM.
func (at *OnExit) Add(f func()) {
at.Lock()
defer at.Unlock()
at.handlers = append(at.handlers, f)
at.once.Do(func() {
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
<-c
at.Exit(interruptedExitCode)
}()
})
}
// Exit wraps os.Exit
func (at *OnExit) Exit(status int) {
at.execute()
os.Exit(status)
}
func (at *OnExit) execute() {
at.Lock()
defer at.Unlock()
for _, f := range at.handlers {
f()
}
at.handlers = make([]func(), 0)
}