diff --git a/internal/commands/start.go b/internal/commands/start.go index 058686db6..89cf4e40c 100644 --- a/internal/commands/start.go +++ b/internal/commands/start.go @@ -99,16 +99,16 @@ func startAction(ctx *cli.Context) error { return nil } - child, err := dctx.Reborn() - if err != nil { - log.Fatal(err) + child, contextErr := dctx.Reborn() + + if contextErr != nil { + return fmt.Errorf("daemon context error: %w", contextErr) } if child != nil { if writeErr := fs.WriteString(conf.PIDFilename(), strconv.Itoa(child.Pid)); writeErr != nil { log.Error(writeErr) - log.Fatalf("failed writing process id to %s", clean.Log(conf.PIDFilename())) - return nil + return fmt.Errorf("failed writing process id to %s", clean.Log(conf.PIDFilename())) } log.Infof("daemon started with process id %v\n", child.Pid) @@ -116,22 +116,28 @@ func startAction(ctx *cli.Context) error { } } + // Show info if read-only mode is enabled. if conf.ReadOnly() { log.Infof("config: enabled read-only mode") } - // Start Web server. + // Start built-in web server. go server.Start(cctx, conf) - if count, err := photoprism.RestoreAlbums(conf.AlbumsPath(), false); err != nil { - log.Errorf("restore: %s", err) + // Restore albums from YAML files. + if count, restoreErr := photoprism.RestoreAlbums(conf.AlbumsPath(), false); restoreErr != nil { + log.Errorf("restore: %s", restoreErr) } else if count > 0 { log.Infof("%d albums restored", count) } - // Start background workers. - session.Monitor(time.Hour) + // Start worker that periodically deletes expired sessions. + session.Cleanup(conf.SessionCacheDuration() * 4) + + // Start sync and metadata maintenance background workers. workers.Start(conf) + + // Start auto-indexing background worker. auto.Start(conf) // Wait for signal to initiate server shutdown. @@ -149,8 +155,8 @@ func startAction(ctx *cli.Context) error { log.Info("shutting down...") cancel() - if err := dctx.Release(); err != nil { - log.Error(err) + if contextErr := dctx.Release(); contextErr != nil { + log.Error(contextErr) } // Finally, close the DB connection after a short grace period. diff --git a/internal/session/monitor.go b/internal/session/cleanup.go similarity index 65% rename from internal/session/monitor.go rename to internal/session/cleanup.go index 154fb178d..f497a14fd 100644 --- a/internal/session/monitor.go +++ b/internal/session/cleanup.go @@ -11,8 +11,8 @@ import ( var stop = make(chan bool, 1) -// MonitorAction deletes expired sessions. -var MonitorAction = func() { +// CleanupAction deletes sessions that have expired. +var CleanupAction = func() { if n := entity.DeleteExpiredSessions(); n > 0 { event.AuditInfo([]string{"deleted %s"}, english.Plural(n, "expired session", "expired sessions")) } else { @@ -20,12 +20,13 @@ var MonitorAction = func() { } } -// Monitor starts a background worker that periodically deletes expired sessions. -func Monitor(interval time.Duration) { +// Cleanup starts a background worker that periodically deletes expired sessions. +func Cleanup(interval time.Duration) { + // Immediately delete sessions that have already expired. + CleanupAction() + + // Periodically delete expired sessions based on the specified interval. ticker := time.NewTicker(interval) - - MonitorAction() - go func() { for { select { @@ -33,7 +34,7 @@ func Monitor(interval time.Duration) { ticker.Stop() return case <-ticker.C: - MonitorAction() + CleanupAction() } } }() diff --git a/internal/session/monitor_test.go b/internal/session/cleanup_test.go similarity index 52% rename from internal/session/monitor_test.go rename to internal/session/cleanup_test.go index cf18ba347..83b7b1b71 100644 --- a/internal/session/monitor_test.go +++ b/internal/session/cleanup_test.go @@ -5,7 +5,7 @@ import ( "time" ) -func TestWatch(t *testing.T) { - Monitor(time.Minute) +func TestCleanup(t *testing.T) { + Cleanup(time.Minute) Shutdown() } diff --git a/internal/workers/workers.go b/internal/workers/workers.go index 2a00e16d3..97e0fc128 100644 --- a/internal/workers/workers.go +++ b/internal/workers/workers.go @@ -36,7 +36,7 @@ import ( var log = event.Log var stop = make(chan bool, 1) -// Start runs the metadata, share & sync background workers at regular intervals. +// Start runs the sync and metadata maintenance background workers at regular intervals. func Start(conf *config.Config) { interval := conf.WakeupInterval()