@@ -30,9 +30,39 @@ import (
3030
3131var configPath = "config.yaml" // 全局可用
3232var appStatus = "未启动"
33- var currentAppPid int
3433var extraArgs []string
3534var lastFoundArgs []string // 仅记录 FindProcessByPath 找到的参数(不含exe路径)
35+ var currentAppPid int
36+
37+ var (
38+ idleTimer * time.Timer
39+ idleTimerC <- chan time.Time
40+ )
41+
42+ func setCurrentAppPid (pid int ) {
43+ currentAppPid = pid
44+ if pid == 0 {
45+ if idleTimer == nil {
46+ idleTimer = time .NewTimer (2 * time .Minute )
47+ idleTimerC = idleTimer .C
48+ go func () {
49+ <- idleTimerC
50+ if currentAppPid == 0 {
51+ fmt .Println ("[evs] 2分钟无应用运行,自动退出" )
52+ os .Exit (0 )
53+ }
54+ }()
55+ }
56+ } else {
57+ if idleTimer != nil {
58+ if ! idleTimer .Stop () {
59+ <- idleTimer .C // drain
60+ }
61+ idleTimer = nil
62+ idleTimerC = nil
63+ }
64+ }
65+ }
3666
3767func internalGetConfig () (* internal.Config , error ) {
3868 cfg := internal .GetConfig ()
@@ -119,10 +149,10 @@ func internalKillCurrentApp() error {
119149 err := internal .KillProcessTreeAndWait (currentAppPid )
120150 if err == nil {
121151 appStatus = "已终止 (PID=" + fmt .Sprint (currentAppPid ) + ")"
152+ setCurrentAppPid (0 )
122153 } else {
123154 appStatus = "终止失败 (PID=" + fmt .Sprint (currentAppPid ) + ")"
124155 }
125- currentAppPid = 0
126156 return err
127157}
128158
@@ -160,24 +190,24 @@ func runAppProxy(args []string) {
160190 switch status {
161191 case "running" :
162192 appStatus = fmt .Sprintf ("运行中 (PID=%d)" , pid )
163- currentAppPid = pid
193+ setCurrentAppPid ( pid )
164194 fmt .Printf ("已启动应用: %s (PID=%d)\n " , app .Path , pid )
165195 case "exited" :
166196 appStatus = fmt .Sprintf ("已退出 (PID=%d)" , pid )
167197 fmt .Println ("应用已正常退出" )
168- currentAppPid = 0
198+ setCurrentAppPid ( 0 )
169199 case "exit_failed" :
170200 appStatus = fmt .Sprintf ("异常退出 (PID=%d)" , pid )
171201 fmt .Printf ("应用异常退出,返回码非0: %v\n " , exitErr )
172- currentAppPid = 0
202+ setCurrentAppPid ( 0 )
173203 case "killed" :
174204 appStatus = fmt .Sprintf ("被终止 (PID=%d)" , pid )
175205 fmt .Printf ("应用被信号终止: %v\n " , exitErr )
176- currentAppPid = 0
206+ setCurrentAppPid ( 0 )
177207 case "crashed" :
178208 appStatus = fmt .Sprintf ("已崩溃 (PID=%d)" , pid )
179209 fmt .Printf ("应用崩溃: %v\n " , exitErr )
180- currentAppPid = 0
210+ setCurrentAppPid ( 0 )
181211 case "start_failed" :
182212 appStatus = "启动失败"
183213 fmt .Printf ("启动应用失败: %v\n " , exitErr )
@@ -299,7 +329,7 @@ func handleConsoleConn(conn net.Conn, configPath string) {
299329 case "exit" :
300330 if currentAppPid != 0 {
301331 _ = internalKillCurrentApp ()
302- currentAppPid = 0
332+ setCurrentAppPid ( 0 )
303333 }
304334 conn .Write ([]byte ("OK\n " ))
305335 time .Sleep (50 * time .Millisecond )
@@ -358,14 +388,14 @@ func main() {
358388 lastFoundArgs = nil
359389 }
360390 fmt .Printf ("[DEBUG] lastFoundArgs 赋值后: %v\n " , lastFoundArgs )
361- currentAppPid = pid
391+ setCurrentAppPid ( pid )
362392 }
363393
364394 if shouldStart {
365395 go runAppProxy (nil )
366396 }
367397
368- // 启动 socket 服务
398+ // 启动 soc
369399 startConsoleServer (configPath )
370400 }
371401}
0 commit comments