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)
|
|
|
|
|
2020-04-06 22:27:05 +02:00
|
|
|
// Start runs PhotoPrism background workers every wakeup interval.
|
2020-04-06 09:41:42 +02:00
|
|
|
func Start(conf *config.Config) {
|
2020-04-06 22:09:45 +02:00
|
|
|
ticker := time.NewTicker(conf.WakeupInterval())
|
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-05-26 15:15:14 +02:00
|
|
|
mutex.GroomWorker.Cancel()
|
|
|
|
mutex.ShareWorker.Cancel()
|
|
|
|
mutex.SyncWorker.Cancel()
|
2020-04-03 18:08:49 +02:00
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2020-05-26 15:15:14 +02:00
|
|
|
StartGroom(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-05-26 15:15:14 +02:00
|
|
|
// StartGroom runs the groom worker once.
|
|
|
|
func StartGroom(conf *config.Config) {
|
|
|
|
if !mutex.WorkersBusy() {
|
|
|
|
go func() {
|
|
|
|
worker := NewGroom(conf)
|
|
|
|
if err := worker.Start(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-04-06 10:26:26 +02:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-06 16:34:29 +02:00
|
|
|
// StartShare 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-04-06 10:26:26 +02:00
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|