11package main
22
33import (
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
168var (
@@ -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-
6519func 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}
0 commit comments