forked from FloatTech/gocqzbp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
87 lines (79 loc) · 1.89 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package main
import (
"os"
"os/exec"
"time"
"github.com/sirupsen/logrus"
"github.com/Mrs4s/go-cqhttp/cmd/gocq"
_ "github.com/Mrs4s/go-cqhttp/db/leveldb" // leveldb
_ "github.com/Mrs4s/go-cqhttp/modules/mime" // mime检查模块
_ "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.InitLog()
switch {
case !nofork:
os.Args = append(os.Args, "nofork")
logrus.Infoln("主进程已启动, pid:", os.Getpid())
for {
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
}
}
case !norecover:
defer func() {
logrus.Errorln("子进程退出:", recover())
os.Exit(1)
}()
}
logrus.Infoln("子进程已启动, pid:", os.Getpid())
gocq.InitCache()
gocq.InitDB()
gocq.PrintBanner()
gocq.LoadDevice()
gocq.CheckKey(gocq.ParseCommand())
gocq.Main()
}
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 {
panic(err)
}
return cmd.Wait()
}