Skip to content

Commit 2260627

Browse files
committed
update
1 parent 7bea89c commit 2260627

File tree

2 files changed

+154
-139
lines changed

2 files changed

+154
-139
lines changed

tool/startup/main.go

Lines changed: 2 additions & 139 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,8 @@
11
package main
22

33
import (
4-
"os"
5-
"os/signal"
6-
"path/filepath"
7-
"strconv"
8-
"strings"
9-
"syscall"
10-
11-
"github.com/admpub/go-ps"
12-
"github.com/admpub/log"
134
_ "github.com/admpub/nging/v5/tool/startup/ico"
5+
"github.com/admpub/nging/v5/tool/startup/pkg"
146
)
157

168
var (
@@ -22,137 +14,8 @@ var (
2214
VERSION = `0.0.1`
2315
MAIN_EXE = `nging`
2416
EXIT_CODE = `124`
25-
logger = log.New(`startup`)
2617
)
2718

28-
func isExitCode(exitCode int, exitCodes []int) bool {
29-
for _, code := range exitCodes {
30-
if exitCode == code {
31-
return true
32-
}
33-
}
34-
return false
35-
}
36-
37-
func underMainProcess() bool {
38-
ppid := os.Getppid()
39-
if ppid == 1 {
40-
return false
41-
}
42-
proc, err := ps.FindProcess(ppid)
43-
if err != nil {
44-
logger.Debug(`ps.FindProcess: `, err)
45-
return false
46-
}
47-
if proc == nil {
48-
return false
49-
}
50-
name := filepath.Base(proc.Executable())
51-
matched := MAIN_EXE == name
52-
if matched {
53-
proc, err := os.FindProcess(ppid)
54-
if err != nil {
55-
logger.Debug(`os.FindProcess: `, err)
56-
return false
57-
}
58-
if err = proc.Kill(); err != nil && err != os.ErrProcessDone {
59-
logger.Debug(`proc.Kill: `, err)
60-
}
61-
}
62-
return matched
63-
}
64-
6519
func main() {
66-
logger.Sync()
67-
var exitCodes []int
68-
for _, exitCode := range strings.Split(EXIT_CODE, `,`) {
69-
exitCodeN, _ := strconv.Atoi(exitCode)
70-
if exitCodeN > 0 {
71-
exitCodes = append(exitCodes, exitCodeN)
72-
}
73-
}
74-
if len(exitCodes) == 0 {
75-
exitCodes = append(exitCodes, 124)
76-
}
77-
executable, err := os.Executable()
78-
if err != nil {
79-
log.Fatal(err)
80-
}
81-
executable, err = filepath.EvalSymlinks(executable)
82-
if err != nil {
83-
log.Fatal(err)
84-
}
85-
var disabledLoop bool
86-
workDir := filepath.Dir(executable)
87-
88-
logDir := filepath.Join(workDir, `data/logs`)
89-
os.MkdirAll(logDir, os.ModePerm)
90-
ft := log.NewFileTarget()
91-
filepathSeparator := string([]byte{filepath.Separator})
92-
ft.FileName = logDir + filepathSeparator + `startup_{date:20060102}.log` //按天分割日志
93-
ft.MaxLevel = log.LevelInfo
94-
logger.AddTarget(ft)
95-
96-
executable = filepath.Join(workDir, MAIN_EXE)
97-
procArgs := []string{executable}
98-
if len(os.Args) > 1 {
99-
disabledLoop = os.Args[1] != `service` && !strings.HasPrefix(os.Args[1], `-`)
100-
procArgs = append(procArgs, os.Args[1:]...)
101-
}
102-
if !disabledLoop && len(procArgs) > 2 {
103-
disabledLoop = procArgs[1] == `service` && procArgs[2] != `run`
104-
}
105-
var proc *os.Process
106-
var state *os.ProcessState
107-
logger.Debug(strings.Join(procArgs, ` `))
108-
go func() {
109-
shutdown := make(chan os.Signal, 1)
110-
signal.Notify(
111-
shutdown,
112-
os.Interrupt, // ctrl+c 信号
113-
syscall.SIGTERM, // pkill 信号
114-
)
115-
sig := <-shutdown
116-
if proc != nil {
117-
proc.Signal(sig)
118-
}
119-
logger.Debug(`received signal: `, sig.String())
120-
os.Exit(0)
121-
}()
122-
123-
pidDir := filepath.Join(workDir, `data/pid`)
124-
os.MkdirAll(pidDir, os.ModePerm)
125-
126-
START:
127-
proc, err = os.StartProcess(executable, procArgs, &os.ProcAttr{
128-
Dir: workDir,
129-
Env: os.Environ(),
130-
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
131-
})
132-
if err != nil {
133-
logger.Fatal(err)
134-
}
135-
136-
os.WriteFile(pidDir+filepathSeparator+`nging_forked.pid`, []byte(strconv.Itoa(proc.Pid)), os.ModePerm)
137-
138-
state, err = proc.Wait()
139-
if disabledLoop {
140-
return
141-
}
142-
if err != nil {
143-
logger.Error(err)
144-
goto START
145-
}
146-
if state.Exited() {
147-
if isExitCode(state.ExitCode(), exitCodes) {
148-
if underMainProcess() {
149-
logger.Info(`[UnderMainProcess]exitCode:`, state.ExitCode())
150-
proc.Signal(syscall.SIGTERM)
151-
os.Exit(0)
152-
}
153-
goto START
154-
}
155-
logger.Info(`exit code: `, state.ExitCode())
156-
os.Exit(state.ExitCode())
157-
}
20+
pkg.Start(MAIN_EXE, EXIT_CODE)
15821
}

tool/startup/pkg/startup.go

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
package pkg
2+
3+
import (
4+
"os"
5+
"os/signal"
6+
"path/filepath"
7+
"regexp"
8+
"strconv"
9+
"strings"
10+
"syscall"
11+
12+
"github.com/admpub/go-ps"
13+
"github.com/admpub/log"
14+
)
15+
16+
var (
17+
logger = log.New(`startup`)
18+
)
19+
20+
func isExitCode(exitCode int, exitCodes []int) bool {
21+
for _, code := range exitCodes {
22+
if exitCode == code {
23+
return true
24+
}
25+
}
26+
return false
27+
}
28+
29+
func underMainProcess(mainExe string) bool {
30+
ppid := os.Getppid()
31+
if ppid == 1 {
32+
return false
33+
}
34+
proc, err := ps.FindProcess(ppid)
35+
if err != nil {
36+
logger.Debug(`ps.FindProcess: `, err)
37+
return false
38+
}
39+
if proc == nil {
40+
return false
41+
}
42+
name := filepath.Base(proc.Executable())
43+
matched := mainExe == name
44+
if matched {
45+
proc, err := os.FindProcess(ppid)
46+
if err != nil {
47+
logger.Debug(`os.FindProcess: `, err)
48+
return false
49+
}
50+
if err = proc.Kill(); err != nil && err != os.ErrProcessDone {
51+
logger.Debug(`proc.Kill: `, err)
52+
}
53+
}
54+
return matched
55+
}
56+
57+
func Start(mainExe, exitCode string) {
58+
logger.Sync()
59+
var exitCodes []int
60+
for _, exitCode := range strings.Split(exitCode, `,`) {
61+
exitCodeN, _ := strconv.Atoi(exitCode)
62+
if exitCodeN > 0 {
63+
exitCodes = append(exitCodes, exitCodeN)
64+
}
65+
}
66+
if len(exitCodes) == 0 {
67+
exitCodes = append(exitCodes, 124)
68+
}
69+
executable, err := os.Executable()
70+
if err != nil {
71+
log.Fatal(err)
72+
}
73+
executable, err = filepath.EvalSymlinks(executable)
74+
if err != nil {
75+
log.Fatal(err)
76+
}
77+
var disabledLoop bool
78+
workDir := filepath.Dir(executable)
79+
80+
logDir := filepath.Join(workDir, `data/logs`)
81+
os.MkdirAll(logDir, os.ModePerm)
82+
ft := log.NewFileTarget()
83+
filepathSeparator := string([]byte{filepath.Separator})
84+
ft.FileName = logDir + filepathSeparator + `startup_{date:20060102}.log` //按天分割日志
85+
ft.MaxLevel = log.LevelInfo
86+
logger.AddTarget(ft)
87+
88+
executable = filepath.Join(workDir, mainExe)
89+
procArgs := []string{executable}
90+
if len(os.Args) > 1 {
91+
disabledLoop = os.Args[1] != `service` && !strings.HasPrefix(os.Args[1], `-`)
92+
procArgs = append(procArgs, os.Args[1:]...)
93+
}
94+
if !disabledLoop && len(procArgs) > 2 {
95+
disabledLoop = procArgs[1] == `service` && procArgs[2] != `run`
96+
}
97+
var proc *os.Process
98+
var state *os.ProcessState
99+
logger.Debug(strings.Join(procArgs, ` `))
100+
go func() {
101+
shutdown := make(chan os.Signal, 1)
102+
signal.Notify(
103+
shutdown,
104+
os.Interrupt, // ctrl+c 信号
105+
syscall.SIGTERM, // pkill 信号
106+
)
107+
sig := <-shutdown
108+
if proc != nil {
109+
proc.Signal(sig)
110+
}
111+
logger.Debug(`received signal: `, sig.String())
112+
os.Exit(0)
113+
}()
114+
115+
pidDir := filepath.Join(workDir, `data/pid`)
116+
os.MkdirAll(pidDir, os.ModePerm)
117+
118+
START:
119+
proc, err = os.StartProcess(executable, procArgs, &os.ProcAttr{
120+
Dir: workDir,
121+
Env: os.Environ(),
122+
Files: []*os.File{os.Stdin, os.Stdout, os.Stderr},
123+
})
124+
if err != nil {
125+
logger.Fatal(err)
126+
}
127+
128+
filenameRE := regexp.MustCompile(`[^\w]+`)
129+
pidFilename := filenameRE.ReplaceAllString(strings.SplitN(mainExe, `.`, 2)[0], `_`)
130+
os.WriteFile(pidDir+filepathSeparator+pidFilename+`_forked.pid`, []byte(strconv.Itoa(proc.Pid)), os.ModePerm)
131+
132+
state, err = proc.Wait()
133+
if disabledLoop {
134+
return
135+
}
136+
if err != nil {
137+
logger.Error(err)
138+
goto START
139+
}
140+
if state.Exited() {
141+
if isExitCode(state.ExitCode(), exitCodes) {
142+
if underMainProcess(mainExe) {
143+
logger.Info(`[UnderMainProcess]exitCode:`, state.ExitCode())
144+
proc.Signal(syscall.SIGTERM)
145+
os.Exit(0)
146+
}
147+
goto START
148+
}
149+
logger.Info(`exit code: `, state.ExitCode())
150+
os.Exit(state.ExitCode())
151+
}
152+
}

0 commit comments

Comments
 (0)