2021-01-02 18:56:15 +01:00
|
|
|
/*
|
|
|
|
|
|
|
|
Package workers contains background workers for file sync & metadata optimization.
|
|
|
|
|
|
|
|
Copyright (c) 2018 - 2021 Michael Mayer <hello@photoprism.org>
|
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU Affero General Public License as published
|
|
|
|
by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
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.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
PhotoPrism® is a registered trademark of Michael Mayer. You may use it as required
|
|
|
|
to describe our software, run your own server, for educational purposes, but not for
|
|
|
|
offering commercial goods, products, or services without prior written permission.
|
|
|
|
In other words, please ask.
|
|
|
|
|
|
|
|
Feel free to send an e-mail to hello@photoprism.org if you have questions,
|
|
|
|
want to support our work, or just want to say hello.
|
|
|
|
|
|
|
|
Additional information can be found in our Developer Guide:
|
|
|
|
https://docs.photoprism.org/developer-guide/
|
|
|
|
|
|
|
|
*/
|
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"
|
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-18 03:47:25 +01:00
|
|
|
if err := worker.Start(time.Minute, 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
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|