2021-01-02 18:56:15 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Package workers contains background workers for file sync & metadata optimization.
|
|
|
|
|
2022-02-20 17:36:51 +01:00
|
|
|
Copyright (c) 2018 - 2022 Michael Mayer <hello@photoprism.app>
|
2021-01-02 18:56:15 +01:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
2022-02-21 15:30:18 +01:00
|
|
|
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
|
|
|
|
|
|
|
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.
|
|
|
|
|
2022-02-21 15:30:18 +01: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-02-21 15:30:18 +01:00
|
|
|
Feel free to send an e-mail 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:
|
2020-06-29 13:35:38 +02:00
|
|
|
StartMeta(conf)
|
2020-04-06 10:26:26 +02:00
|
|
|
StartShare(conf)
|
|
|
|
StartSync(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
|
|
|
|
}
|
|
|
|
|
2020-06-29 13:35:38 +02:00
|
|
|
// StartMeta runs the metadata worker once.
|
|
|
|
func StartMeta(conf *config.Config) {
|
2020-05-26 15:15:14 +02:00
|
|
|
if !mutex.WorkersBusy() {
|
|
|
|
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
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 16:34:29 +02:00
|
|
|
// StartShare runs the share worker once.
|
2020-04-06 10:26:26 +02:00
|
|
|
func StartShare(conf *config.Config) {
|
2020-05-26 15:15:14 +02:00
|
|
|
if !mutex.ShareWorker.Busy() {
|
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
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-24 14:27:34 +02:00
|
|
|
// StartSync runs the sync worker once.
|
2020-04-06 10:26:26 +02:00
|
|
|
func StartSync(conf *config.Config) {
|
2020-05-26 15:15:14 +02:00
|
|
|
if !mutex.SyncWorker.Busy() {
|
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
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|