From b48165fa0a79d624e8861b6557d39372bd79ffd5 Mon Sep 17 00:00:00 2001 From: sneak Date: Tue, 8 Sep 2020 19:57:28 -0700 Subject: [PATCH] add better startup and shutdown msgs --- Dockerfile | 5 ++--- Makefile | 2 ++ bot/bot.go | 25 ++++++++++++++++--------- cmd/sco/main.go | 2 ++ 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/Dockerfile b/Dockerfile index 327d24c..61fcbb6 100644 --- a/Dockerfile +++ b/Dockerfile @@ -5,12 +5,11 @@ ADD . /build/ WORKDIR /build RUN make build -# temp -RUN find / FROM scratch COPY --from=builder /build/sco /app/main -COPY --from=builder /build /archive/build +COPY --from=builder /go /goarchive + WORKDIR /app CMD ["./main"] diff --git a/Makefile b/Makefile index 73332f7..977cde2 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ VERSION := $(shell git rev-parse --short HEAD) +COMMIT := $(shell git log --pretty=format:"%H" -1) BUILDTIME := $(shell date -u '+%Y-%m-%dT%H:%M:%SZ') BUILDTIMEFILENAME := $(shell date -u '+%Y%m%d-%H%M%SZ') BUILDTIMETAG := $(shell date -u '+%Y%m%d%H%M%S') @@ -12,6 +13,7 @@ IMAGENAME := sneak/$(FN) UNAME_S := $(shell uname -s) GOLDFLAGS += -X main.Version=$(VERSION) +GOLDFLAGS += -X main.Commit=$(COMMIT) GOLDFLAGS += -X main.Buildarch=$(BUILDARCH) # osx can't statically link apparently?! diff --git a/bot/bot.go b/bot/bot.go index 4b65aee..0c3504e 100644 --- a/bot/bot.go +++ b/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) } }() diff --git a/cmd/sco/main.go b/cmd/sco/main.go index a91e192..3837b07 100644 --- a/cmd/sco/main.go +++ b/cmd/sco/main.go @@ -4,12 +4,14 @@ import "os" import "git.eeqj.de/sneak/sco/bot" var Version string +var Commit string var Buildarch string func main() { mybot := bot.New(func(b *bot.Bot) { b.BotName = "lsvsco" b.Version = Version + b.Commit = Commit b.Buildarch = Buildarch b.APIURL = os.Getenv("SCO_API_URL") b.TeamName = os.Getenv("SCO_TEAM_NAME")