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

Tentative fix for mutex killing strimertul

This commit is contained in:
Ash Keel 2022-02-08 15:13:13 +01:00
parent 8ba3c3fbc0
commit f3f85622bd
No known key found for this signature in database
GPG key ID: BAD8D93E7314ED3E
2 changed files with 62 additions and 57 deletions

View file

@ -117,7 +117,8 @@ func Register(manager *modules.Manager) error {
} }
func (m *Manager) Status() modules.ModuleStatus { func (m *Manager) Status() modules.ModuleStatus {
if !m.config.Enabled { config := m.Config()
if !config.Enabled {
return modules.ModuleStatus{ return modules.ModuleStatus{
Enabled: false, Enabled: false,
} }

View file

@ -15,8 +15,8 @@ import (
) )
func (b *Bot) SetupLoyalty(loyalty *loyalty.Manager) { func (b *Bot) SetupLoyalty(loyalty *loyalty.Manager) {
config := loyalty.Config()
b.Loyalty = loyalty b.Loyalty = loyalty
config := loyalty.Config()
b.SetBanList(config.BanList) b.SetBanList(config.BanList)
// Add loyalty-based commands // Add loyalty-based commands
@ -55,79 +55,83 @@ func (b *Bot) SetupLoyalty(loyalty *loyalty.Manager) {
var statusMux sync.Mutex var statusMux sync.Mutex
streamOnline := true streamOnline := true
go func() { go func() {
defer statusMux.Unlock()
for { for {
// Wait for next poll // Wait for next poll
time.Sleep(60 * time.Second) time.Sleep(60 * time.Second)
// Check if streamer is online, if possible // Check if streamer is online, if possible
statusMux.Lock() func() {
streamOnline = true statusMux.Lock()
status, err := b.api.API.GetStreams(&helix.StreamsParams{ defer statusMux.Unlock()
UserLogins: []string{b.config.Channel}, streamOnline = true
}) status, err := b.api.API.GetStreams(&helix.StreamsParams{
if err != nil { UserLogins: []string{b.config.Channel},
b.logger.Error("Error checking stream status", zap.Error(err)) })
} else { if err != nil {
streamOnline = len(status.Data.Streams) > 0 b.logger.Error("Error checking stream status", zap.Error(err))
} } else {
statusMux.Unlock() streamOnline = len(status.Data.Streams) > 0
}
err = b.api.db.PutJSON(StreamInfoKey, status.Data.Streams) err = b.api.db.PutJSON(StreamInfoKey, status.Data.Streams)
if err != nil { if err != nil {
b.logger.Warn("Error saving stream info", zap.Error(err)) b.logger.Warn("Error saving stream info", zap.Error(err))
} }
}()
} }
}() }()
go func() { go func() {
defer statusMux.Unlock()
for { for {
// Wait for next poll status := loyalty.Status()
time.Sleep(time.Duration(config.Points.Interval) * time.Second) if status.Enabled {
config := loyalty.Config()
if config.Points.Interval > 0 {
// Wait for next poll
time.Sleep(time.Duration(config.Points.Interval) * time.Second)
// If stream is confirmed offline, don't give points away! // If stream is confirmed offline, don't give points away!
statusMux.Lock() statusMux.Lock()
isOnline := streamOnline isOnline := streamOnline
statusMux.Unlock() statusMux.Unlock()
if !isOnline { if !isOnline {
continue
}
if config.Points.Interval > 0 {
b.logger.Debug("awarding points")
// Get user list
users, err := b.Client.Userlist(b.config.Channel)
if err != nil {
b.logger.Error("error listing users", zap.Error(err))
continue
}
// Iterate for each user in the list
pointsToGive := make(map[string]int64)
for _, user := range users {
// Check if user is blocked
if b.IsBanned(user) {
continue continue
} }
// Check if user was active (chatting) for the bonus dingus b.logger.Debug("awarding points")
award := config.Points.Amount
if b.IsActive(user) { // Get user list
award += config.Points.ActivityBonus users, err := b.Client.Userlist(b.config.Channel)
if err != nil {
b.logger.Error("error listing users", zap.Error(err))
continue
} }
// Add to point pool if already on it, otherwise initialize // Iterate for each user in the list
pointsToGive[user] = award pointsToGive := make(map[string]int64)
} for _, user := range users {
// Check if user is blocked
if b.IsBanned(user) {
continue
}
b.ResetActivity() // Check if user was active (chatting) for the bonus dingus
award := config.Points.Amount
if b.IsActive(user) {
award += config.Points.ActivityBonus
}
// If changes were made, save the pool! // Add to point pool if already on it, otherwise initialize
if len(users) > 0 { pointsToGive[user] = award
err := b.Loyalty.GivePoints(pointsToGive) }
if err != nil {
b.logger.Error("error giving points to user", zap.Error(err)) b.ResetActivity()
// If changes were made, save the pool!
if len(users) > 0 {
err := b.Loyalty.GivePoints(pointsToGive)
if err != nil {
b.logger.Error("error giving points to user", zap.Error(err))
}
} }
} }
} }