mirror of
https://github.com/muety/wakapi.git
synced 2025-12-05 22:20:24 -08:00
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:
@@ -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
|
||||
}
|
||||
|
||||
|
||||
@@ -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},
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user