-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathinitialize.go
More file actions
167 lines (148 loc) · 4.74 KB
/
Copy pathinitialize.go
File metadata and controls
167 lines (148 loc) · 4.74 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package fs
import (
"context"
"fmt"
"os"
"os/signal"
"reflect"
"strconv"
"strings"
"sync"
"syscall"
"github.com/farseer-go/fs/color"
"github.com/farseer-go/fs/configure"
"github.com/farseer-go/fs/container"
"github.com/farseer-go/fs/core"
"github.com/farseer-go/fs/dateTime"
"github.com/farseer-go/fs/flog"
"github.com/farseer-go/fs/modules"
"github.com/farseer-go/fs/net"
"github.com/farseer-go/fs/sonyflake"
"github.com/farseer-go/fs/stopwatch"
)
var (
Context context.Context // 最顶层的上下文
contextCancel context.CancelFunc // 最顶层的上下文
dependModules []modules.FarseerModule // 依赖的模块
isInit bool // 是否初始化完
)
var onceInit sync.Once
// Initialize 初始化框架
func Initialize[TModule modules.FarseerModule](appName string) {
sw := stopwatch.StartNew()
Context, contextCancel = context.WithCancel(context.Background())
onceInit.Do(func() {
core.AppName = appName
core.ProcessId = os.Getppid()
core.HostName, _ = os.Hostname()
core.StartupAt = dateTime.Now()
core.AppId = sonyflake.GenerateId()
core.AppIp = net.GetIp()
})
flog.LogBuffer <- fmt.Sprint("FarsVer: ", color.Colors[2](core.Version))
flog.LogBuffer <- fmt.Sprint("AppName: ", color.Colors[2](core.AppName))
flog.LogBuffer <- fmt.Sprint("AppID: ", color.Colors[2](core.AppId))
flog.LogBuffer <- fmt.Sprint("AppIP: ", color.Colors[2](core.AppIp))
flog.LogBuffer <- fmt.Sprint("HostName:", color.Colors[2](core.HostName))
flog.LogBuffer <- fmt.Sprint("HostTime:", color.Colors[2](core.StartupAt.ToString("yyyy-MM-dd hh:mm:ss")))
flog.LogBuffer <- fmt.Sprint("PID: ", color.Colors[2](core.ProcessId))
showComponentLog()
flog.LogBuffer <- "---------------------------------------"
// 加载模块依赖
var startupModule TModule
dependModules = modules.GetDependModules(startupModule)
var moduleNames []string
for _, farseerModule := range dependModules {
moduleName := reflect.TypeOf(farseerModule).String()
moduleNames = append(moduleNames, color.Colors[5](moduleName))
}
flog.LogBuffer <- fmt.Sprint("Loading Module: " + strings.Join(moduleNames, "->") + " (" + color.Red(len(dependModules)) + " modules) ")
//flog.LogBuffer <- fmt.Sprint("Loaded, " + color.Red(len(dependModules)) + " modules in total")
// 执行所有模块初始化
modules.StartModules(dependModules)
flog.CloseBuffer()
flog.Println("Initialization completed, total time: " + sw.GetMillisecondsText())
flog.Println("---------------------------------------")
if proxy := configure.GetString("Proxy"); proxy != "" {
flog.Println("http使用代理: ", color.Blue(proxy))
}
// 健康检查
healthChecks := container.ResolveAll[core.IHealthCheck]()
if len(healthChecks) > 0 {
flog.Println("Health Check...")
isSuccess := true
for _, healthCheck := range healthChecks {
item, err := healthCheck.Check()
if err == nil {
flog.Printf("%s%s\n", color.Green("【✓】"), item)
} else {
flog.Printf("%s%s:%s\n", color.Red("【✕】"), item, color.Red(err.Error()))
isSuccess = false
}
}
if !isSuccess {
//os.Exit(-1)
panic("健康检查失败")
}
}
// 日志内容美化
if len(healthChecks) > 0 {
flog.Println("---------------------------------------")
}
isInit = true
// 加载callbackFnList,启动后才执行的模块
if len(core.CallbackInitList) > 0 {
for index, fn := range core.CallbackInitList {
sw.Restart()
fn.F()
flog.Println("Run " + strconv.Itoa(index+1) + ": " + fn.Name + ", Use: " + sw.GetText())
}
flog.Println("---------------------------------------")
}
}
// 组件日志
func showComponentLog() {
logConfig := configure.GetSubNodes("Log.Component")
var logSets []string
for k, v := range logConfig {
if v == true {
logSets = append(logSets, k)
}
}
if len(logSets) > 0 {
flog.LogBuffer <- fmt.Sprint("Log Switch: ", color.Colors[2](strings.Join(logSets, " ")))
}
}
// Exit 应用退出
func Exit() {
contextCancel()
// 关闭模块
modules.ShutdownModules(dependModules)
}
// AddInitCallback 添加框架启动完后执行的函数
func AddInitCallback(name string, fn func()) {
// 未初始化完时,加入到列表中
if !isInit {
core.AddInitCallback(name, fn)
} else { // 初始化完后,则立即执行
sw := stopwatch.StartNew()
fn()
flog.Println("Run : " + name + ", Use: " + sw.GetMillisecondsText())
}
}
// Run 运行应用,等待退出信号
func Run() {
flog.Println("应用已成功启动!")
// 注册退出信号
exitSignal := make(chan os.Signal, 1)
signal.Notify(exitSignal, syscall.SIGINT, syscall.SIGTERM)
select {
case <-exitSignal:
// 收到信号
case <-Context.Done():
// 被 Stop 调用
}
// 关闭模块
modules.ShutdownModules(dependModules)
flog.Println("应用已退出!")
}