Config: Allow scheduling of indexing and backup tasks #2495 #2608 #4243

Note that this is "bleeding edge" functionality and that the newly added
config option PHOTOPRISM_BACKUP_RETAIN can be set, but does not have any
effect yet. Feedback welcome!

Signed-off-by: Michael Mayer <michael@photoprism.app>
This commit is contained in:
Michael Mayer
2024-05-11 19:11:49 +02:00
parent 6a5826e252
commit 0e7c91f1b6
41 changed files with 553 additions and 197 deletions

View File

@@ -27,6 +27,8 @@ package workers
import (
"time"
"github.com/go-co-op/gocron/v2"
"github.com/photoprism/photoprism/internal/config"
"github.com/photoprism/photoprism/internal/entity"
"github.com/photoprism/photoprism/internal/event"
@@ -36,11 +38,32 @@ import (
var log = event.Log
var stop = make(chan bool, 1)
// Start runs the sync and metadata maintenance background workers at regular intervals.
// Start starts the execution of background workers and scheduled tasks based on the current configuration.
func Start(conf *config.Config) {
if scheduler, err := gocron.NewScheduler(gocron.WithLocation(conf.DefaultTimezone())); err != nil {
log.Errorf("scheduler: %s (start)", err)
return
} else if scheduler != nil {
Scheduler = scheduler
// Schedule backup job.
if err = NewJob("backup", conf.BackupSchedule(), NewBackup(conf).StartScheduled); err != nil {
log.Errorf("scheduler: %s (backup)", err)
}
// Schedule indexing job.
if err = NewJob("index", conf.IndexSchedule(), NewIndex(conf).StartScheduled); err != nil {
log.Errorf("scheduler: %s (index)", err)
}
// Start the scheduler.
Scheduler.Start()
}
// Start the other background workers.
interval := conf.WakeupInterval()
// Disabled in safe mode?
// Other workers can be disabled in safe mode by setting the execution interval to a value < 1.
if interval.Seconds() <= 0 {
log.Warnf("config: disabled metadata, share & sync background workers")
return
@@ -52,7 +75,6 @@ func Start(conf *config.Config) {
for {
select {
case <-stop:
log.Info("shutting down workers")
ticker.Stop()
mutex.MetaWorker.Cancel()
mutex.ShareWorker.Cancel()
@@ -67,14 +89,22 @@ func Start(conf *config.Config) {
}()
}
// Stop shuts down all service workers.
func Stop() {
// Shutdown stops the background workers and scheduled tasks.
func Shutdown() {
log.Info("shutting down workers")
stop <- true
if Scheduler != nil {
if err := Scheduler.Shutdown(); err != nil {
log.Warnf("scheduler: %s (shutdown)", err)
}
}
}
// RunMeta runs the metadata worker once.
// RunMeta runs the metadata maintenance worker once.
func RunMeta(conf *config.Config) {
if !mutex.IndexWorkersRunning() {
if !mutex.WorkersRunning() {
go func() {
worker := NewMeta(conf)