mirror of https://github.com/ouqiang/gocron
增加以守护进程模式运行
parent
95278d373e
commit
4a77958556
38
cmd/web.go
38
cmd/web.go
|
@ -15,11 +15,16 @@ import (
|
||||||
"time"
|
"time"
|
||||||
"io"
|
"io"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"os/exec"
|
||||||
|
"github.com/ouqiang/gocron/modules/utils"
|
||||||
)
|
)
|
||||||
|
|
||||||
// web服务器默认端口
|
// web服务器默认端口
|
||||||
const DefaultPort = 5920
|
const DefaultPort = 5920
|
||||||
|
|
||||||
|
const InitProcess = 1
|
||||||
|
|
||||||
var CmdWeb = cli.Command{
|
var CmdWeb = cli.Command{
|
||||||
Name: "web",
|
Name: "web",
|
||||||
Usage: "run web server",
|
Usage: "run web server",
|
||||||
|
@ -28,17 +33,23 @@ var CmdWeb = cli.Command{
|
||||||
cli.IntFlag{
|
cli.IntFlag{
|
||||||
Name: "port,p",
|
Name: "port,p",
|
||||||
Value: DefaultPort,
|
Value: DefaultPort,
|
||||||
Usage: "bind port number",
|
Usage: "bind port",
|
||||||
},
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "env,e",
|
Name: "env,e",
|
||||||
Value: "prod",
|
Value: "prod",
|
||||||
Usage: "runtime environment, dev|test|prod",
|
Usage: "runtime environment, dev|test|prod",
|
||||||
},
|
},
|
||||||
|
cli.BoolFlag{
|
||||||
|
Name: "d",
|
||||||
|
Usage: "-d=true, run as daemon process",
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(ctx *cli.Context) {
|
func run(ctx *cli.Context) {
|
||||||
|
// 设置守护进程
|
||||||
|
becomeDaemon(ctx)
|
||||||
// 设置运行环境
|
// 设置运行环境
|
||||||
setEnvironment(ctx)
|
setEnvironment(ctx)
|
||||||
// 初始化应用
|
// 初始化应用
|
||||||
|
@ -57,6 +68,31 @@ func run(ctx *cli.Context) {
|
||||||
m.Run(port)
|
m.Run(port)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func becomeDaemon(ctx *cli.Context) {
|
||||||
|
// 不支持windows
|
||||||
|
if utils.IsWindows() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if !ctx.IsSet("d") {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if os.Getppid() == InitProcess {
|
||||||
|
// 子进程不再处理
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
filePath, _:= filepath.Abs(os.Args[0])
|
||||||
|
cmd := exec.Command(filePath, os.Args[1:]...)
|
||||||
|
cmd.Stdin = os.Stdin
|
||||||
|
cmd.Stdout = os.Stdout
|
||||||
|
cmd.Stderr = os.Stderr
|
||||||
|
cmd.Start()
|
||||||
|
|
||||||
|
// 父进程退出, 子进程由init-1号进程收养
|
||||||
|
os.Exit(0)
|
||||||
|
}
|
||||||
|
|
||||||
func initModule() {
|
func initModule() {
|
||||||
if !app.Installed {
|
if !app.Installed {
|
||||||
return
|
return
|
||||||
|
|
|
@ -252,7 +252,9 @@ func (engine *Engine) Close() error {
|
||||||
func (engine *Engine) Ping() error {
|
func (engine *Engine) Ping() error {
|
||||||
session := engine.NewSession()
|
session := engine.NewSession()
|
||||||
defer session.Close()
|
defer session.Close()
|
||||||
engine.logger.Infof("PING DATABASE %v", engine.DriverName())
|
if engine.showSQL {
|
||||||
|
engine.logger.Infof("PING DATABASE %v", engine.DriverName())
|
||||||
|
}
|
||||||
return session.Ping()
|
return session.Ping()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue