Skip to content

Commit

Permalink
新增日志目录自定义参数
Browse files Browse the repository at this point in the history
  • Loading branch information
liangqi1 committed Jan 4, 2023
1 parent 919db3c commit bdfbb54
Show file tree
Hide file tree
Showing 15 changed files with 77 additions and 36 deletions.
4 changes: 2 additions & 2 deletions DEVELOP.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ sh init_dev.sh
## 测试开发

```shell
sudo PMON2_CONF=config/config-dev.yml ./bin/pmond
sudo PMON2_CONF=config/config-dev.yml ./bin/pmon2 exec bin/test
sudo PMON2_DEBUG=true PMON2_CONF=config/config-dev.yml ./bin/pmond
sudo PMON2_DEBUG=true PMON2_CONF=config/config-dev.yml ./bin/pmon2 exec bin/test
```

因为 `pmon2` 启动进程使用的是fork/exec,所以需要sudo或root级别权限。
9 changes: 6 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ Go官方一直没有提供任何进程管理工具,对于 `Go` 服务的部署

```bash
# CentOS6
sudo yum install -y https://github.com/ntt360/pmon2/releases/download/v1.12.0/pmon2-1.12.0-1.el6.x86_64.rpm
sudo yum install -y https://github.com/ntt360/pmon2/releases/download/v1.12.0/pmon2-1.12.1-1.el6.x86_64.rpm

# CentOS7
sudo yum install -y https://github.com/ntt360/pmon2/releases/download/v1.12.0/pmon2-1.12.0-1.el7.x86_64.rpm
sudo yum install -y https://github.com/ntt360/pmon2/releases/download/v1.12.0/pmon2-1.12.1-1.el7.x86_64.rpm

# CentOS8
sudo yum install -y https://github.com/ntt360/pmon2/releases/download/v1.12.0/pmon2-1.12.0-1.el8.x86_64.rpm
sudo yum install -y https://github.com/ntt360/pmon2/releases/download/v1.12.0/pmon2-1.12.1-1.el8.x86_64.rpm
```

:exclamation::exclamation: **注意:** :exclamation::exclamation:
Expand Down Expand Up @@ -98,6 +98,9 @@ sudo pmon2 run [./二进制文件名] [参数1] [参数2] ...
// 进程运行日志,不配置则使用默认路径:/var/log/pmon2/
--log -l

// 仅自定义日志目录,优先级低于 --log
--log_dir -d

// 进程自定义参数,多个参数以空格分割
--args -a "-arg1=val1 -arg2=val2"

Expand Down
4 changes: 2 additions & 2 deletions app/executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
"syscall"
)

