#1 - Created request to check newest version of the app
All checks were successful
Build / build (push) Successful in 2m35s
All checks were successful
Build / build (push) Successful in 2m35s
#2 - Added request to download the newest version of the app #3 - Added request to check progress during sync #4 - Now blocking all request while sync is in progress #5 - Implemented ants for thread pooling #6 - Changed the sync request to now only start the sync
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"fmt"
|
||||
"github.com/panjf2000/ants/v2"
|
||||
"io"
|
||||
"io/fs"
|
||||
"log"
|
||||
@@ -21,6 +22,11 @@ import (
|
||||
"github.com/spf13/afero"
|
||||
)
|
||||
|
||||
var Syncing = false
|
||||
var foldersSynced float32
|
||||
var numberOfFoldersToSync float32
|
||||
var totalTime time.Duration
|
||||
|
||||
var allGames []repository.Game
|
||||
var gamesBeforeSync []repository.Game
|
||||
var gamesAfterSync []repository.Game
|
||||
@@ -61,67 +67,18 @@ func (gs GameStatus) String() string {
|
||||
return statusName[gs]
|
||||
}
|
||||
|
||||
var syncWg sync.WaitGroup
|
||||
|
||||
func ResetDB() {
|
||||
//db.ClearSongs(-1)
|
||||
repo.ClearSongs(db.Ctx)
|
||||
//db.ClearGames()
|
||||
repo.ClearGames(db.Ctx)
|
||||
}
|
||||
|
||||
func SyncGamesNewFull() Response {
|
||||
return syncGamesNew(true)
|
||||
func SyncProgress() string {
|
||||
//log.Printf("Progress: %v%%\n", int((foldersSynced/numberOfFoldersToSync)*10))
|
||||
log.Printf("Progress: %v/%v %v%%\n", int(foldersSynced), int(numberOfFoldersToSync), int((foldersSynced/numberOfFoldersToSync)*100))
|
||||
return fmt.Sprintf("Progress: %v%%", int((foldersSynced/numberOfFoldersToSync)*100))
|
||||
}
|
||||
|
||||
func SyncGamesNewOnlyChanges() Response {
|
||||
return syncGamesNew(false)
|
||||
}
|
||||
|
||||
func syncGamesNew(full bool) Response {
|
||||
musicPath := os.Getenv("MUSIC_PATH")
|
||||
fmt.Printf("dir: %s\n", musicPath)
|
||||
initRepo()
|
||||
start := time.Now()
|
||||
foldersToSkip := []string{".sync", "dist", "old", "characters"}
|
||||
fmt.Println(foldersToSkip)
|
||||
|
||||
var err error
|
||||
gamesAdded = nil
|
||||
gamesReAdded = nil
|
||||
gamesChangedTitle = nil
|
||||
gamesChangedContent = nil
|
||||
gamesRemoved = nil
|
||||
catchedErrors = nil
|
||||
brokenSongs = nil
|
||||
|
||||
gamesBeforeSync, err = repo.FindAllGames(db.Ctx)
|
||||
handleError("FindAllGames Before", err, "")
|
||||
fmt.Printf("Games Before: %d\n", len(gamesBeforeSync))
|
||||
|
||||
allGames, err = repo.GetAllGamesIncludingDeleted(db.Ctx)
|
||||
handleError("GetAllGamesIncludingDeleted", err, "")
|
||||
err = repo.SetGameDeletionDate(db.Ctx)
|
||||
handleError("SetGameDeletionDate", err, "")
|
||||
|
||||
directories, err := os.ReadDir(musicPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
syncWg.Add(len(directories))
|
||||
for _, dir := range directories {
|
||||
go func() {
|
||||
defer syncWg.Done()
|
||||
syncGameNew(dir, foldersToSkip, musicPath, full)
|
||||
}()
|
||||
}
|
||||
syncWg.Wait()
|
||||
checkBrokenSongsNew()
|
||||
|
||||
gamesAfterSync, err = repo.FindAllGames(db.Ctx)
|
||||
handleError("FindAllGames After", err, "")
|
||||
|
||||
func SyncResult() Response {
|
||||
fmt.Printf("\nGames Before: %d\n", len(gamesBeforeSync))
|
||||
fmt.Printf("Games After: %d\n", len(gamesAfterSync))
|
||||
|
||||
@@ -148,16 +105,16 @@ func syncGamesNew(full bool) Response {
|
||||
fmt.Printf("\n\n")
|
||||
var gamesRemovedTemp []string
|
||||
for _, beforeGame := range gamesBeforeSync {
|
||||
var found bool = false
|
||||
var found = false
|
||||
for _, afterGame := range gamesAfterSync {
|
||||
if beforeGame.GameName == afterGame.GameName {
|
||||
found = true
|
||||
//fmt.Printf("Game: %s, Found: %v break\n", beforeGame.GameName, found)
|
||||
fmt.Printf("Game: %s, Found: %v break\n", beforeGame.GameName, found)
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
//fmt.Printf("Game: %s, Found: %v\n", beforeGame.GameName, found)
|
||||
fmt.Printf("Game: %s, Found: %v\n", beforeGame.GameName, found)
|
||||
gamesRemovedTemp = append(gamesRemovedTemp, beforeGame.GameName)
|
||||
}
|
||||
}
|
||||
@@ -186,8 +143,6 @@ func syncGamesNew(full bool) Response {
|
||||
fmt.Printf("%s\n", error)
|
||||
}
|
||||
|
||||
finished := time.Now()
|
||||
totalTime := finished.Sub(start)
|
||||
out := time.Time{}.Add(totalTime)
|
||||
fmt.Printf("\nTotal time: %v\n", totalTime)
|
||||
fmt.Printf("Total time: %v\n", out.Format("15:04:05.00000"))
|
||||
@@ -202,16 +157,94 @@ func syncGamesNew(full bool) Response {
|
||||
}
|
||||
}
|
||||
|
||||
func SyncGamesNewFull() {
|
||||
syncGamesNew(true)
|
||||
Reset()
|
||||
}
|
||||
|
||||
func SyncGamesNewOnlyChanges() {
|
||||
syncGamesNew(false)
|
||||
Reset()
|
||||
}
|
||||
|
||||
func syncGamesNew(full bool) {
|
||||
Syncing = true
|
||||
|
||||
musicPath := os.Getenv("MUSIC_PATH")
|
||||
fmt.Printf("dir: %s\n", musicPath)
|
||||
if !strings.HasSuffix(musicPath, "/") {
|
||||
musicPath += "/"
|
||||
}
|
||||
|
||||
var syncWg sync.WaitGroup
|
||||
|
||||
initRepo()
|
||||
start := time.Now()
|
||||
foldersToSkip := []string{".sync", "dist", "old", "characters"}
|
||||
fmt.Println(foldersToSkip)
|
||||
|
||||
var err error
|
||||
gamesAdded = nil
|
||||
gamesReAdded = nil
|
||||
gamesChangedTitle = nil
|
||||
gamesChangedContent = nil
|
||||
gamesRemoved = nil
|
||||
catchedErrors = nil
|
||||
brokenSongs = nil
|
||||
|
||||
gamesBeforeSync, err = repo.FindAllGames(db.Ctx)
|
||||
handleError("FindAllGames Before", err, "")
|
||||
fmt.Printf("Games Before: %d\n", len(gamesBeforeSync))
|
||||
|
||||
allGames, err = repo.GetAllGamesIncludingDeleted(db.Ctx)
|
||||
handleError("GetAllGamesIncludingDeleted", err, "")
|
||||
err = repo.SetGameDeletionDate(db.Ctx)
|
||||
handleError("SetGameDeletionDate", err, "")
|
||||
|
||||
directories, err := os.ReadDir(musicPath)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
pool, _ := ants.NewPool(50, ants.WithPreAlloc(true))
|
||||
defer pool.Release()
|
||||
|
||||
numberOfFoldersToSync = float32(len(directories))
|
||||
syncWg.Add(int(numberOfFoldersToSync))
|
||||
for _, dir := range directories {
|
||||
pool.Submit(func() {
|
||||
defer syncWg.Done()
|
||||
syncGameNew(dir, foldersToSkip, musicPath, full)
|
||||
})
|
||||
}
|
||||
syncWg.Wait()
|
||||
checkBrokenSongsNew()
|
||||
|
||||
gamesAfterSync, err = repo.FindAllGames(db.Ctx)
|
||||
handleError("FindAllGames After", err, "")
|
||||
|
||||
finished := time.Now()
|
||||
totalTime = finished.Sub(start)
|
||||
out := time.Time{}.Add(totalTime)
|
||||
fmt.Printf("\nTotal time: %v\n", totalTime)
|
||||
fmt.Printf("Total time: %v\n", out.Format("15:04:05.00000"))
|
||||
|
||||
Syncing = false
|
||||
}
|
||||
|
||||
func checkBrokenSongsNew() {
|
||||
allSongs, err := repo.FetchAllSongs(db.Ctx)
|
||||
handleError("FetchAllSongs", err, "")
|
||||
var brokenWg sync.WaitGroup
|
||||
poolBroken, _ := ants.NewPool(50, ants.WithPreAlloc(true))
|
||||
defer poolBroken.Release()
|
||||
|
||||
brokenWg.Add(len(allSongs))
|
||||
for _, song := range allSongs {
|
||||
go func() {
|
||||
poolBroken.Submit(func() {
|
||||
defer brokenWg.Done()
|
||||
checkBrokenSongNew(song)
|
||||
}()
|
||||
})
|
||||
}
|
||||
brokenWg.Wait()
|
||||
err = repo.RemoveBrokenSongs(db.Ctx, brokenSongs)
|
||||
@@ -343,6 +376,8 @@ func syncGameNew(file os.DirEntry, foldersToSkip []string, baseDir string, full
|
||||
err = repo.RemoveDeletionDate(db.Ctx, id)
|
||||
handleError("RemoveDeletionDate", err, "")
|
||||
}
|
||||
foldersSynced++
|
||||
log.Printf("Progress: %v/%v %v%%\n", int(foldersSynced), int(numberOfFoldersToSync), int((foldersSynced/numberOfFoldersToSync)*100))
|
||||
}
|
||||
|
||||
func insertGameNew(name string, path string, hash string) int32 {
|
||||
@@ -365,19 +400,26 @@ func insertGameNew(name string, path string, hash string) int32 {
|
||||
func newCheckSongs(entries []os.DirEntry, gameDir string, id int32) int32 {
|
||||
//hasher := md5.New()
|
||||
var numberOfSongs int32
|
||||
numberOfFiles := len(entries)
|
||||
|
||||
var songWg sync.WaitGroup
|
||||
songWg.Add(len(entries))
|
||||
poolSong, _ := ants.NewPool(numberOfFiles, ants.WithPreAlloc(true))
|
||||
defer poolSong.Release()
|
||||
|
||||
songWg.Add(numberOfFiles)
|
||||
for _, entry := range entries {
|
||||
go func() {
|
||||
poolSong.Submit(func() {
|
||||
defer songWg.Done()
|
||||
newCheckSong(entry, gameDir, id)
|
||||
}()
|
||||
if newCheckSong(entry, gameDir, id) {
|
||||
numberOfSongs++
|
||||
}
|
||||
})
|
||||
}
|
||||
songWg.Wait()
|
||||
return numberOfSongs
|
||||
}
|
||||
|
||||
func newCheckSong(entry os.DirEntry, gameDir string, id int32) {
|
||||
func newCheckSong(entry os.DirEntry, gameDir string, id int32) bool {
|
||||
fileInfo, err := entry.Info()
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
@@ -396,7 +438,7 @@ func newCheckSong(entry os.DirEntry, gameDir string, id int32) {
|
||||
handleError("GetSongWithHash", err, fmt.Sprintf("GameID: %d | Path: %s | SongName: %s | SongHash: %s\n", id, path, entry.Name(), songHash))
|
||||
if err == nil {
|
||||
if song.SongName == songName && song.Path == path {
|
||||
return
|
||||
return false
|
||||
}
|
||||
}
|
||||
fmt.Printf("Song Changed\n")
|
||||
@@ -432,9 +474,11 @@ func newCheckSong(entry os.DirEntry, gameDir string, id int32) {
|
||||
|
||||
}
|
||||
}
|
||||
return true
|
||||
} else if isCoverImage(fileInfo) {
|
||||
//TODO: Later add cover art image here in db
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func handleError(funcName string, err error, msg string) {
|
||||
|
||||
Reference in New Issue
Block a user