bot: Bot setup and teardown done: Connect w/retry, join/leave, full config, and logging.

This commit is contained in:
2018-11-14 21:01:29 +01:00
parent 112c29694d
commit 7336ea2b47
9 changed files with 236 additions and 76 deletions
+24 -1
View File
@@ -2,13 +2,16 @@ package channels
import (
"context"
"log"
"time"
"git.aiterp.net/rpdata/logbot3/internal/models"
"git.aiterp.net/rpdata/logbot3/internal/models/changes"
)
// SubscribeLogged returns a channel that gets all the channel changes.
func SubscribeLogged(ctx context.Context) (<-chan models.Channel, error) {
earliest := time.Now()
channels, err := ListLogged(ctx)
if err != nil {
return nil, err
@@ -21,8 +24,28 @@ func SubscribeLogged(ctx context.Context) (<-chan models.Channel, error) {
go func() {
for {
time.Sleep(time.Second * 20)
time.Sleep(time.Second * 5)
// Get the changes.
changes, err := changes.List(ctx, &changes.Filter{EarliestDate: &earliest, Keys: []models.ChangeKey{{Model: "Channel", ID: "*"}}})
if ctx.Err() != nil {
return
} else if err != nil {
log.Println("Failed to get chnges:", err)
continue
}
// Group changes to the same model.
for _, change := range changes {
for _, obj := range change.Objects {
channel, err := obj.Channel()
if err != nil {
continue
}
output <- channel
}
}
}
}()