mirror of https://github.com/ouqiang/gocron
parent
519c6613c7
commit
94197e0511
13
README.md
13
README.md
|
@ -48,11 +48,16 @@
|
||||||
3. 编译 `go build`
|
3. 编译 `go build`
|
||||||
4. 启动、访问方式同上
|
4. 启动、访问方式同上
|
||||||
|
|
||||||
### 启动可选参数
|
### 命令
|
||||||
|
|
||||||
|
* gocron web
|
||||||
|
* -p 端口, 指定端口, 默认5920
|
||||||
|
* -e 指定运行环境, dev|test|prod, dev模式下可查看更多日志信息, 默认prod
|
||||||
|
* -d 后台运行
|
||||||
|
* -h 查看帮助
|
||||||
|
* gocron serv
|
||||||
|
* -s stop|status stop:停止gocron status:查看运行状态
|
||||||
|
|
||||||
* -p 端口, 指定端口, 默认5920
|
|
||||||
* -e 指定运行环境, dev|test|prod, dev模式下可查看更多日志信息, 默认prod
|
|
||||||
* -h 查看帮助
|
|
||||||
|
|
||||||
## 安全
|
## 安全
|
||||||
* 使用`https`访问保证数据传输安全, 可在web服务器如nginx中配置https,通过反向代理,访问内部的gocron
|
* 使用`https`访问保证数据传输安全, 可在web服务器如nginx中配置https,通过反向代理,访问内部的gocron
|
||||||
|
|
|
@ -0,0 +1,67 @@
|
||||||
|
package cmd
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/urfave/cli"
|
||||||
|
"fmt"
|
||||||
|
"github.com/ouqiang/gocron/modules/utils"
|
||||||
|
"github.com/ouqiang/gocron/modules/app"
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
)
|
||||||
|
|
||||||
|
var CmdServ = cli.Command{
|
||||||
|
Name: "serv",
|
||||||
|
Usage: "manage gocron, ./gocron serv -s stop|status",
|
||||||
|
Action: runServ,
|
||||||
|
Flags: []cli.Flag{
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "s",
|
||||||
|
Value:"",
|
||||||
|
Usage: "stop|status",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func runServ(ctx *cli.Context) {
|
||||||
|
if utils.IsWindows() {
|
||||||
|
fmt.Println("not support on windows")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
option := ctx.String("s")
|
||||||
|
if !utils.InStringSlice([]string{"stop", "status"}, option) {
|
||||||
|
fmt.Println("invalid option")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
app.InitEnv()
|
||||||
|
pid := app.GetPid()
|
||||||
|
if pid <= 0 {
|
||||||
|
fmt.Println("not running")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
process ,err := os.FindProcess(pid)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("not running", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
switch option {
|
||||||
|
case "stop":
|
||||||
|
stop(process)
|
||||||
|
case "status":
|
||||||
|
status(process)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func stop(process *os.Process) {
|
||||||
|
fmt.Println("stopping gocron......")
|
||||||
|
err := process.Signal(syscall.SIGTERM)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Println("failed to kill process", err)
|
||||||
|
} else {
|
||||||
|
fmt.Println("stopped")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
func status(process *os.Process) {
|
||||||
|
fmt.Printf("running, pid-[%d]\n", process.Pid)
|
||||||
|
}
|
15
cmd/web.go
15
cmd/web.go
|
@ -28,7 +28,7 @@ const InitProcess = 1
|
||||||
var CmdWeb = cli.Command{
|
var CmdWeb = cli.Command{
|
||||||
Name: "web",
|
Name: "web",
|
||||||
Usage: "run web server",
|
Usage: "run web server",
|
||||||
Action: run,
|
Action: runWeb,
|
||||||
Flags: []cli.Flag{
|
Flags: []cli.Flag{
|
||||||
cli.IntFlag{
|
cli.IntFlag{
|
||||||
Name: "port,p",
|
Name: "port,p",
|
||||||
|
@ -47,13 +47,14 @@ var CmdWeb = cli.Command{
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
func run(ctx *cli.Context) {
|
func runWeb(ctx *cli.Context) {
|
||||||
// 设置守护进程
|
// 设置守护进程
|
||||||
becomeDaemon(ctx)
|
becomeDaemon(ctx)
|
||||||
// 设置运行环境
|
// 设置运行环境
|
||||||
setEnvironment(ctx)
|
setEnvironment(ctx)
|
||||||
// 初始化应用
|
// 初始化应用
|
||||||
app.InitEnv()
|
app.InitEnv()
|
||||||
|
app.WritePid()
|
||||||
// 初始化模块 DB、定时任务等
|
// 初始化模块 DB、定时任务等
|
||||||
initModule()
|
initModule()
|
||||||
// 捕捉信号,配置热更新等
|
// 捕捉信号,配置热更新等
|
||||||
|
@ -65,6 +66,7 @@ func run(ctx *cli.Context) {
|
||||||
// 注册中间件.
|
// 注册中间件.
|
||||||
routers.RegisterMiddleware(m)
|
routers.RegisterMiddleware(m)
|
||||||
port := parsePort(ctx)
|
port := parsePort(ctx)
|
||||||
|
fmt.Println("server start")
|
||||||
m.Run(port)
|
m.Run(port)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -195,8 +197,13 @@ func getWebLogWriter() io.Writer {
|
||||||
|
|
||||||
// 应用退出
|
// 应用退出
|
||||||
func shutdown() {
|
func shutdown() {
|
||||||
if !app.Installed {
|
defer func() {
|
||||||
|
app.RemovePid()
|
||||||
|
logger.Info("已退出")
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
|
}()
|
||||||
|
|
||||||
|
if !app.Installed {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
logger.Info("应用准备退出")
|
logger.Info("应用准备退出")
|
||||||
|
@ -222,6 +229,4 @@ func shutdown() {
|
||||||
time.Sleep(3 * time.Second)
|
time.Sleep(3 * time.Second)
|
||||||
taskNumInRunning = service.TaskNum.Num()
|
taskNumInRunning = service.TaskNum.Num()
|
||||||
}
|
}
|
||||||
logger.Info("已退出")
|
|
||||||
os.Exit(0)
|
|
||||||
}
|
}
|
|
@ -13,7 +13,7 @@ import (
|
||||||
"github.com/ouqiang/gocron/cmd"
|
"github.com/ouqiang/gocron/cmd"
|
||||||
)
|
)
|
||||||
|
|
||||||
const AppVersion = "0.2"
|
const AppVersion = "0.3"
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
|
@ -22,6 +22,7 @@ func main() {
|
||||||
app.Version = AppVersion
|
app.Version = AppVersion
|
||||||
app.Commands = []cli.Command{
|
app.Commands = []cli.Command{
|
||||||
cmd.CmdWeb,
|
cmd.CmdWeb,
|
||||||
|
cmd.CmdServ,
|
||||||
}
|
}
|
||||||
app.Flags = append(app.Flags, []cli.Flag{}...)
|
app.Flags = append(app.Flags, []cli.Flag{}...)
|
||||||
app.Run(os.Args)
|
app.Run(os.Args)
|
||||||
|
|
|
@ -7,6 +7,8 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"github.com/ouqiang/gocron/modules/utils"
|
"github.com/ouqiang/gocron/modules/utils"
|
||||||
"gopkg.in/ini.v1"
|
"gopkg.in/ini.v1"
|
||||||
|
"io/ioutil"
|
||||||
|
"strconv"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -17,8 +19,10 @@ var (
|
||||||
AppConfig string // 应用配置文件
|
AppConfig string // 应用配置文件
|
||||||
Installed bool // 应用是否安装过
|
Installed bool // 应用是否安装过
|
||||||
Setting *ini.Section // 应用配置
|
Setting *ini.Section // 应用配置
|
||||||
|
PidFile string
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
func InitEnv() {
|
func InitEnv() {
|
||||||
runtime.GOMAXPROCS(runtime.NumCPU())
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
||||||
logger.InitLogger()
|
logger.InitLogger()
|
||||||
|
@ -31,6 +35,7 @@ func InitEnv() {
|
||||||
LogDir = AppDir + "/log"
|
LogDir = AppDir + "/log"
|
||||||
DataDir = AppDir + "/data"
|
DataDir = AppDir + "/data"
|
||||||
AppConfig = ConfDir + "/app.ini"
|
AppConfig = ConfDir + "/app.ini"
|
||||||
|
PidFile = LogDir + "/gocron.pid"
|
||||||
checkDirExists(ConfDir, LogDir, DataDir)
|
checkDirExists(ConfDir, LogDir, DataDir)
|
||||||
Installed = IsInstalled()
|
Installed = IsInstalled()
|
||||||
}
|
}
|
||||||
|
@ -45,6 +50,33 @@ func IsInstalled() bool {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func WritePid() {
|
||||||
|
pid := os.Getpid()
|
||||||
|
pidStr := strconv.Itoa(pid)
|
||||||
|
err := ioutil.WriteFile(PidFile, []byte(pidStr), 0644)
|
||||||
|
if err != nil {
|
||||||
|
logger.Fatalf("写入pid文件失败", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPid() int {
|
||||||
|
bytes, err := ioutil.ReadFile(PidFile)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
pidStr := string(bytes)
|
||||||
|
pid, err := strconv.Atoi(pidStr)
|
||||||
|
if err != nil {
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
return pid
|
||||||
|
}
|
||||||
|
|
||||||
|
func RemovePid() {
|
||||||
|
os.Remove(PidFile)
|
||||||
|
}
|
||||||
|
|
||||||
// 创建安装锁文件
|
// 创建安装锁文件
|
||||||
func CreateInstallLock() error {
|
func CreateInstallLock() error {
|
||||||
_, err := os.Create(ConfDir + "/install.lock")
|
_, err := os.Create(ConfDir + "/install.lock")
|
||||||
|
|
Loading…
Reference in New Issue