-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoat.go
141 lines (121 loc) · 3.29 KB
/
goat.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
129
130
131
132
133
134
135
136
137
138
139
140
141
package goat
import (
"context"
"fmt"
"github.com/PCloud63514/goat/environment"
"github.com/PCloud63514/goat/internal/utils"
"github.com/PCloud63514/goat/logger"
"os"
"os/signal"
"runtime"
"strings"
"sync"
"syscall"
"time"
)
type HandlerType string
const (
HandlerType_Initialize HandlerType = "INITIALIZE"
HandlerType_Starting HandlerType = "STARTING"
HandlerType_Started HandlerType = "STARTED"
HandlerType_Stop HandlerType = "STOP"
HandlerType_Destroy HandlerType = "DESTROY"
)
var (
app *Goat = new()
)
func new() *Goat {
ctx, cancel := context.WithCancel(context.Background())
return &Goat{
mu: sync.RWMutex{},
chains: make(map[HandlerType][]HandlerFunc),
environment: environment.New(),
ctx: ctx,
cancelFunc: cancel,
}
}
type HandlerFunc func(ctx context.Context, env environment.Environment)
type Goat struct {
mu sync.RWMutex
startRunDateTime time.Time
environment environment.Environment
chains map[HandlerType][]HandlerFunc
ctx context.Context
cancelFunc context.CancelFunc
}
func (app *Goat) Run() {
app.onInitialize()
app.onStarting()
app.onStarted()
app.onPulling()
app.onStop()
}
func (app *Goat) onSystemInitialize() {
app.startRunDateTime = time.Now()
logger.Infof("Application Initialize\t Profile: [%v]\t PID: %v\tGoVersion: %v\t StartDateTime: %v\t",
strings.Join(app.environment.GetProfiles(), ","),
os.Getpid(),
runtime.Version(),
app.startRunDateTime.Format("2006-01-02 15:04:05"),
)
utils.MakeFile(os.Getpid(), ".pid")
utils.MakeFile(strings.Join(app.environment.GetProfiles(), ","), ".profile")
}
func (app *Goat) onInitialize() {
app.onSystemInitialize()
app.executeHandlers(HandlerType_Initialize)
}
func (app *Goat) onStarting() {
app.executeHandlers(HandlerType_Starting)
app.applicationStartMsg()
}
func (app *Goat) onStarted() {
app.executeHandlers(HandlerType_Started)
}
func (app *Goat) onPulling() {
go func() {
sigCh := make(chan os.Signal, 1)
signal.Notify(sigCh, syscall.SIGINT, syscall.SIGTERM)
<-sigCh
app.cancelFunc()
}()
<-app.ctx.Done()
logger.Info("shutdown...")
}
func (app *Goat) onStop() {
app.executeHandlers(HandlerType_Stop)
}
func (app *Goat) onDestroy() {
app.executeHandlers(HandlerType_Destroy)
logger.Info("Shutdown complete")
}
func (app *Goat) applicationStartMsg() {
endTime := time.Now()
elapsedTime := endTime.Sub(app.startRunDateTime)
logger.Infof("Application Start\n\t\t\t┠ Profile: [%v]\n\t\t\t┠ PID: %v\n\t\t\t┠ GoVersion: %v\n\t\t\t┠ StartDateTime: %v\n\t\t\t┖ CompletedSeconds: %v",
strings.Join(app.environment.GetProfiles(), ","),
os.Getpid(),
runtime.Version(),
app.startRunDateTime.Format("2006-01-02 15:04:05"),
fmt.Sprintf("%dm %ds", int(elapsedTime.Minutes()), int(elapsedTime.Seconds())%60),
)
}
func (app *Goat) getHandlers(t HandlerType) []HandlerFunc {
app.mu.RLock()
defer app.mu.RUnlock()
if v, ok := app.chains[t]; ok {
return v
}
return make([]HandlerFunc, 0)
}
func (app *Goat) AddHandlerFunc(hFunc HandlerFunc, t HandlerType) {
if app.chains[t] == nil {
app.chains[t] = []HandlerFunc{}
}
app.chains[t] = append(app.chains[t], hFunc)
}
func (app *Goat) executeHandlers(t HandlerType) {
for _, execute := range app.getHandlers(t) {
execute(app.ctx, app.environment)
}
}