refactor: 删除守护进程模块, web访问日志输出到终端, Windows不再支持后台运行

pull/21/merge
ouqiang 2017-07-22 08:54:42 +08:00
parent d13f1997dd
commit 5d97ffad3b
8 changed files with 5 additions and 161 deletions

View File

@ -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

View File

@ -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

View File

@ -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)
}

View File

@ -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)
}()

View File

@ -15,7 +15,5 @@ func main() {
} else {
addr = os.Args[1]
}
for {
server.Start(addr)
}
server.Start(addr)
}

View File

@ -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)

View File

@ -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")

View File

@ -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)