Browse Source

fix character service not refreshing logs.

master 2.1.2
Gisle Aune 4 years ago
parent
commit
6ad0b9d467
  1. 30
      services/characters.go
  2. 2
      services/services.go

30
services/characters.go

@ -7,8 +7,11 @@ import (
"git.aiterp.net/rpdata/api/models/changekeys" "git.aiterp.net/rpdata/api/models/changekeys"
"git.aiterp.net/rpdata/api/repositories" "git.aiterp.net/rpdata/api/repositories"
"git.aiterp.net/rpdata/api/services/loaders" "git.aiterp.net/rpdata/api/services/loaders"
"log"
"sort" "sort"
"strings" "strings"
"sync/atomic"
"time"
) )
type CharacterService struct { type CharacterService struct {
@ -16,6 +19,9 @@ type CharacterService struct {
loader *loaders.CharacterLoader loader *loaders.CharacterLoader
changeService *ChangeService changeService *ChangeService
authService *AuthService authService *AuthService
logService *LogService
refreshSoftLock uint32
} }
// Find uses the loader to find the character by the ID. // Find uses the loader to find the character by the ID.
@ -61,6 +67,8 @@ func (s *CharacterService) List(ctx context.Context, filter models.CharacterFilt
return characters, nil return characters, nil
} }
go s.refreshLogs()
return s.characters.List(ctx, filter) return s.characters.List(ctx, filter)
} }
@ -108,6 +116,8 @@ func (s *CharacterService) Create(ctx context.Context, nick, name, shortName, au
s.changeService.Submit(ctx, "Character", "add", true, changekeys.Listed(character), character) s.changeService.Submit(ctx, "Character", "add", true, changekeys.Listed(character), character)
go s.refreshLogs()
return character, nil return character, nil
} }
@ -160,6 +170,8 @@ func (s *CharacterService) AddNick(ctx context.Context, id string, nick string)
s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character) s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character)
go s.refreshLogs()
return character, nil return character, nil
} }
@ -184,6 +196,8 @@ func (s *CharacterService) RemoveNick(ctx context.Context, id string, nick strin
s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character) s.changeService.Submit(ctx, "Character", "edit", true, changekeys.Listed(character), character)
go s.refreshLogs()
return character, nil return character, nil
} }
@ -207,5 +221,21 @@ func (s *CharacterService) Delete(ctx context.Context, id string) (*models.Chara
s.changeService.Submit(ctx, "Character", "remove", true, changekeys.Listed(character), character) s.changeService.Submit(ctx, "Character", "remove", true, changekeys.Listed(character), character)
go s.refreshLogs()
return character, nil return character, nil
} }
func (s *CharacterService) refreshLogs() {
if !atomic.CompareAndSwapUint32(&s.refreshSoftLock, 0, 1) {
return
}
time.Sleep(time.Second * 60)
atomic.StoreUint32(&s.refreshSoftLock, 0)
err := s.logService.RefreshAllLogCharacters(context.Background())
if err != nil {
log.Println("Failed to refersh log characters:", err)
return
}
}

2
services/services.go

@ -67,5 +67,7 @@ func NewBundle(db database.Database, spaceClient *space.Client) *Bundle {
authService: bundle.Auth, authService: bundle.Auth,
} }
bundle.Characters.logService = bundle.Logs
return bundle return bundle
} }
Loading…
Cancel
Save