1
0
Fork 0
mirror of https://git.sr.ht/~ashkeel/strimertul synced 2024-09-18 01:50:50 +00:00

refactor: single key subscription function

This commit is contained in:
Ash Keel 2022-11-25 14:16:20 +01:00
parent 4fdcb8f2ba
commit a15393be70
No known key found for this signature in database
GPG key ID: BAD8D93E7314ED3E
7 changed files with 300 additions and 277 deletions

View file

@ -93,6 +93,20 @@ func (mod *DBModule) Subscribe(fn kv.SubscriptionCallback, prefixes ...string) e
return nil
}
func (mod *DBModule) SubscribeKey(fn func(string), key string) error {
_, err := mod.makeRequest(kv.CmdSubscribePrefix, map[string]interface{}{"prefix": key})
if err != nil {
return err
}
go mod.client.SetPrefixSubCallback(key, func(changedKey string, value string) {
if key != changedKey {
return
}
fn(value)
})
return nil
}
func (mod *DBModule) GetJSON(key string, dst interface{}) error {
res, err := mod.GetKey(key)
if err != nil {

View file

@ -134,8 +134,7 @@ func (s *Server) Listen() error {
restart := containers.NewRWSync(false)
exit := make(chan error)
go func() {
err := s.db.Subscribe(func(key, value string) {
if key == ServerConfigKey {
err := s.db.SubscribeKey(func(value string) {
oldBind := s.Config.Bind
oldPassword := s.Config.KVPassword
err := json.Unmarshal([]byte(value), &s.Config)
@ -159,7 +158,6 @@ func (s *Server) Listen() error {
return
}
}
}
}, ServerConfigKey)
if err != nil {
exit <- fmt.Errorf("error while handling subscription to HTTP config changes: %w", err)

View file

@ -92,7 +92,10 @@ func Register(manager *modules.Manager) error {
}
// Subscribe for changes
go db.Subscribe(loyalty.update, "loyalty/")
err = db.Subscribe(loyalty.update, "loyalty/")
if err != nil {
logger.Error("could not setup loyalty reload subscription", zap.Error(err))
}
// Register module
manager.Modules[modules.ModuleLoyalty] = loyalty

View file

@ -154,15 +154,19 @@ func NewBot(api *Client, config BotConfig) *Bot {
if err != nil {
bot.logger.Error("failed to parse custom commands", zap.Error(err))
}
go api.db.Subscribe(bot.updateCommands, CustomCommandsKey)
go api.db.Subscribe(bot.handleWriteMessageRPC, WriteMessageRPC)
err = api.db.SubscribeKey(bot.updateCommands, CustomCommandsKey)
if err != nil {
bot.logger.Error("could not set-up bot command reload subscription", zap.Error(err))
}
err = api.db.SubscribeKey(bot.handleWriteMessageRPC, WriteMessageRPC)
if err != nil {
bot.logger.Error("could not set-up bot command reload subscription", zap.Error(err))
}
return bot
}
func (b *Bot) updateCommands(key, value string) {
switch key {
case CustomCommandsKey:
func (b *Bot) updateCommands(value string) {
err := func() error {
b.mu.Lock()
defer b.mu.Unlock()
@ -178,14 +182,10 @@ func (b *Bot) updateCommands(key, value string) {
return
}
}
}
func (b *Bot) handleWriteMessageRPC(key, value string) {
switch key {
case WriteMessageRPC:
func (b *Bot) handleWriteMessageRPC(value string) {
b.Client.Say(b.config.Channel, value)
}
}
func (b *Bot) updateTemplates() error {
for cmd, tmpl := range b.customCommands {

View file

@ -66,9 +66,7 @@ func Register(manager *modules.Manager) error {
}
// Listen for config changes
go db.Subscribe(func(key, value string) {
switch key {
case ConfigKey:
err = db.SubscribeKey(func(value string) {
err := json.UnmarshalFromString(value, &config)
if err != nil {
logger.Error("failed to unmarshal config", zap.Error(err))
@ -82,7 +80,12 @@ func Register(manager *modules.Manager) error {
client.API = api
logger.Info("reloaded/updated Twitch API")
case BotConfigKey:
}, ConfigKey)
if err != nil {
client.logger.Error("could not setup twitch config reload subscription", zap.Error(err))
}
err = db.SubscribeKey(func(value string) {
var twitchBotConfig BotConfig
err := json.UnmarshalFromString(value, &twitchBotConfig)
if err != nil {
@ -102,8 +105,10 @@ func Register(manager *modules.Manager) error {
}
client.restart <- true
logger.Info("reloaded/restarted Twitch bot")
}, BotConfigKey)
if err != nil {
client.logger.Error("could not setup twitch bot config reload subscription", zap.Error(err))
}
}, ConfigKey, BotConfigKey)
if config.Enabled {
client.API, err = client.getHelixAPI()

View file

@ -111,8 +111,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
mod.compileTemplates()
go bot.api.db.Subscribe(func(key, value string) {
if key == BotAlertsKey {
err = bot.api.db.SubscribeKey(func(value string) {
err := json.UnmarshalFromString(value, &mod.Config)
if err != nil {
bot.logger.Debug("error reloading timer config", zap.Error(err))
@ -120,8 +119,10 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
bot.logger.Info("reloaded alert config")
}
mod.compileTemplates()
}
}, BotAlertsKey)
if err != nil {
bot.logger.Error("could not set-up bot alert reload subscription", zap.Error(err))
}
// Subscriptions are handled with a slight delay as info come from different events and must be aggregated
pendingSubs := make(map[string]subMixedEvent)
@ -234,8 +235,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
}
}
go bot.api.db.Subscribe(func(key, value string) {
if key == EventSubEventKey {
err = bot.api.db.SubscribeKey(func(value string) {
var ev eventSubNotification
err := json.UnmarshalFromString(value, &ev)
if err != nil {
@ -416,8 +416,10 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
// Compile template and send
writeTemplate(bot, tpl, &giftEv)
}
}
}, EventSubEventKey)
if err != nil {
bot.logger.Error("could not setup twitch alert subscription", zap.Error(err))
}
bot.logger.Debug("loaded bot alerts")

View file

@ -55,16 +55,17 @@ func SetupTimers(bot *Bot) *BotTimerModule {
bot.api.db.PutJSON(BotTimersKey, mod.Config)
}
go bot.api.db.Subscribe(func(key, value string) {
if key == BotTimersKey {
err = bot.api.db.SubscribeKey(func(value string) {
err := json.UnmarshalFromString(value, &mod.Config)
if err != nil {
bot.logger.Debug("error reloading timer config", zap.Error(err))
} else {
bot.logger.Info("reloaded timer config")
}
}
}, BotTimersKey)
if err != nil {
bot.logger.Error("could not set-up timer reload subscription", zap.Error(err))
}
bot.logger.Debug("loaded timers", zap.Int("timers", len(mod.Config.Timers)))