mirror of
https://git.sr.ht/~ashkeel/strimertul
synced 2024-09-18 01:50:50 +00:00
refactor: better signature/name for DB subscription functions
This commit is contained in:
parent
fd9fe4c3bc
commit
674611f0d1
7 changed files with 18 additions and 18 deletions
|
@ -82,7 +82,7 @@ func (mod *DBModule) PutKey(key string, data string) error {
|
|||
return err
|
||||
}
|
||||
|
||||
func (mod *DBModule) Subscribe(fn kv.SubscriptionCallback, prefixes ...string) error {
|
||||
func (mod *DBModule) SubscribePrefix(fn kv.SubscriptionCallback, prefixes ...string) error {
|
||||
for _, prefix := range prefixes {
|
||||
_, err := mod.makeRequest(kv.CmdSubscribePrefix, map[string]interface{}{"prefix": prefix})
|
||||
if err != nil {
|
||||
|
@ -93,7 +93,7 @@ func (mod *DBModule) Subscribe(fn kv.SubscriptionCallback, prefixes ...string) e
|
|||
return nil
|
||||
}
|
||||
|
||||
func (mod *DBModule) SubscribeKey(fn func(string), key string) error {
|
||||
func (mod *DBModule) SubscribeKey(key string, fn func(string)) error {
|
||||
_, err := mod.makeRequest(kv.CmdSubscribePrefix, map[string]interface{}{"prefix": key})
|
||||
if err != nil {
|
||||
return err
|
||||
|
|
|
@ -134,7 +134,7 @@ func (s *Server) Listen() error {
|
|||
restart := containers.NewRWSync(false)
|
||||
exit := make(chan error)
|
||||
go func() {
|
||||
err := s.db.SubscribeKey(func(value string) {
|
||||
err := s.db.SubscribeKey(ServerConfigKey, func(value string) {
|
||||
oldBind := s.Config.Bind
|
||||
oldPassword := s.Config.KVPassword
|
||||
err := json.Unmarshal([]byte(value), &s.Config)
|
||||
|
@ -158,7 +158,7 @@ func (s *Server) Listen() error {
|
|||
return
|
||||
}
|
||||
}
|
||||
}, ServerConfigKey)
|
||||
})
|
||||
if err != nil {
|
||||
exit <- fmt.Errorf("error while handling subscription to HTTP config changes: %w", err)
|
||||
}
|
||||
|
|
|
@ -91,8 +91,8 @@ func Register(manager *modules.Manager) error {
|
|||
loyalty.points[k] = entry
|
||||
}
|
||||
|
||||
// Subscribe for changes
|
||||
err = db.Subscribe(loyalty.update, "loyalty/")
|
||||
// SubscribePrefix for changes
|
||||
err = db.SubscribePrefix(loyalty.update, "loyalty/")
|
||||
if err != nil {
|
||||
logger.Error("could not setup loyalty reload subscription", zap.Error(err))
|
||||
}
|
||||
|
|
|
@ -154,11 +154,11 @@ func NewBot(api *Client, config BotConfig) *Bot {
|
|||
if err != nil {
|
||||
bot.logger.Error("failed to parse custom commands", zap.Error(err))
|
||||
}
|
||||
err = api.db.SubscribeKey(bot.updateCommands, CustomCommandsKey)
|
||||
err = api.db.SubscribeKey(CustomCommandsKey, bot.updateCommands)
|
||||
if err != nil {
|
||||
bot.logger.Error("could not set-up bot command reload subscription", zap.Error(err))
|
||||
}
|
||||
err = api.db.SubscribeKey(bot.handleWriteMessageRPC, WriteMessageRPC)
|
||||
err = api.db.SubscribeKey(WriteMessageRPC, bot.handleWriteMessageRPC)
|
||||
if err != nil {
|
||||
bot.logger.Error("could not set-up bot command reload subscription", zap.Error(err))
|
||||
}
|
||||
|
|
|
@ -66,7 +66,7 @@ func Register(manager *modules.Manager) error {
|
|||
}
|
||||
|
||||
// Listen for config changes
|
||||
err = db.SubscribeKey(func(value string) {
|
||||
err = db.SubscribeKey(ConfigKey, func(value string) {
|
||||
err := json.UnmarshalFromString(value, &config)
|
||||
if err != nil {
|
||||
logger.Error("failed to unmarshal config", zap.Error(err))
|
||||
|
@ -80,12 +80,12 @@ func Register(manager *modules.Manager) error {
|
|||
client.API = api
|
||||
|
||||
logger.Info("reloaded/updated Twitch API")
|
||||
}, ConfigKey)
|
||||
})
|
||||
if err != nil {
|
||||
client.logger.Error("could not setup twitch config reload subscription", zap.Error(err))
|
||||
}
|
||||
|
||||
err = db.SubscribeKey(func(value string) {
|
||||
err = db.SubscribeKey(BotConfigKey, func(value string) {
|
||||
var twitchBotConfig BotConfig
|
||||
err := json.UnmarshalFromString(value, &twitchBotConfig)
|
||||
if err != nil {
|
||||
|
@ -105,7 +105,7 @@ 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))
|
||||
}
|
||||
|
|
|
@ -111,7 +111,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
|||
|
||||
mod.compileTemplates()
|
||||
|
||||
err = bot.api.db.SubscribeKey(func(value string) {
|
||||
err = bot.api.db.SubscribeKey(BotAlertsKey, func(value string) {
|
||||
err := json.UnmarshalFromString(value, &mod.Config)
|
||||
if err != nil {
|
||||
bot.logger.Debug("error reloading timer config", zap.Error(err))
|
||||
|
@ -119,7 +119,7 @@ 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))
|
||||
}
|
||||
|
@ -235,7 +235,7 @@ func SetupAlerts(bot *Bot) *BotAlertsModule {
|
|||
}
|
||||
}
|
||||
|
||||
err = bot.api.db.SubscribeKey(func(value string) {
|
||||
err = bot.api.db.SubscribeKey(EventSubEventKey, func(value string) {
|
||||
var ev eventSubNotification
|
||||
err := json.UnmarshalFromString(value, &ev)
|
||||
if err != nil {
|
||||
|
@ -416,7 +416,7 @@ 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))
|
||||
}
|
||||
|
|
|
@ -55,14 +55,14 @@ func SetupTimers(bot *Bot) *BotTimerModule {
|
|||
bot.api.db.PutJSON(BotTimersKey, mod.Config)
|
||||
}
|
||||
|
||||
err = bot.api.db.SubscribeKey(func(value string) {
|
||||
err = bot.api.db.SubscribeKey(BotTimersKey, 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))
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue