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()
|
|
|
|
mutex.Share.Cancel()
|
|
|
|
mutex.Sync.Cancel()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartShare runs the share worker once.
|
2020-04-06 10:26:26 +02:00
|
|
|
func StartShare(conf *config.Config) {
|
|
|
|
if !mutex.Share.Busy() {
|
|
|
|
go func() {
|
|
|
|
s := NewShare(conf)
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
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) {
|
|
|
|
if !mutex.Sync.Busy() {
|
|
|
|
go func() {
|
|
|
|
s := NewSync(conf)
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|