Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
admpub committed Jan 23, 2024
1 parent 5148b40 commit 1aaf227
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 12 deletions.
8 changes: 6 additions & 2 deletions application/library/config/cliconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,16 @@ func (c *CLIConfig) ParseConfig() {

// RunStartup manager启动时同时启动的服务
func (c *CLIConfig) RunStartup() {
c.ParseConfig()
c.Startup = strings.TrimSpace(c.Startup)
if len(c.Startup) < 1 || !IsInstalled() {
return
}
for _, serverType := range param.StringSlice(strings.Split(c.Startup, `,`)).Unique() {
serverTypes := param.StringSlice(strings.Split(c.Startup, `,`)).Unique()
if len(serverTypes) == 0 {
return
}
c.ParseConfig()
for _, serverType := range serverTypes {
serverType = strings.TrimSpace(serverType)
cm := cmder.Get(serverType)
if cm != nil {
Expand Down
11 changes: 10 additions & 1 deletion application/library/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ func (p *program) run(logPrefix string) error {
err = p.cmd.Wait()
}
if err != nil {
p.logger.Error(logPrefix+"Error running:", err)
p.logger.Error(logPrefix+"Error running: ", err)
}
return err
}
Expand All @@ -220,6 +220,15 @@ func (p *program) retryableRun() {
// Do work here
err := p.run(``)
if err == nil {
if p.cmd != nil && p.cmd.ProcessState != nil {
var result string
if p.cmd.ProcessState.Success() {
result = `successful`
} else {
result = `failed`
}
p.logger.Infof("Process execution result: %s", result)
}
return
}
maxRetries := p.MaxRetries
Expand Down
35 changes: 26 additions & 9 deletions tool/startup/main.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"log"
"os"
"os/signal"
"path/filepath"
Expand All @@ -10,6 +9,7 @@ import (
"syscall"

"github.com/admpub/go-ps"
"github.com/admpub/log"
_ "github.com/admpub/nging/v5/tool/startup/ico"
)

Expand All @@ -22,6 +22,7 @@ var (
VERSION = `0.0.1`
MAIN_EXE = `nging`
EXIT_CODE = `124`
logger = log.New(`startup`)
)

func isExitCode(exitCode int, exitCodes []int) bool {
Expand All @@ -40,25 +41,26 @@ func underMainProcess() bool {
}
proc, err := ps.FindProcess(ppid)
if err != nil {
log.Println(`ps.FindProcess: `, err)
logger.Debug(`ps.FindProcess: `, err)
return false
}
name := filepath.Base(proc.Executable())
matched := MAIN_EXE == name
if matched {
proc, err := os.FindProcess(ppid)
if err != nil {
log.Println(`os.FindProcess: `, err)
logger.Debug(`os.FindProcess: `, err)
return false
}
if err = proc.Kill(); err != nil && err != os.ErrProcessDone {
log.Println(`proc.Kill: `, err)
logger.Debug(`proc.Kill: `, err)
}
}
return matched
}

func main() {
logger.Sync()
var exitCodes []int
for _, exitCode := range strings.Split(EXIT_CODE, `,`) {
exitCodeN, _ := strconv.Atoi(exitCode)
Expand All @@ -79,6 +81,15 @@ func main() {
}
var disabledLoop bool
workDir := filepath.Dir(executable)

logDir := filepath.Join(workDir, `data/logs`)
os.MkdirAll(logDir, os.ModePerm)
ft := log.NewFileTarget()
filepathSeparator := string([]byte{filepath.Separator})
ft.FileName = logDir + filepathSeparator + `startup_{date:20060102}.log` //按天分割日志
ft.MaxLevel = log.LevelInfo
logger.AddTarget(ft)

executable = filepath.Join(workDir, MAIN_EXE)
procArgs := []string{executable}
if len(os.Args) > 1 {
Expand All @@ -90,7 +101,7 @@ func main() {
}
var proc *os.Process
var state *os.ProcessState
log.Println(strings.Join(procArgs, ` `))
logger.Debug(strings.Join(procArgs, ` `))
go func() {
shutdown := make(chan os.Signal, 1)
signal.Notify(
Expand All @@ -102,6 +113,7 @@ func main() {
if proc != nil {
proc.Signal(sig)
}
logger.Debug(`received signal: `, sig.String())
os.Exit(0)
}()

Expand All @@ -112,26 +124,31 @@ START:
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
})
if err != nil {
log.Fatal(err)
logger.Fatal(err)
}

pidDir := filepath.Join(workDir, `data/pid/daemon`)
os.MkdirAll(pidDir, os.ModePerm)
os.WriteFile(pidDir+filepathSeparator+`startup.pid`, []byte(strconv.Itoa(proc.Pid)), os.ModePerm)

state, err = proc.Wait()
if disabledLoop {
return
}
if err != nil {
log.Println(err)
logger.Debug(err)
goto START
}
if state.Exited() {
if isExitCode(state.ExitCode(), exitCodes) {
if underMainProcess() {
log.Println(`[UnderMainProcess]exitCode:`, state.ExitCode())
logger.Info(`[UnderMainProcess]exitCode:`, state.ExitCode())
proc.Signal(syscall.SIGTERM)
os.Exit(0)
}
goto START
}
log.Println(`exitCode:`, state.ExitCode())
logger.Info(`exit code: `, state.ExitCode())
os.Exit(state.ExitCode())
}
}

0 comments on commit 1aaf227

Please sign in to comment.