fix: deadlock caused by not using open transaction during batch insert

fix: regenerate durations upon language mapping update
fix: minor ui
This commit is contained in:
Ferdinand Mütsch
2025-02-21 11:28:56 +01:00
parent 7b7f5e94bd
commit b27e9bb083
7 changed files with 895 additions and 848 deletions

View File

@@ -56,6 +56,23 @@ func NewDurationService(durationRepository repositories.IDurationRepository, hea
}
}(&sub1)
sub2 := srv.eventBus.Subscribe(0, config.EventLanguageMappingsChanged)
go func(sub *hub.Subscription) {
for m := range sub.Receiver {
userId := m.Fields[config.FieldUserId].(string)
user, err := srv.userService.GetUserById(userId)
if err != nil {
config.Log().Error("user not found for regenerating durations after language mapping change", "user", userId)
continue
}
slog.Info("regenerating durations because language mappings were updated", "user", userId)
srv.queue.Dispatch(func() {
srv.Regenerate(user, true)
})
}
}(&sub2)
return srv
}

View File

@@ -2,6 +2,7 @@ package services
import (
"errors"
"github.com/leandro-lugaresi/hub"
"github.com/muety/wakapi/config"
"github.com/muety/wakapi/models"
"github.com/muety/wakapi/repositories"
@@ -12,12 +13,14 @@ import (
type LanguageMappingService struct {
config *config.Config
cache *cache.Cache
eventBus *hub.Hub
repository repositories.ILanguageMappingRepository
}
func NewLanguageMappingService(languageMappingsRepo repositories.ILanguageMappingRepository) *LanguageMappingService {
return &LanguageMappingService{
config: config.Get(),
eventBus: config.EventBus(),
repository: languageMappingsRepo,
cache: cache.New(24*time.Hour, 24*time.Hour),
}
@@ -60,6 +63,7 @@ func (srv *LanguageMappingService) Create(mapping *models.LanguageMapping) (*mod
}
srv.cache.Delete(result.UserID)
srv.notifyUpdate(mapping)
return result, nil
}
@@ -69,6 +73,7 @@ func (srv *LanguageMappingService) Delete(mapping *models.LanguageMapping) error
}
err := srv.repository.Delete(mapping.ID)
srv.cache.Delete(mapping.UserID)
srv.notifyUpdate(mapping)
return err
}
@@ -76,3 +81,11 @@ func (srv *LanguageMappingService) getServerMappings() map[string]string {
// https://dave.cheney.net/2017/04/30/if-a-map-isnt-a-reference-variable-what-is-it
return srv.config.App.GetCustomLanguages()
}
func (srv *LanguageMappingService) notifyUpdate(mapping *models.LanguageMapping) {
name := config.EventLanguageMappingsChanged
srv.eventBus.Publish(hub.Message{
Name: name,
Fields: map[string]interface{}{config.FieldPayload: mapping, config.FieldUserId: mapping.UserID},
})
}