This repository has been archived by the owner on Jun 3, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathrun.go
81 lines (71 loc) · 1.58 KB
/
run.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
package bobo
import (
"fmt"
"os"
"strconv"
"time"
"github.com/jpillora/overseer"
"github.com/eure/bobo/errorcode"
)
// Run is entry-point of bot daemon.
func Run(opt RunOption) {
switch {
case opt.UseUpgrade:
runWithUpgrading(opt)
default:
run(opt)
}
}
// run is entry-point of bot daemon.
func run(opt RunOption) {
logger := opt.Logger
logger.Infof("bobo", "pid=[%d]", os.Getpid())
for {
errCode := runBot(opt)
logger.Errorf("bobo", "errCode=[%d]", errCode)
if errCode != errorcode.None {
os.Exit(errCode)
}
time.Sleep(time.Second * 5)
logger.Infof("bobo", "run again...")
}
}
// runWithUpgrading is entry-point of bot daemon with self-upgrading binary.
// ref: https://github.com/jpillora/overseer
func runWithUpgrading(opt RunOption) {
overseer.Run(overseer.Config{
Program: func(state overseer.State) {
run(opt)
},
Fetcher: opt.UpgradeFetcher,
Debug: opt.UpgradeDebug,
})
}
func runBot(opt RunOption) (errCode int) {
bot, err := NewBotWithConfig(Config{
Engine: opt.Engine,
CommandSet: opt.CommandSet,
Logger: opt.Logger,
HTTPClient: opt.HTTPClient,
MaxRunningCommands: opt.MaxRunningCommands,
})
if err != nil {
panic(err)
}
if opt.NoPanic {
defer func() {
if err := recover(); err != nil {
bot.LogInfo("PANIC", fmt.Sprintf("%v", err))
bot.exit()
errCode = errorcode.None
}
}()
}
return bot.Run()
}
// IsDebug checks debug flag is true or not.
// debug flag is set via envvar "BOBO_DEBUG".
func IsDebug() bool {
b, _ := strconv.ParseBool(os.Getenv("BOBO_DEBUG"))
return b
}