Make pool and poolSong global variables
Build / build (push) Successful in 48s

This commit is contained in:
2026-05-26 20:54:12 +02:00
parent d152ec1f11
commit a446dad7b6
12 changed files with 253 additions and 258 deletions
+42 -28
View File
@@ -41,6 +41,8 @@ var gamesChangedContent []string
var gamesRemoved []string
var catchedErrors []string
var brokenSongs []string
var pool *ants.Pool
var poolSong *ants.Pool
type SyncResponse struct {
GamesAdded []string `json:"games_added"`
@@ -155,7 +157,7 @@ func SyncResult() SyncResponse {
}
out := time.Time{}.Add(totalTime)
logging.GetLogger().Info("Sync completed", zap.Duration("total_time", totalTime))
logging.GetLogger().Info("Sync completed", zap.String("total_time", out.Format("15:04:05.00000")))
return SyncResponse{
GamesAdded: gamesAdded,
@@ -183,6 +185,7 @@ func syncGamesNew(full bool) {
musicPath := os.Getenv("MUSIC_PATH")
fmt.Printf("dir: %s\n", musicPath)
logging.GetLogger().Debug("Folder to sync", zap.String("MUSIC_PATH", musicPath))
if !strings.HasSuffix(musicPath, "/") {
musicPath += "/"
}
@@ -217,8 +220,10 @@ func syncGamesNew(full bool) {
logging.GetLogger().Fatal("Failed to read music directory", zap.String("path", musicPath), zap.String("error", err.Error()))
}
pool, _ := ants.NewPool(10, ants.WithPreAlloc(true))
pool, _ = ants.NewPool(10, ants.WithPreAlloc(true))
poolSong, _ = ants.NewPool(10, ants.WithPreAlloc(true))
defer pool.Release()
defer poolSong.Release()
foldersSynced = 0
numberOfFoldersToSync = float32(len(directories))
@@ -320,11 +325,12 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
switch status {
case NewGame:
if id != -1 {
for _, entry := range entries {
fileInfo, err := entry.Info()
if err != nil {
logging.GetLogger().Error("Failed to get file info", zap.String("error", err.Error()))
}
for _, entry := range entries {
fileInfo, err := entry.Info()
if err != nil {
logging.GetLogger().Error("Failed to get file info", zap.String("error", err.Error()))
continue
}
id = getIdFromFileNew(fileInfo)
if id != -1 {
break
@@ -332,22 +338,22 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
}
err = repo.InsertGameWithExistingId(db.Ctx, repository.InsertGameWithExistingIdParams{ID: id, GameName: file.Name(), Path: gameDir, Hash: dirHash})
handleError("InsertGameWithExistingId", err, "")
if err != nil {
logging.GetLogger().Debug("Game already exists, removing old ID file",
zap.Int32("id", id),
zap.String("game_dir", gameDir))
fileName := gameDir + "/." + strconv.Itoa(int(id)) + ".id"
logging.GetLogger().Debug("Removing ID file", zap.String("filename", fileName))
err := os.Remove(fileName)
if err != nil {
logging.GetLogger().Error("Failed to remove ID file", zap.String("filename", fileName), zap.String("error", err.Error()))
logging.GetLogger().Debug("Game already exists, removing old ID file",
zap.Int32("id", id),
zap.String("game_dir", gameDir))
fileName := gameDir + "/." + strconv.Itoa(int(id)) + ".id"
logging.GetLogger().Debug("Removing ID file", zap.String("filename", fileName))
err := os.Remove(fileName)
if err != nil {
logging.GetLogger().Error("Failed to remove ID file", zap.String("filename", fileName), zap.String("error", err.Error()))
}
newDirHash := getHashForDir(gameDir)
id = insertGameNew(file.Name(), gameDir, newDirHash)
}
newDirHash := getHashForDir(gameDir)
id = insertGameNew(file.Name(), gameDir, newDirHash)
}
} else {
id = insertGameNew(file.Name(), gameDir, dirHash)
}
@@ -371,7 +377,8 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
case TitleChanged:
logging.GetLogger().Debug("Game title changed",
zap.Int32("id", id),
zap.String("game", file.Name()),
zap.String("oldName", oldGame.GameName),
zap.String("newName", file.Name()),
zap.String("hash", dirHash),
zap.String("status", status.String()))
err = repo.UpdateGameName(db.Ctx, repository.UpdateGameNameParams{Name: file.Name(), Path: gameDir, ID: id})
@@ -386,11 +393,21 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
for _, beforeGame := range gamesBeforeSync {
if dirHash == beforeGame.Hash {
found = true
logging.GetLogger().Debug("Game not changed",
zap.Int32("id", id),
zap.String("newName", file.Name()),
zap.String("hash", dirHash),
zap.String("status", status.String()))
}
}
if !found {
newCheckSongs(entries, gameDir, id)
gamesReAdded = append(gamesReAdded, file.Name())
logging.GetLogger().Debug("Game added again",
zap.Int32("id", id),
zap.String("newName", file.Name()),
zap.String("hash", dirHash),
zap.String("status", status.String()))
}
}
@@ -432,9 +449,6 @@ func newCheckSongs(entries []os.DirEntry, gameDir string, id int32) int32 {
numberOfFiles := len(entries)
var songWg sync.WaitGroup
poolSong, _ := ants.NewPool(10, ants.WithPreAlloc(true))
defer poolSong.Release()
songWg.Add(numberOfFiles)
for _, entry := range entries {
poolSong.Submit(func() {
@@ -484,9 +498,9 @@ func newCheckSong(entry os.DirEntry, gameDir string, id int32) bool {
handleError("CheckSong", err, fmt.Sprintf("GameID: %d | Path: %s | SongName: %s | SongHash: %s\n", id, path, entry.Name(), songHash))
if count2 > 0 {
err = repo.AddHashToSong(db.Ctx, repository.AddHashToSongParams{Hash: songHash, Path: path})
handleError("AddHashToSong", err, fmt.Sprintf("GameID: %d | Path: %s | SongName: %s | SongHash: %s", id, path, entry.Name(), songHash))
count, err = repo.CheckSongWithHash(db.Ctx, songHash)
handleError("CheckSongWithHash 2", err, fmt.Sprintf("GameID: %d | Path: %s | SongName: %s | SongHash: %s", id, path, entry.Name(), songHash))
handleError("AddHashToSong", err, fmt.Sprintf("GameID: %d | Path: %s | SongName: %s | SongHash: %s", id, path, entry.Name(), songHash))
count, err = repo.CheckSongWithHash(db.Ctx, songHash)
handleError("CheckSongWithHash 2", err, fmt.Sprintf("GameID: %d | Path: %s | SongName: %s | SongHash: %s", id, path, entry.Name(), songHash))
}
}