func Exec(processFile, customLogFile, name, extArgs string, user *user.User, autoRestart bool) (*model.Process, error) {
logPath, err := getLogPath(customLogFile, crypto.Crc32Hash(processFile))
func Exec(processFile, customLogFile, name, extArgs string, user *user.User, autoRestart bool, logDir string) (*model.Process, error) {
logPath, err := getLogPath(customLogFile, crypto.Crc32Hash(processFile), logDir)
if err != nil {
return nil, err
}
Expand Down
11 changes: 9 additions & 2 deletions app/executor/process_log.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,13 @@ import (

const logSuffix = ".log"

func getLogPath(customLogFile string, hash string) (string, error) {
prjDir := strings.TrimRight(app.Config.GetLogsDir(), "/")
func getLogPath(customLogFile string, hash string, logDir string) (string, error) {
if len(logDir) <= 0 {
app.Log.Debugf("custom log dir: %s \n", logDir)
logDir = app.Config.GetLogsDir()
}

prjDir := strings.TrimRight(logDir, "/")
if len(customLogFile) <= 0 {
_, err := os.Stat(prjDir)
if os.IsNotExist(err) {
Expand All @@ -23,6 +28,8 @@ func getLogPath(customLogFile string, hash string) (string, error) {
customLogFile = prjDir + "/" + hash + logSuffix
}

app.Log.Debugf("log file is: %s \n", customLogFile)

return customLogFile, nil
}

Expand Down
5 changes: 1 addition & 4 deletions app/god/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,6 @@ func checkFork(process model.Process) bool {
}

func restartProcess(p model.Process) error {
//fmt.Printf("Monitor: try get process (%d) status \n", p.Pid)
_, err := os.Stat(fmt.Sprintf("/proc/%d/status", p.Pid))
if err == nil { // process already running
//fmt.Printf("Monitor: process (%d) already running \n", p.Pid)
Expand All @@ -100,7 +99,6 @@ func restartProcess(p model.Process) error {

// proc status file not exit
if os.IsNotExist(err) && (p.Status == model.StatusRunning || p.Status == model.StatusFailed) {
//fmt.Printf("Monitor: process not exist %s \n", p.Status)
if checkFork(p) {
return nil
}
Expand All @@ -114,8 +112,7 @@ func restartProcess(p model.Process) error {
return nil
}

//fmt.Printf("try to restart process %d \n", p.Pid)
_, err := process2.TryStart(p)
_, err := process2.TryStart(p, "")
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion app/model/exec_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import "encoding/json"
type ExecFlags struct {
User string `json:"user"`
Log string `json:"log"`
LogDir string `json:"log_dir"`
NoAutoRestart bool `json:"no_auto_restart"`
Args string `json:"args"`
Name string `json:"name"`
}

func (ExecFlags) Parse(jsonStr string) (*ExecFlags, error) {
func (e *ExecFlags) Parse(jsonStr string) (*ExecFlags, error) {
var m ExecFlags
err := json.Unmarshal([]byte(jsonStr), &m)
if err != nil {
Expand Down
33 changes: 21 additions & 12 deletions app/svc/process/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ import (
"strconv"
)

func FindByProcessFile(pFile string) *model.Process {
var rel model.Process
err := app.Db().First(&rel, "process_file = ?", pFile).Error
if err != nil {
return nil
}

return &rel
}

func IsRunning(pid int) bool {
_, err := os.Stat(fmt.Sprintf("/proc/%d/status", pid))
if err != nil {
Expand All @@ -30,7 +20,7 @@ func IsRunning(pid int) bool {
return true
}

func TryStop(forced bool, p *model.Process ) error {
func TryStop(forced bool, p *model.Process) error {
var cmd *exec.Cmd
if forced {
cmd = exec.Command("kill", "-9", strconv.Itoa(p.Pid))
Expand All @@ -48,7 +38,7 @@ func TryStop(forced bool, p *model.Process ) error {
return app.Db().Save(p).Error
}

func TryStart(m model.Process) ([]string, error) {
func TryStart(m model.Process, flags string) ([]string, error) {
var flagsModel = model.ExecFlags{
User: m.Username,
Log: m.Log,
Expand All @@ -57,6 +47,25 @@ func TryStart(m model.Process) ([]string, error) {
Name: m.Name,
}

if len(flags) > 0 {
app.Log.Debugf("start with flags: %s \n", flags)
execFlags := model.ExecFlags{}
curFlag, err := execFlags.Parse(flags)
if err != nil {
return nil, err
}

if len(curFlag.Log) > 0 {
flagsModel.Log = curFlag.Log
}

// log dir
if len(curFlag.LogDir) > 0 && len(curFlag.Log) == 0 {
flagsModel.LogDir = curFlag.LogDir
flagsModel.Log = ""
}
}

data, err := proxy.RunProcess([]string{"restart", m.ProcessFile, flagsModel.Json()})
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions client/cmd/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ func init() {
Cmd.Flags().StringVarP(&flag.Log, "log", "l", "", "the process stdout log")
Cmd.Flags().StringVarP(&flag.Args, "args", "a", "", "the process extra arguments")
Cmd.Flags().StringVar(&flag.Name, "name", "", "run process name")
Cmd.Flags().StringVarP(&flag.LogDir, "log_dir", "d", "", "the process stdout log dir")
}

func cmdRun(args []string, flags string) {
Expand Down
13 changes: 10 additions & 3 deletions client/cmd/restart/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ import (
"os"
)

var flag model.ExecFlags

var Cmd = &cobra.Command{
Use: "restart",
Short: "restart some process by id or name",
Run: func(cmd *cobra.Command, args []string) {
cmdRun(args)
cmdRun(args, flag.Json())
},
}

func cmdRun(args []string) {
func init() {
Cmd.Flags().StringVarP(&flag.LogDir, "log_dir", "d", "", "the process stdout log dir")
Cmd.Flags().StringVarP(&flag.Log, "log", "l", "", "the process stdout log")
}

func cmdRun(args []string, flags string) {
if len(args) == 0 {
app.Log.Fatal("please input restart process id or name")
}
Expand All @@ -36,7 +43,7 @@ func cmdRun(args []string) {
}
}

rel, err := process.TryStart(m)
rel, err := process.TryStart(m, flags)
if err != nil {
if len(os.Getenv("PMON2_DEBUG")) > 0 {
app.Log.Fatalf("%+v", err)
Expand Down
13 changes: 10 additions & 3 deletions client/cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,22 @@ import (
"os"
)

var flag model.ExecFlags

var Cmd = &cobra.Command{
Use: "start",
Short: "start some process by id or name",
Run: func(cmd *cobra.Command, args []string) {
cmdRun(args)
cmdRun(args, flag.Json())
},
}

func cmdRun(args []string) {
func init() {
Cmd.Flags().StringVarP(&flag.LogDir, "log_dir", "d", "", "the process stdout log dir")
Cmd.Flags().StringVarP(&flag.Log, "log", "l", "", "the process stdout log")
}

func cmdRun(args []string, flags string) {
if len(args) == 0 {
app.Log.Fatal("please input start process id or name")
}
Expand All @@ -39,7 +46,7 @@ func cmdRun(args []string) {
return
}

rel, err := process.TryStart(m)
rel, err := process.TryStart(m, flags)
if err != nil {
if len(os.Getenv("PMON2_DEBUG")) > 0 {
app.Log.Fatalf("%+v", err)
Expand Down
3 changes: 2 additions & 1 deletion client/proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ func RunProcess(args []string) ([]byte, error) {

var output string

flagModel, err := model.ExecFlags{}.Parse(args[2])
flags := model.ExecFlags{}
flagModel, err := flags.Parse(args[2])
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down
7 changes: 6 additions & 1 deletion client/worker/restart.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ func Restart(pFile string, flags *model.ExecFlags) (string, error) {
m.Log = cstLog
}

// if reset log dir
if len(flags.LogDir) > 0 && len(flags.Log) == 0 {
m.Log = ""
}

cstName := flags.Name
if len(cstName) > 0 && cstName != m.Name {
m.Name = cstName
Expand All @@ -42,7 +47,7 @@ func Restart(pFile string, flags *model.ExecFlags) (string, error) {
return "", err
}

process, err := executor.Exec(m.ProcessFile, m.Log, m.Name, m.Args, runUser, !flags.NoAutoRestart)
process, err := executor.Exec(m.ProcessFile, m.Log, m.Name, m.Args, runUser, !flags.NoAutoRestart, flags.LogDir)
if err != nil {
return "", err
}
Expand Down
2 changes: 1 addition & 1 deletion client/worker/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func Start(processFile string, flags *model.ExecFlags) (string, error) {
return "", errors.Errorf("process name: %s already exist, please set other name by --name", name)
}
// start process
process, err := executor.Exec(processFile, flags.Log, name, flags.Args, runUser, !flags.NoAutoRestart)
process, err := executor.Exec(processFile, flags.Log, name, flags.Args, runUser, !flags.NoAutoRestart, flags.LogDir)
if err != nil {
return "", err
}
Expand Down
2 changes: 2 additions & 0 deletions cmd/pmond/pmond.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ func main() {
log.Fatal(err)
}

log.Printf("pmon2 daemon is running! \n")

// start monitor process file
god.NewMonitor()
}
3 changes: 2 additions & 1 deletion init_dev.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ set -e

rootDir=$(cd "$(dirname "$0")"; pwd)

rm -rf "$rootDir/config/config-dev.yml"
sudo rm -rf "$rootDir/config/config-dev.yml" "$rootDir/tmp"

# 写配置
cd "config"
Expand All @@ -30,6 +30,7 @@ fi
mkdir -p "$logs"

# build go bin
go mod tidy
go build -o bin/pmon2 cmd/pmon2/pmon2.go
go build -o bin/pmond cmd/pmond/pmond.go
# `bin/test` 进程是用于测试模拟的业务进程,开发也可以测试自己的业务进程。
Expand Down

0 comments on commit bdfbb54

Please sign in to comment.