-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
exitUtils.go
50 lines (45 loc) · 792 Bytes
/
exitUtils.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
package go_utils
import (
"github.com/projectdiscovery/gologger"
"os"
"os/signal"
"sync"
"syscall"
)
type Exit struct {
funcs []func() error
Lock *sync.Mutex
}
func (r *Exit) RegClose(fns ...func() error) {
r.Lock.Lock()
defer r.Lock.Unlock()
r.funcs = append(r.funcs, fns...)
}
var (
gExit *Exit
TmpLc sync.RWMutex
)
func NewExit() *Exit {
TmpLc.Lock()
defer TmpLc.Unlock()
if nil != gExit {
return gExit
}
gExit = &Exit{Lock: &sync.Mutex{}}
// close handler
go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
gologger.DefaultLogger.Info().Msg("- Ctrl+C pressed in Terminal")
for _, fn1 := range gExit.funcs {
if nil != fn1 {
fn1()
}
}
//os.Exit(0)
}()
}()
return gExit
}