I think things are pretty good

This commit is contained in:
Brian Buller 2023-12-01 10:57:15 -06:00
parent ff58d1ddf4
commit 0fc78d4b4c
8 changed files with 65 additions and 69 deletions

View File

@ -2,12 +2,8 @@
helperbot:
go build -o build/helperbot *.go
plugins:
./buildplugins.sh
package:
go build -o build/helperbot *.go
./buildplugins.sh
cp helperbot.service build/
cd build && tar -zcvf helperbot.tgz *

View File

@ -10,7 +10,6 @@ import (
type App struct {
debug bool
Running bool
m *models.BotModel
@ -28,7 +27,6 @@ func NewApp(debugMode bool) (*App, error) {
if err != nil {
return nil, err
}
a.Running = true
go a.watchMessageChannel()
return a, err
}
@ -54,6 +52,7 @@ func (a *App) initialize() error {
if a.m, err = models.NewBotModel(a.debug); err != nil {
return err
}
a.m.Running = true
// Load up the plugins
a.plugins = append(a.plugins, &plugins.StatsState{}, &plugins.AoCState{})
@ -69,6 +68,9 @@ func (a *App) initialize() error {
return err
}
func (a *App) Stop() { a.m.Running = false }
func (a *App) IsRunning() bool { return a.m.Running }
func (a *App) SendQuitMessage() { a.m.SendMessage(models.QuitMessage) }
func (a *App) setupMessageWatchers() {
@ -76,7 +78,7 @@ func (a *App) setupMessageWatchers() {
if msg.Is(models.QuitMessage) {
// App is shutting down
fmt.Println("Received QuitMessage")
a.Running = false
a.m.Running = false
} else if msg.IsError() {
// Received an Error Message
fmt.Printf("ERROR: %s\n", msg)
@ -89,14 +91,13 @@ func (a *App) setupMessageWatchers() {
})
for _, v := range a.plugins {
fmt.Println("Setting up watchers for", v.Name())
a.m.AddMessageWatcher(fmt.Sprintf("plugin-%s", v.Name()), v.ProcessMessage)
a.m.AddRTMWatcher(fmt.Sprintf("plugin-%s", v.Name()), v.ProcessRTMEvent)
}
}
func (a *App) watchMessageChannel() {
for a.Running {
for a.m.Running {
a.m.ProcessMessageChannel()
}
}

View File

@ -1,14 +0,0 @@
#!/bin/bash
mkdir -p build/plugins
cd plugins_src
for i in `ls`; do
echo "Building plugin: $i"
cd "$i"
go build -buildmode=plugin "$i.go"
if [ $? -eq 0 ]; then
mv *.so ../../build/plugins
fi
cd ..
done
cd ..

View File

@ -7,4 +7,3 @@ rmE build/aoc-util
rmE build/helperbot
rmE build/helperbot.service
rmE build/helperbot.tgz
rm build/plugins/*

View File

@ -36,9 +36,9 @@ var rootCmd = &cobra.Command{
// Save the changes when the app quits
fmt.Println("\nFinishing up...")
a.SendQuitMessage()
a.Running = false
a.Stop()
}()
for a.Running {
for a.IsRunning() {
time.Sleep(time.Second * 1)
}
fmt.Println("Done")

View File

@ -24,6 +24,8 @@ type BotModel struct {
messageWatchers map[string]func(msg BotMessage) bool
rtmWatchers map[string]func(event *slack.RTMEvent) bool
Running bool
}
func NewBotModel(debug bool) (*BotModel, error) {
@ -60,7 +62,9 @@ func (m *BotModel) DebugRTMWatcher(event *slack.RTMEvent) bool {
}
func (m *BotModel) SendMessage(msg BotMessage) {
if m.debug {
fmt.Println("Sending:", msg)
}
m.messages <- msg
}
@ -72,11 +76,8 @@ func (m *BotModel) AddRTMWatcher(name string, watcher func(event *slack.RTMEvent
}
func (m *BotModel) ProcessMessageChannel() {
fmt.Println(">> ProcessMessageChannel")
msg := <-m.messages
fmt.Println(">>>> Received Message", msg)
for k, v := range m.messageWatchers {
fmt.Printf(">>>> Message Watcher [%s]\n", k)
for _, v := range m.messageWatchers {
// Pass the message to the watcher
if v != nil && v(msg) {
// if a watcher returns true, the message was consumed
@ -85,11 +86,8 @@ func (m *BotModel) ProcessMessageChannel() {
}
}
func (m *BotModel) ProcessRTMChannel() {
fmt.Println(">> ProcessRTMChannel")
msg := <-m.otherRTMEvents
fmt.Println(">>>> Received Message", msg)
for k, v := range m.rtmWatchers {
fmt.Printf(">>>> RTM Watcher [%s]\n", k)
for _, v := range m.rtmWatchers {
// Pass the event to the watcher
if v(msg) {
// if a watcher returns true, the message was consumed

View File

@ -1,9 +1,7 @@
#!/bin/bash
rm helperbot.tgz
./buildplugins.sh
cd cmd
go build -o helperbot
mv plugins build/
#tar -zcvf ../helperbot.tgz helperbot helperbot.service plugins
#cd ..
tar -zcvf ../helperbot.tgz helperbot helperbot.service plugins
cd ..

View File

@ -21,6 +21,7 @@ type AoCState struct {
boardId string
sessionCookie string
sessionNeedsUpdate bool
lastUpdate time.Time
aoc *aoc.AoC
lastYear int
@ -112,26 +113,16 @@ func (s *AoCState) runLoop() {
if err != nil {
return
}
// Don't do anything until we've done an initial update of all earlier years
for _, yr := range s.GetListOfAoCYears() {
if yr == s.GetLatestYear() {
continue
}
if !s.sessionNeedsUpdate {
fmt.Printf("Startup: Checking if board needs update (%d)\n", yr)
s.AoCSilentBoardCheckAndUpdate(yr)
}
}
for {
for s.model.Running {
_, err := s.getChannelId()
// This plugin fails without a channel id
if err != nil {
return
}
for _, yr := range s.GetListOfAoCYears() {
if !s.sessionNeedsUpdate {
if s.GetLatestYear() != s.lastYear {
// Latest year changed. Grab that board first.
// Latest year changed. Update it.
s.lastYear = s.GetLatestYear()
admin := s.model.GetSlackAdminDMId()
if admin == "" {
@ -139,14 +130,11 @@ func (s *AoCState) runLoop() {
return
}
s.SendSlackMessage(fmt.Sprintf(":christmas_tree: AoC Set latest leaderboard to %d", s.lastYear), admin)
}
s.AoCBoardCheckAndUpdate(s.lastYear)
time.Sleep(time.Minute)
s.lastUpdate = time.Now()
}
s.AoCBoardCheckAndUpdate(yr)
time.Sleep(time.Minute)
}
}
time.Sleep(time.Minute)
time.Sleep(time.Minute * 15)
}
s.model.SendMessage(models.NewBotMessage(s.Name(), models.MsgSrcApp, "status", "done", ""))
@ -186,6 +174,9 @@ func (s *AoCState) ProcessAdminDirectMessage(m models.BotMessage) bool {
case "session":
s.DoSessionCmd(m)
return true
case "status":
s.DoStatusCmd(m)
return true
}
return false
}
@ -204,7 +195,11 @@ func (s *AoCState) ProcessChannelMessage(m models.BotMessage) bool {
func (s *AoCState) DoHelpCmd(m models.BotMessage) bool {
txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n",
"-- WiP --",
"• help - Print this message\n",
"• ping - PONG! Check if the service is running\n",
"• top [year] - Print the top 5 users for the passed year\n",
" If no year is passed, the current year is used\n",
" Passing 'all' considers all years\n",
)
s.SendSlackMessage(txt, m.Target)
return true
@ -212,7 +207,13 @@ func (s *AoCState) DoHelpCmd(m models.BotMessage) bool {
func (s *AoCState) DoHelpAdminCmd(m models.BotMessage) bool {
txt := fmt.Sprint(":christmas_tree: AoC Help :christmas_tree:\n",
"-- WiP --",
"• help - Print this message\n",
"• ping - PONG! Check if the service is running\n",
"• top [year] - Print the top 5 users for the passed year\n",
" If no year is passed, the current year is used\n",
" Passing 'all' considers all years\n",
"• session [token] - Print or set the session cookie\n",
"• status - Print the status of the service\n",
)
s.SendSlackMessage(txt, m.Target)
return true
@ -250,6 +251,23 @@ func (s *AoCState) DoSessionCmd(m models.BotMessage) bool {
return true
}
func (s *AoCState) DoStatusCmd(m models.BotMessage) bool {
admin := s.model.GetSlackAdminDMId()
if admin == "" {
s.SendAdminIdError()
return true
}
txt := ":christmas_tree: Advent of Code Status :christmas_tree:"
txt = fmt.Sprintf(
"%s\nLast Update: %s\nNext Update In: %s", txt,
s.lastUpdate.Format(time.RFC3339),
s.lastUpdate.Add(time.Minute*15).Sub(time.Now()).Round(time.Second),
)
s.SendSlackMessage(txt, m.Target)
return false
}
func (s *AoCState) DoTopCmd(m models.BotMessage) bool {
msgPts := strings.Fields(m.Text)
var err error