forked from ouqiang/gocron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gocron.go
199 lines (171 loc) · 3.84 KB
/
gocron.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
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// Command gocron
//go:generate statik -src=../../web/public -dest=../../internal -f
package main
import (
"os"
"os/signal"
"syscall"
macaron "gopkg.in/macaron.v1"
"github.com/mayouzi/gocron/internal/models"
"github.com/mayouzi/gocron/internal/modules/app"
"github.com/mayouzi/gocron/internal/modules/logger"
"github.com/mayouzi/gocron/internal/modules/setting"
"github.com/mayouzi/gocron/internal/routers"
"github.com/mayouzi/gocron/internal/service"
"github.com/mayouzi/goutil"
"github.com/urfave/cli"
)
var (
AppVersion = "1.6.1"
BuildDate, GitCommit string
)
// web服务器默认端口
const DefaultPort = 5920
func main() {
cliApp := cli.NewApp()
cliApp.Name = "gocron"
cliApp.Usage = "gocron service"
cliApp.Version, _ = goutil.FormatAppVersion(AppVersion, GitCommit, BuildDate)
cliApp.Commands = getCommands()
cliApp.Flags = append(cliApp.Flags, []cli.Flag{}...)
err := cliApp.Run(os.Args)
if err != nil {
logger.Fatal(err)
}
}
// getCommands
func getCommands() []cli.Command {
command := cli.Command{
Name: "web",
Usage: "run web server",
Action: runWeb,
Flags: []cli.Flag{
cli.StringFlag{
Name: "host",
Value: "0.0.0.0",
Usage: "bind host",
},
cli.IntFlag{
Name: "port,p",
Value: DefaultPort,
Usage: "bind port",
},
cli.StringFlag{
Name: "env,e",
Value: "prod",
Usage: "runtime environment, dev|test|prod",
},
},
}
return []cli.Command{command}
}
func runWeb(ctx *cli.Context) {
// 设置运行环境
setEnvironment(ctx)
// 初始化应用
app.InitEnv(AppVersion)
// 初始化模块 DB、定时任务等
initModule()
// 捕捉信号,配置热更新等
go catchSignal()
m := macaron.Classic()
// 注册路由
routers.Register(m)
// 注册中间件.
routers.RegisterMiddleware(m)
host := parseHost(ctx)
port := parsePort(ctx)
m.Run(host, port)
}
func initModule() {
if !app.Installed {
return
}
config, err := setting.Read(app.AppConfig)
if err != nil {
logger.Fatal("读取应用配置失败", err)
}
app.Setting = config
// 初始化DB
models.Db = models.CreateDb()
// 版本升级
upgradeIfNeed()
// 初始化定时任务
service.ServiceTask.Initialize()
}
// 解析端口
func parsePort(ctx *cli.Context) int {
port := DefaultPort
if ctx.IsSet("port") {
port = ctx.Int("port")
}
if port <= 0 || port >= 65535 {
port = DefaultPort
}
return port
}
func parseHost(ctx *cli.Context) string {
if ctx.IsSet("host") {
return ctx.String("host")
}
return "0.0.0.0"
}
func setEnvironment(ctx *cli.Context) {
env := "prod"
if ctx.IsSet("env") {
env = ctx.String("env")
}
switch env {
case "test":
macaron.Env = macaron.TEST
case "dev":
macaron.Env = macaron.DEV
default:
macaron.Env = macaron.PROD
}
}
// 捕捉信号
func catchSignal() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM)
for {
s := <-c
logger.Info("收到信号 -- ", s)
switch s {
case syscall.SIGHUP:
logger.Info("收到终端断开信号, 忽略")
case syscall.SIGINT, syscall.SIGTERM:
shutdown()
}
}
}
// 应用退出
func shutdown() {
defer func() {
logger.Info("已退出")
os.Exit(0)
}()
if !app.Installed {
return
}
logger.Info("应用准备退出")
// 停止所有任务调度
logger.Info("停止定时任务调度")
service.ServiceTask.WaitAndExit()
}
// 判断应用是否需要升级, 当存在版本号文件且版本小于app.VersionId时升级
func upgradeIfNeed() {
currentVersionId := app.GetCurrentVersionId()
// 没有版本号文件
if currentVersionId == 0 {
return
}
if currentVersionId >= app.VersionId {
return
}
migration := new(models.Migration)
logger.Infof("版本升级开始, 当前版本号%d", currentVersionId)
migration.Upgrade(currentVersionId)
app.UpdateVersionFile()
logger.Infof("已升级到最新版本%d", app.VersionId)
}