2022-10-07 17:10:22 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2024-01-01 16:52:18 +00:00
|
|
|
"context"
|
2022-10-07 17:10:22 +00:00
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
func updateConfigCron() string {
|
2024-06-09 10:44:41 +00:00
|
|
|
minute := rand.Intn(60) //nolint:mnd,gosec // Only used to distribute load
|
2023-01-01 14:33:51 +00:00
|
|
|
return fmt.Sprintf("0 %d * * * *", minute)
|
2022-10-07 17:10:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func updateConfigFromRemote() {
|
2023-01-01 14:33:51 +00:00
|
|
|
log.Debug("starting remote rule update")
|
2022-10-07 17:10:22 +00:00
|
|
|
err := patchConfig(
|
|
|
|
cfg.Config,
|
|
|
|
"Remote Update", "twitch-bot@luzifer.io",
|
|
|
|
"update rules from subscription URLs",
|
|
|
|
func(cfg *configFile) error {
|
2023-01-01 14:33:51 +00:00
|
|
|
var updates int
|
2022-10-07 17:10:22 +00:00
|
|
|
|
|
|
|
for _, r := range cfg.Rules {
|
|
|
|
logger := log.WithField("rule", r.MatcherID())
|
|
|
|
|
2024-01-01 16:52:18 +00:00
|
|
|
rhu, err := r.UpdateFromSubscription(context.Background())
|
2022-10-07 17:10:22 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Error("updating rule")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if rhu {
|
2023-01-01 14:33:51 +00:00
|
|
|
updates++
|
2022-10-07 17:10:22 +00:00
|
|
|
logger.Info("updated rule from remote URL")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-01 14:33:51 +00:00
|
|
|
log.WithField("updates", updates).Debug("remote rule update finished")
|
|
|
|
if updates == 0 {
|
2022-10-07 17:10:22 +00:00
|
|
|
return errSaveNotRequired
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("updating config rules from subscriptions")
|
|
|
|
}
|
|
|
|
}
|