forked from scakemyer/quasar
-
Notifications
You must be signed in to change notification settings - Fork 43
/
exit.go
76 lines (57 loc) · 1.01 KB
/
exit.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
package exit
import (
"context"
"net/http"
"os"
"runtime/debug"
"github.com/elgatito/elementum/util/event"
"github.com/op/go-logging"
)
const (
// ExitCodeSuccess = exit code 0
ExitCodeSuccess = 0
// ExitCodeError = exit code 1
ExitCodeError = 1
// ExitCodeRestart = exit code 5
ExitCodeRestart = 5
)
var (
lastExitCode = -1
IsShared = false
Args string
Code int
Closer event.Event
Server *http.Server
log = logging.MustGetLogger("exit")
)
func Reset() {
lastExitCode = -1
Args = ""
Code = 0
Closer.Clear()
}
func Exit(code int) {
if code == ExitCodeSuccess && lastExitCode != -1 {
code = lastExitCode
}
Code = code
if Server != nil {
Server.Shutdown(context.Background())
}
if IsShared {
return
}
os.Exit(Code)
}
func PanicCovered(err error) {
panic(err)
}
func Panic(err error) {
PanicWithCode(err, ExitCodeError)
}
func PanicWithCode(err error, code int) {
lastExitCode = code
log.Errorf("Panic: %s", err)
log.Errorf("Stacktrace: \n" + string(debug.Stack()))
Closer.Set()
}