diff --git a/model/datastore.go b/model/datastore.go index 04774702a..3babb9f1b 100644 --- a/model/datastore.go +++ b/model/datastore.go @@ -41,6 +41,6 @@ type DataStore interface { Resource(ctx context.Context, model interface{}) ResourceRepository - WithTx(func(tx DataStore) error) error + WithTx(block func(tx DataStore) error, scope ...string) error GC(ctx context.Context) error } diff --git a/persistence/persistence.go b/persistence/persistence.go index bae35c0dc..d9569c4d0 100644 --- a/persistence/persistence.go +++ b/persistence/persistence.go @@ -118,14 +118,28 @@ func (s *SQLStore) Resource(ctx context.Context, m interface{}) model.ResourceRe return nil } -func (s *SQLStore) WithTx(block func(tx model.DataStore) error) error { - conn, ok := s.db.(*dbx.DB) - if !ok { +func (s *SQLStore) WithTx(block func(tx model.DataStore) error, scope ...string) error { + var msg string + if len(scope) > 0 { + msg = scope[0] + } + start := time.Now() + conn, inTx := s.db.(*dbx.DB) + if !inTx { + log.Trace("Nested Transaction started", "scope", msg) conn = dbx.NewFromDB(db.Db(), db.Driver) + } else { + log.Trace("Transaction started", "scope", msg) } return conn.Transactional(func(tx *dbx.Tx) error { newDb := &SQLStore{db: tx} - return block(newDb) + err := block(newDb) + if !inTx { + log.Trace("Nested Transaction finished", "scope", msg, "elapsed", time.Since(start), err) + } else { + log.Trace("Transaction finished", "scope", msg, "elapsed", time.Since(start), err) + } + return err }) } diff --git a/scanner/phase_1_folders.go b/scanner/phase_1_folders.go index 44a8dca77..2894878d1 100644 --- a/scanner/phase_1_folders.go +++ b/scanner/phase_1_folders.go @@ -390,7 +390,7 @@ func (p *phaseFolders) persistChanges(entry *folderEntry) (*folderEntry, error) } } return nil - }) + }, "scanner: persist changes") if err != nil { log.Error(p.ctx, "Scanner: Error persisting changes to DB", "folder", entry.path, err) } @@ -464,7 +464,7 @@ func (p *phaseFolders) finalize(err error) error { } } return nil - }) + }, "scanner: finalize phaseFolders") return errors.Join(err, errF) } diff --git a/scanner/phase_2_missing_tracks.go b/scanner/phase_2_missing_tracks.go index 2d54c3487..c0a1287f1 100644 --- a/scanner/phase_2_missing_tracks.go +++ b/scanner/phase_2_missing_tracks.go @@ -159,7 +159,7 @@ func (p *phaseMissingTracks) processMissingTracks(in *missingTracks) (*missingTr } } return nil - }) + }, "scanner: process missing tracks") if err != nil { return nil, err } diff --git a/scanner/phase_3_refresh_albums.go b/scanner/phase_3_refresh_albums.go index 290087688..ad7a68a47 100644 --- a/scanner/phase_3_refresh_albums.go +++ b/scanner/phase_3_refresh_albums.go @@ -113,7 +113,7 @@ func (p *phaseRefreshAlbums) refreshAlbum(album *model.Album) (*model.Album, err p.refreshed.Add(1) p.state.changesDetected.Store(true) return nil - }) + }, "scanner: refresh album") if err != nil { return nil, err } @@ -153,5 +153,5 @@ func (p *phaseRefreshAlbums) finalize(err error) error { log.Debug(p.ctx, "Scanner: Refreshed artist annotations", "artists", cnt, "elapsed", time.Since(start)) p.state.changesDetected.Store(true) return nil - }) + }, "scanner: finalize phaseRefreshAlbums") } diff --git a/scanner/scanner.go b/scanner/scanner.go index a7ba2b16d..d698a32b4 100644 --- a/scanner/scanner.go +++ b/scanner/scanner.go @@ -128,7 +128,7 @@ func (s *scannerImpl) runGC(ctx context.Context, state *scanState) func() error log.Debug(ctx, "Scanner: No changes detected, skipping GC") } return nil - }) + }, "scanner: GC") } } @@ -155,7 +155,7 @@ func (s *scannerImpl) runRefreshStats(ctx context.Context, state *scanState) fun } log.Debug(ctx, "Scanner: Updated tag counts", "elapsed", time.Since(start)) return nil - }) + }, "scanner: refresh stats") } } @@ -189,7 +189,7 @@ func (s *scannerImpl) runUpdateLibraries(ctx context.Context, libs model.Librari } } return nil - }) + }, "scanner: update libraries") } } diff --git a/server/initial_setup.go b/server/initial_setup.go index da2aea255..ebfdad47a 100644 --- a/server/initial_setup.go +++ b/server/initial_setup.go @@ -35,7 +35,7 @@ func initialSetup(ds model.DataStore) { err = properties.Put(consts.InitialSetupFlagKey, time.Now().String()) return err - }) + }, "initial setup") } // If the Dev Admin user is not present, create it diff --git a/tests/mock_data_store.go b/tests/mock_data_store.go index a4f94fb92..e5f7cd8b6 100644 --- a/tests/mock_data_store.go +++ b/tests/mock_data_store.go @@ -209,7 +209,7 @@ func (db *MockDataStore) Radio(ctx context.Context) model.RadioRepository { return db.MockedRadio } -func (db *MockDataStore) WithTx(block func(model.DataStore) error) error { +func (db *MockDataStore) WithTx(block func(tx model.DataStore) error, label ...string) error { return block(db) }