stash/pkg/manager/manager_tasks.go

146 lines
2.8 KiB
Go
Raw Normal View History

2019-02-11 06:39:21 +00:00
package manager
import (
"path/filepath"
"sync"
2019-02-11 06:39:21 +00:00
"github.com/bmatcuk/doublestar"
2019-02-14 23:42:52 +00:00
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/manager/config"
2019-02-14 23:42:52 +00:00
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/utils"
2019-02-11 06:39:21 +00:00
)
2019-10-12 08:20:27 +00:00
func (s *singleton) Scan(nameFromMetadata bool) {
if s.Status != Idle {
return
}
2019-02-11 06:39:21 +00:00
s.Status = Scan
go func() {
defer s.returnToIdleState()
var results []string
for _, path := range config.GetStashPaths() {
globPath := filepath.Join(path, "**/*.{zip,m4v,mp4,mov,wmv,avi,mpg,mpeg,rmvb,rm,flv,asf,mkv,webm}") // TODO: Make this configurable
globResults, _ := doublestar.Glob(globPath)
results = append(results, globResults...)
}
logger.Infof("Starting scan of %d files", len(results))
2019-02-11 06:39:21 +00:00
var wg sync.WaitGroup
for _, path := range results {
2019-02-11 06:39:21 +00:00
wg.Add(1)
2019-10-12 08:20:27 +00:00
task := ScanTask{FilePath: path, NameFromMetadata: nameFromMetadata}
2019-02-11 06:39:21 +00:00
go task.Start(&wg)
wg.Wait()
}
logger.Info("Finished scan")
2019-02-11 06:39:21 +00:00
}()
}
func (s *singleton) Import() {
if s.Status != Idle {
return
}
2019-02-11 06:39:21 +00:00
s.Status = Import
go func() {
defer s.returnToIdleState()
var wg sync.WaitGroup
wg.Add(1)
task := ImportTask{}
go task.Start(&wg)
wg.Wait()
}()
}
func (s *singleton) Export() {
if s.Status != Idle {
return
}
2019-02-11 06:39:21 +00:00
s.Status = Export
go func() {
defer s.returnToIdleState()
var wg sync.WaitGroup
wg.Add(1)
task := ExportTask{}
go task.Start(&wg)
wg.Wait()
}()
}
func (s *singleton) Generate(sprites bool, previews bool, markers bool, transcodes bool) {
if s.Status != Idle {
return
}
2019-02-11 06:39:21 +00:00
s.Status = Generate
qb := models.NewSceneQueryBuilder()
//this.job.total = await ObjectionUtils.getCount(Scene);
instance.Paths.Generated.EnsureTmpDir()
go func() {
defer s.returnToIdleState()
scenes, err := qb.All()
if err != nil {
logger.Errorf("failed to get scenes for generate")
return
}
delta := utils.Btoi(sprites) + utils.Btoi(previews) + utils.Btoi(markers) + utils.Btoi(transcodes)
var wg sync.WaitGroup
for _, scene := range scenes {
2019-05-27 19:34:26 +00:00
if scene == nil {
logger.Errorf("nil scene, skipping generate")
continue
}
2019-02-11 06:39:21 +00:00
wg.Add(delta)
2019-05-04 19:18:52 +00:00
// Clear the tmp directory for each scene
if sprites || previews || markers {
instance.Paths.Generated.EmptyTmpDir()
}
2019-02-11 06:39:21 +00:00
if sprites {
2019-05-27 19:34:26 +00:00
task := GenerateSpriteTask{Scene: *scene}
2019-02-11 06:39:21 +00:00
go task.Start(&wg)
}
if previews {
2019-05-27 19:34:26 +00:00
task := GeneratePreviewTask{Scene: *scene}
2019-02-11 06:39:21 +00:00
go task.Start(&wg)
}
if markers {
2019-05-27 19:34:26 +00:00
task := GenerateMarkersTask{Scene: *scene}
2019-02-11 06:39:21 +00:00
go task.Start(&wg)
}
if transcodes {
2019-05-27 19:34:26 +00:00
task := GenerateTranscodeTask{Scene: *scene}
2019-02-11 20:12:08 +00:00
go task.Start(&wg)
2019-02-11 06:39:21 +00:00
}
wg.Wait()
}
}()
}
func (s *singleton) returnToIdleState() {
if r := recover(); r != nil {
2019-02-11 06:39:21 +00:00
logger.Info("recovered from ", r)
}
if s.Status == Generate {
instance.Paths.Generated.RemoveTmpDir()
}
s.Status = Idle
}