Skip to content

Commit

Permalink
取消双进程
Browse files Browse the repository at this point in the history
  • Loading branch information
fumiama committed Mar 11, 2023
1 parent 8ba0408 commit 5e7e43f
Showing 1 changed file with 0 additions and 77 deletions.
77 changes: 0 additions & 77 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,95 +1,18 @@
package main

import (
"os"
"os/exec"
"time"

"github.com/sirupsen/logrus"

"github.com/Mrs4s/go-cqhttp/cmd/gocq"
"github.com/Mrs4s/go-cqhttp/global/terminal"

_ "github.com/Mrs4s/go-cqhttp/db/leveldb" // leveldb
_ "github.com/Mrs4s/go-cqhttp/modules/silk" // silk编码模块
)

var (
nofork = len(os.Args) > 1 && func() bool {
for i := len(os.Args) - 1; i >= 0; i-- {
if os.Args[i] == "nofork" {
if i == len(os.Args)-1 {
os.Args = os.Args[:i]
} else {
os.Args = append(os.Args[:i], os.Args[i+1:]...)
}
return true
}
}
return false
}()
norecover = len(os.Args) > 1 && func() bool {
for i := len(os.Args) - 1; i >= 0; i-- {
if os.Args[i] == "norecover" {
if i == len(os.Args)-1 {
os.Args = os.Args[:i]
} else {
os.Args = append(os.Args[:i], os.Args[i+1:]...)
}
return true
}
}
return false
}()
)

func main() {
gocq.PrepareData()

switch {
case !nofork:
os.Args = append(os.Args, "nofork")
logrus.Infoln("主进程已启动, pid:", os.Getpid())
c := 16
for c > 0 {
err := runChild()
logrus.Errorln("子进程退出, 重启中:", err)
time.Sleep(time.Second)
if _, err = os.Stat(os.Args[0]); err != nil && (os.Args[0][0] == '.' || os.Args[0][0] == '/' || os.Args[0][1] == ':') {
logrus.Errorln("可执行文件被删除, 将使用自身进程作恢复处理, 如再崩溃则无法二次恢复")
break
}
c--
}
logrus.Errorln("到达重启次数上限, 将使用自身进程作恢复处理, 如再崩溃则无法二次恢复")
case !norecover:
defer func() {
logrus.Errorln("子进程退出:", recover())
os.Exit(1)
}()
}

logrus.Infoln("子进程已启动, pid:", os.Getpid())

gocq.LoginInteract()
_ = terminal.DisableQuickEdit()
_ = terminal.EnableVT100()
gocq.WaitSignal()
_ = terminal.RestoreInputMode()
}

func runChild() error {
cmd := exec.Command(os.Args[0], os.Args[1:]...)
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
if err != nil {
cmd = exec.Command("./"+os.Args[0], os.Args[1:]...)
err = cmd.Start()
}
if err != nil {
panic(err)
}
return cmd.Wait()
}

0 comments on commit 5e7e43f

Please sign in to comment.