add better startup and shutdown msgs
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
25
bot/bot.go
25
bot/bot.go
@@ -24,6 +24,7 @@ type Bot struct {
|
||||
DebuggingChannelName string
|
||||
TeamName string
|
||||
Version string
|
||||
Commit string
|
||||
WebsocketURL string
|
||||
StartupUnixTime int64
|
||||
client *model.Client4
|
||||
@@ -70,7 +71,8 @@ func (b *Bot) Main() int {
|
||||
|
||||
// Lets create a bot channel for logging debug messages into
|
||||
b.CreateBotDebuggingChannelIfNeeded()
|
||||
b.SendMsgToDebuggingChannel("_"+b.BotName+" has **started** running_", "")
|
||||
msg := fmt.Sprintf("_**%s** has started up_\n\nrunning on version %s", b.BotName, b.Version)
|
||||
b.SendMsgToDebuggingChannel(msg, "")
|
||||
|
||||
// Lets start listening to some channels via the websocket!
|
||||
var err *model.AppError
|
||||
@@ -223,6 +225,10 @@ func (b *Bot) HandleWebSocketResponse(event *model.WebSocketEvent) {
|
||||
|
||||
}
|
||||
|
||||
func (b *Bot) Shutdown() {
|
||||
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
|
||||
}
|
||||
|
||||
func (b *Bot) HandleMsgFromChannel(event *model.WebSocketEvent) {
|
||||
|
||||
post := model.PostFromJson(strings.NewReader(event.Data["post"].(string)))
|
||||
@@ -238,14 +244,13 @@ func (b *Bot) HandleMsgFromChannel(event *model.WebSocketEvent) {
|
||||
}
|
||||
|
||||
if matched, _ := regexp.MatchString(`(?:^|\W)version(?:$|\W)`, post.Message); matched {
|
||||
msg := fmt.Sprintf("I am running version `%s` from git: https://git.eeqj.de/sneak/sco", b.Version)
|
||||
msg := fmt.Sprintf("I am running version `%s` from git: https://git.eeqj.de/sneak/sco/src/commit/%s", b.Version, b.Commit)
|
||||
b.SendMsgToChannel(msg, post.Id, post.ChannelId)
|
||||
return
|
||||
}
|
||||
|
||||
if matched, _ := regexp.MatchString(`(?:^|\W)uptime(?:$|\W)`, post.Message); matched {
|
||||
uptime := time.Now().Unix() - b.StartupUnixTime
|
||||
msg := fmt.Sprintf("running: uptime %d seconds", uptime)
|
||||
msg := fmt.Sprintf("running: uptime %d seconds", b.Uptime())
|
||||
b.SendMsgToChannel(msg, post.Id, post.ChannelId)
|
||||
return
|
||||
}
|
||||
@@ -254,6 +259,10 @@ func (b *Bot) HandleMsgFromChannel(event *model.WebSocketEvent) {
|
||||
|
||||
}
|
||||
|
||||
func (b *Bot) Uptime() int64 {
|
||||
return (time.Now().Unix() - b.StartupUnixTime)
|
||||
}
|
||||
|
||||
func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
|
||||
println("responding to debugging channel msg")
|
||||
|
||||
@@ -262,8 +271,7 @@ func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
|
||||
|
||||
// ignore my events
|
||||
if matched, _ := regexp.MatchString(`(?:^|\W)shutdown(?:$|\W)`, post.Message); matched {
|
||||
b.SendMsgToDebuggingChannel("i will now exit", post.Id)
|
||||
syscall.Kill(syscall.Getpid(), syscall.SIGINT)
|
||||
b.Shutdown()
|
||||
return
|
||||
}
|
||||
|
||||
@@ -293,7 +301,6 @@ func (b *Bot) HandleMsgFromDebuggingChannel(event *model.WebSocketEvent) {
|
||||
}
|
||||
|
||||
b.SendMsgToChannel("I did not understand your command, sorry", post.Id, post.ChannelId)
|
||||
//b.SendMsgToDebuggingChannel("I did not understand you!", post.Id)
|
||||
}
|
||||
|
||||
func PrintError(err *model.AppError) {
|
||||
@@ -308,11 +315,11 @@ func (b *Bot) SetupGracefulShutdown() {
|
||||
signal.Notify(c, os.Interrupt)
|
||||
go func() {
|
||||
for _ = range c {
|
||||
msg := fmt.Sprintf("_**%s** (version `%s`) is now shutting down_\n\nuptime was %d secs", b.BotName, b.Version, b.Uptime())
|
||||
b.SendMsgToDebuggingChannel(msg, "")
|
||||
if b.webSocketClient != nil {
|
||||
b.webSocketClient.Close()
|
||||
}
|
||||
|
||||
b.SendMsgToDebuggingChannel("_"+b.BotName+" has **stopped** running_", "")
|
||||
os.Exit(0)
|
||||
}
|
||||
}()
|
||||
|
||||
Reference in New Issue
Block a user