diff --git a/build.sh b/build.sh index 01e6ff6..045ea07 100644 --- a/build.sh +++ b/build.sh @@ -54,7 +54,7 @@ fi echo '开始编译调度器' if [[ $OS = 'windows' ]];then - GOOS=$OS GOARCH=$ARCH go build -tags gocron -ldflags '-w -H windowsgui' + GOOS=$OS GOARCH=$ARCH go build -tags gocron -ldflags '-w' else GOOS=$OS GOARCH=$ARCH go build -tags gocron -ldflags '-w' fi diff --git a/build_node.sh b/build_node.sh index cd9cb69..dd3d31a 100644 --- a/build_node.sh +++ b/build_node.sh @@ -60,7 +60,7 @@ fi echo '开始编译任务节点' if [[ $OS = 'windows' ]];then - GOOS=$OS GOARCH=$ARCH go build -tags node -ldflags '-w -H windowsgui' -o $EXEC_NAME + GOOS=$OS GOARCH=$ARCH go build -tags node -ldflags '-w' -o $EXEC_NAME else GOOS=$OS GOARCH=$ARCH go build -tags node -ldflags '-w' -o $EXEC_NAME fi diff --git a/cmd/serv.go b/cmd/serv.go deleted file mode 100644 index 0a792c8..0000000 --- a/cmd/serv.go +++ /dev/null @@ -1,67 +0,0 @@ -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) -} \ No newline at end of file diff --git a/cmd/web.go b/cmd/web.go index 23f2ab9..7800c1a 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -13,18 +13,12 @@ import ( "github.com/ouqiang/gocron/models" "github.com/ouqiang/gocron/modules/setting" "time" - "io" - "fmt" - "path/filepath" - "os/exec" - "github.com/ouqiang/gocron/modules/utils" "github.com/ouqiang/gocron/modules/rpc/grpcpool" ) // web服务器默认端口 const DefaultPort = 5920 -const InitProcess = 1 var CmdWeb = cli.Command{ Name: "web", @@ -54,18 +48,15 @@ var CmdWeb = cli.Command{ } func runWeb(ctx *cli.Context) { - // 设置守护进程 - becomeDaemon(ctx) // 设置运行环境 setEnvironment(ctx) // 初始化应用 app.InitEnv() - app.WritePid() // 初始化模块 DB、定时任务等 initModule() // 捕捉信号,配置热更新等 go catchSignal() - m := macaron.NewWithLogger(getWebLogWriter()) + m := macaron.Classic() // 注册路由 routers.Register(m) @@ -73,38 +64,9 @@ func runWeb(ctx *cli.Context) { routers.RegisterMiddleware(m) host := parseHost(ctx) port := parsePort(ctx) - fmt.Println("server start") m.Run(host, 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 - err := cmd.Start() - if err != nil { - logger.Fatal("创建守护进程失败", err) - } - - // 父进程退出, 子进程由init-1号进程收养 - os.Exit(0) -} - func initModule() { if !app.Installed { return @@ -177,25 +139,10 @@ func catchSignal() { } } -func getWebLogWriter() io.Writer { - if macaron.Env == macaron.DEV { - return os.Stdout - } - logFile := app.LogDir + "/access.log" - var err error - w, err := os.OpenFile(logFile, os.O_RDWR|os.O_CREATE|os.O_APPEND ,0666) - if err != nil { - fmt.Printf("日志文件[%s]打开失败", logFile) - panic(err) - } - - return w -} // 应用退出 func shutdown() { defer func() { - app.RemovePid() logger.Info("已退出") os.Exit(0) }() diff --git a/gocron-node.go b/gocron-node.go index 5096cad..e074560 100644 --- a/gocron-node.go +++ b/gocron-node.go @@ -15,7 +15,5 @@ func main() { } else { addr = os.Args[1] } - for { - server.Start(addr) - } + server.Start(addr) } \ No newline at end of file diff --git a/gocron.go b/gocron.go index a41a7d3..b988da9 100644 --- a/gocron.go +++ b/gocron.go @@ -19,7 +19,6 @@ func main() { app.Version = AppVersion app.Commands = []cli.Command{ cmd.CmdWeb, - cmd.CmdServ, } app.Flags = append(app.Flags, []cli.Flag{}...) app.Run(os.Args) diff --git a/modules/app/app.go b/modules/app/app.go index def5749..7304b12 100644 --- a/modules/app/app.go +++ b/modules/app/app.go @@ -4,11 +4,8 @@ import ( "os" "github.com/ouqiang/gocron/modules/logger" - "runtime" "github.com/ouqiang/gocron/modules/utils" "gopkg.in/ini.v1" - "io/ioutil" - "strconv" ) var ( @@ -19,12 +16,10 @@ var ( AppConfig string // 应用配置文件 Installed bool // 应用是否安装过 Setting *ini.Section // 应用配置 - PidFile string ) func InitEnv() { - runtime.GOMAXPROCS(runtime.NumCPU()) logger.InitLogger() wd, err := os.Getwd() if err != nil { @@ -35,7 +30,6 @@ func InitEnv() { LogDir = AppDir + "/log" DataDir = AppDir + "/data" AppConfig = ConfDir + "/app.ini" - PidFile = LogDir + "/gocron.pid" checkDirExists(ConfDir, LogDir, DataDir) Installed = IsInstalled() } @@ -50,33 +44,6 @@ func IsInstalled() bool { return true } -func WritePid() { - pid := os.Getpid() - pidStr := strconv.Itoa(pid) - err := ioutil.WriteFile(PidFile, []byte(pidStr), 0644) - if err != nil { - logger.Fatal("写入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 { _, err := os.Create(ConfDir + "/install.lock") diff --git a/modules/rpc/server/server.go b/modules/rpc/server/server.go index 7db666c..6f2a4d7 100644 --- a/modules/rpc/server/server.go +++ b/modules/rpc/server/server.go @@ -41,7 +41,7 @@ func Start(addr string) { } s := grpc.NewServer() pb.RegisterTaskServer(s, Server{}) - grpclog.Println("listen address ", addr) + grpclog.Println("listen ", addr) err = s.Serve(l) if err != nil { grpclog.Fatal(err)