2020-04-06 07:41:42 +00:00
|
|
|
package workers
|
2020-04-03 16:08:49 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/photoprism/photoprism/internal/config"
|
2020-04-06 07:41:42 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/event"
|
2020-04-03 16:08:49 +00:00
|
|
|
"github.com/photoprism/photoprism/internal/mutex"
|
|
|
|
)
|
|
|
|
|
2020-04-06 07:41:42 +00:00
|
|
|
var log = event.Log
|
|
|
|
var stop = make(chan bool, 1)
|
|
|
|
|
2020-04-06 20:27:05 +00:00
|
|
|
// Start runs PhotoPrism background workers every wakeup interval.
|
2020-04-06 07:41:42 +00:00
|
|
|
func Start(conf *config.Config) {
|
2020-04-06 20:09:45 +00:00
|
|
|
ticker := time.NewTicker(conf.WakeupInterval())
|
2020-04-03 16:08:49 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-stop:
|
2020-04-06 20:09:45 +00:00
|
|
|
log.Info("shutting down workers")
|
2020-04-03 16:08:49 +00:00
|
|
|
ticker.Stop()
|
|
|
|
mutex.Share.Cancel()
|
|
|
|
mutex.Sync.Cancel()
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
2020-04-06 08:26:26 +00:00
|
|
|
StartShare(conf)
|
|
|
|
StartSync(conf)
|
2020-04-03 16:08:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-04-06 07:41:42 +00:00
|
|
|
}
|
2020-04-03 16:08:49 +00:00
|
|
|
|
2020-04-06 14:34:29 +00:00
|
|
|
// Stop shuts down all service workers.
|
|
|
|
func Stop() {
|
|
|
|
stop <- true
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartShare runs the share worker once.
|
2020-04-06 08:26:26 +00: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 14:34:29 +00:00
|
|
|
// StartShare runs the sync worker once.
|
2020-04-06 08:26:26 +00:00
|
|
|
func StartSync(conf *config.Config) {
|
|
|
|
if !mutex.Sync.Busy() {
|
|
|
|
go func() {
|
|
|
|
s := NewSync(conf)
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|