2021-01-02 18:56:15 +01:00
|
|
|
/*
|
2022-04-25 09:53:55 +02:00
|
|
|
Package workers provides index, sync, and metadata optimization background workers.
|
2021-01-02 18:56:15 +01:00
|
|
|
|
2022-04-13 22:17:59 +02:00
|
|
|
Copyright (c) 2018 - 2022 PhotoPrism UG. All rights reserved.
|
2021-01-02 18:56:15 +01:00
|
|
|
|
2022-08-10 16:09:21 +02:00
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under Version 3 of the GNU Affero General Public License (the "AGPL"):
|
|
|
|
<https://docs.photoprism.app/license/agpl>
|
2021-01-02 18:56:15 +01:00
|
|
|
|
2022-08-10 16:09:21 +02:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Affero General Public License for more details.
|
2021-01-02 18:56:15 +01:00
|
|
|
|
2022-08-10 16:09:21 +02:00
|
|
|
The AGPL is supplemented by our Trademark and Brand Guidelines,
|
|
|
|
which describe how our Brand Assets may be used:
|
|
|
|
<https://photoprism.app/trademark>
|
2021-01-02 18:56:15 +01:00
|
|
|
|
2022-04-13 22:17:59 +02:00
|
|
|
Feel free to send an email to hello@photoprism.app if you have questions,
|
2021-01-02 18:56:15 +01:00
|
|
|
want to support our work, or just want to say hello.
|
|
|
|
|
|
|
|
Additional information can be found in our Developer Guide:
|
2022-02-27 17:32:54 +01:00
|
|
|
<https://docs.photoprism.app/developer-guide/>
|
2021-01-02 18:56:15 +01:00
|
|
|
*/
|
2020-04-06 09:41:42 +02:00
|
|
|
package workers
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2021-11-20 19:14:00 +01:00
|
|
|
"github.com/photoprism/photoprism/internal/entity"
|
2020-04-06 09:41:42 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-04-03 18:08:49 +02:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
|
|
|
)
|
|
|
|
|
2020-04-06 09:41:42 +02:00
|
|
|
var log = event.Log
|
|
|
|
var stop = make(chan bool, 1)
|
|
|
|
|
2021-10-17 14:25:29 +02:00
|
|
|
// Start runs the metadata, share & sync background workers at regular intervals.
|
2020-04-06 09:41:42 +02:00
|
|
|
func Start(conf *config.Config) {
|
2021-10-17 14:25:29 +02:00
|
|
|
interval := conf.WakeupInterval()
|
|
|
|
|
|
|
|
// Disabled in safe mode?
|
|
|
|
if interval.Seconds() <= 0 {
|
|
|
|
log.Warnf("config: disabled metadata, share & sync background workers")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(interval)
|
2020-04-03 18:08:49 +02:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
2020-04-06 22:09:45 +02:00
|
|
|
log.Info("shutting down workers")
|
2020-04-03 18:08:49 +02:00
|
|
|
ticker.Stop()
|
2020-06-29 13:35:38 +02:00
|
|
|
mutex.MetaWorker.Cancel()
|
2020-05-26 15:15:14 +02:00
|
|
|
mutex.ShareWorker.Cancel()
|
|
|
|
mutex.SyncWorker.Cancel()
|
2020-04-03 18:08:49 +02:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2022-10-03 22:59:29 +02:00
|
|
|
RunMeta(conf)
|
|
|
|
RunShare(conf)
|
|
|
|
RunSync(conf)
|
2020-04-03 18:08:49 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-04-06 09:41:42 +02:00
|
|
|
}
|
2020-04-03 18:08:49 +02:00
|
|
|
|
2020-04-06 16:34:29 +02:00
|
|
|
// Stop shuts down all service workers.
|
|
|
|
func Stop() {
|
|
|
|
stop <- true
|
|
|
|
}
|
|
|
|
|
2022-10-03 22:59:29 +02:00
|
|
|
// RunMeta runs the metadata worker once.
|
|
|
|
func RunMeta(conf *config.Config) {
|
|
|
|
if !mutex.IndexWorkersRunning() {
|
2020-05-26 15:15:14 +02:00
|
|
|
go func() {
|
2020-06-29 13:35:38 +02:00
|
|
|
worker := NewMeta(conf)
|
2021-11-20 19:14:00 +01:00
|
|
|
|
|
|
|
delay := time.Minute
|
|
|
|
interval := entity.MetadataUpdateInterval
|
|
|
|
|
|
|
|
if err := worker.Start(delay, interval, false); err != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Warnf("metadata: %s", err)
|
2020-05-26 15:15:14 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 22:59:29 +02:00
|
|
|
// RunShare runs the share worker once.
|
|
|
|
func RunShare(conf *config.Config) {
|
2022-09-28 09:01:17 +02:00
|
|
|
if !mutex.ShareWorker.Running() {
|
2020-04-06 10:26:26 +02:00
|
|
|
go func() {
|
2020-05-26 15:15:14 +02:00
|
|
|
worker := NewShare(conf)
|
|
|
|
if err := worker.Start(); err != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Warnf("share: %s", err)
|
2020-04-06 10:26:26 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 22:59:29 +02:00
|
|
|
// RunSync runs the sync worker once.
|
|
|
|
func RunSync(conf *config.Config) {
|
2022-09-28 09:01:17 +02:00
|
|
|
if !mutex.SyncWorker.Running() {
|
2020-04-06 10:26:26 +02:00
|
|
|
go func() {
|
2020-05-26 15:15:14 +02:00
|
|
|
worker := NewSync(conf)
|
|
|
|
if err := worker.Start(); err != nil {
|
2020-12-05 00:13:44 +01:00
|
|
|
log.Warnf("sync: %s", err)
|
2020-04-06 10:26:26 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|