2021-10-22 23:48:42 +00:00
|
|
|
package manager
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/remeh/sizedwaitgroup"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/internal/manager/config"
|
2021-10-22 23:48:42 +00:00
|
|
|
"github.com/stashapp/stash/pkg/job"
|
|
|
|
"github.com/stashapp/stash/pkg/logger"
|
|
|
|
"github.com/stashapp/stash/pkg/models"
|
2021-10-25 00:40:13 +00:00
|
|
|
"github.com/stashapp/stash/pkg/scene"
|
2022-04-18 00:50:10 +00:00
|
|
|
"github.com/stashapp/stash/pkg/scene/generate"
|
2022-03-17 00:33:59 +00:00
|
|
|
"github.com/stashapp/stash/pkg/sliceutil/stringslice"
|
2021-10-22 23:48:42 +00:00
|
|
|
"github.com/stashapp/stash/pkg/utils"
|
|
|
|
)
|
|
|
|
|
|
|
|
const generateQueueSize = 200000
|
|
|
|
|
|
|
|
type GenerateJob struct {
|
|
|
|
txnManager models.TransactionManager
|
|
|
|
input models.GenerateMetadataInput
|
|
|
|
|
|
|
|
overwrite bool
|
|
|
|
fileNamingAlgo models.HashAlgorithm
|
|
|
|
}
|
|
|
|
|
|
|
|
type totalsGenerate struct {
|
2021-12-13 02:41:07 +00:00
|
|
|
sprites int64
|
|
|
|
previews int64
|
|
|
|
imagePreviews int64
|
|
|
|
markers int64
|
|
|
|
transcodes int64
|
|
|
|
phashes int64
|
|
|
|
interactiveHeatmapSpeeds int64
|
2021-10-22 23:48:42 +00:00
|
|
|
|
|
|
|
tasks int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *GenerateJob) Execute(ctx context.Context, progress *job.Progress) {
|
|
|
|
var scenes []*models.Scene
|
|
|
|
var err error
|
|
|
|
var markers []*models.SceneMarker
|
|
|
|
|
|
|
|
if j.input.Overwrite != nil {
|
|
|
|
j.overwrite = *j.input.Overwrite
|
|
|
|
}
|
|
|
|
j.fileNamingAlgo = config.GetInstance().GetVideoFileNamingAlgorithm()
|
|
|
|
|
|
|
|
config := config.GetInstance()
|
|
|
|
parallelTasks := config.GetParallelTasksWithAutoDetection()
|
|
|
|
|
|
|
|
logger.Infof("Generate started with %d parallel tasks", parallelTasks)
|
|
|
|
|
|
|
|
queue := make(chan Task, generateQueueSize)
|
|
|
|
go func() {
|
2021-10-25 21:41:40 +00:00
|
|
|
defer close(queue)
|
|
|
|
|
2021-10-22 23:48:42 +00:00
|
|
|
var totals totalsGenerate
|
2022-03-17 00:33:59 +00:00
|
|
|
sceneIDs, err := stringslice.StringSliceToIntSlice(j.input.SceneIDs)
|
2021-10-22 23:48:42 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
2022-03-17 00:33:59 +00:00
|
|
|
markerIDs, err := stringslice.StringSliceToIntSlice(j.input.MarkerIDs)
|
2021-10-22 23:48:42 +00:00
|
|
|
if err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
g := &generate.Generator{
|
|
|
|
Encoder: instance.FFMPEG,
|
|
|
|
LockManager: instance.ReadLockManager,
|
|
|
|
MarkerPaths: instance.Paths.SceneMarkers,
|
|
|
|
ScenePaths: instance.Paths.Scene,
|
|
|
|
Overwrite: j.overwrite,
|
|
|
|
}
|
|
|
|
|
2021-10-22 23:48:42 +00:00
|
|
|
if err := j.txnManager.WithReadTxn(ctx, func(r models.ReaderRepository) error {
|
|
|
|
qb := r.Scene()
|
|
|
|
if len(j.input.SceneIDs) == 0 && len(j.input.MarkerIDs) == 0 {
|
2022-04-18 00:50:10 +00:00
|
|
|
totals = j.queueTasks(ctx, g, queue)
|
2021-10-22 23:48:42 +00:00
|
|
|
} else {
|
|
|
|
if len(j.input.SceneIDs) > 0 {
|
|
|
|
scenes, err = qb.FindMany(sceneIDs)
|
|
|
|
for _, s := range scenes {
|
2022-04-18 00:50:10 +00:00
|
|
|
j.queueSceneJobs(ctx, g, s, queue, &totals)
|
2021-10-22 23:48:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(j.input.MarkerIDs) > 0 {
|
|
|
|
markers, err = r.SceneMarker().FindMany(markerIDs)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, m := range markers {
|
2022-04-18 00:50:10 +00:00
|
|
|
j.queueMarkerJob(g, m, queue, &totals)
|
2021-10-22 23:48:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
logger.Error(err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-12-13 02:41:07 +00:00
|
|
|
logger.Infof("Generating %d sprites %d previews %d image previews %d markers %d transcodes %d phashes %d heatmaps & speeds", totals.sprites, totals.previews, totals.imagePreviews, totals.markers, totals.transcodes, totals.phashes, totals.interactiveHeatmapSpeeds)
|
2021-10-22 23:48:42 +00:00
|
|
|
|
|
|
|
progress.SetTotal(int(totals.tasks))
|
|
|
|
}()
|
|
|
|
|
|
|
|
wg := sizedwaitgroup.New(parallelTasks)
|
|
|
|
|
|
|
|
// Start measuring how long the generate has taken. (consider moving this up)
|
|
|
|
start := time.Now()
|
|
|
|
if err = instance.Paths.Generated.EnsureTmpDir(); err != nil {
|
|
|
|
logger.Warnf("could not create temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := instance.Paths.Generated.EmptyTmpDir(); err != nil {
|
|
|
|
logger.Warnf("failure emptying temporary directory: %v", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for f := range queue {
|
|
|
|
if job.IsCancelled(ctx) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Add()
|
2021-10-25 21:41:40 +00:00
|
|
|
// #1879 - need to make a copy of f - otherwise there is a race condition
|
|
|
|
// where f is changed when the goroutine runs
|
|
|
|
localTask := f
|
|
|
|
go progress.ExecuteTask(localTask.GetDescription(), func() {
|
|
|
|
localTask.Start(ctx)
|
2021-10-22 23:48:42 +00:00
|
|
|
wg.Done()
|
|
|
|
progress.Increment()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
if job.IsCancelled(ctx) {
|
|
|
|
logger.Info("Stopping due to user request")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
elapsed := time.Since(start)
|
|
|
|
logger.Info(fmt.Sprintf("Generate finished (%s)", elapsed))
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
func (j *GenerateJob) queueTasks(ctx context.Context, g *generate.Generator, queue chan<- Task) totalsGenerate {
|
2021-10-22 23:48:42 +00:00
|
|
|
var totals totalsGenerate
|
|
|
|
|
|
|
|
const batchSize = 1000
|
|
|
|
|
|
|
|
findFilter := models.BatchFindFilter(batchSize)
|
|
|
|
|
|
|
|
if err := j.txnManager.WithReadTxn(ctx, func(r models.ReaderRepository) error {
|
|
|
|
for more := true; more; {
|
|
|
|
if job.IsCancelled(ctx) {
|
|
|
|
return context.Canceled
|
|
|
|
}
|
|
|
|
|
2021-10-25 00:40:13 +00:00
|
|
|
scenes, err := scene.Query(r.Scene(), nil, findFilter)
|
2021-10-22 23:48:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ss := range scenes {
|
|
|
|
if job.IsCancelled(ctx) {
|
|
|
|
return context.Canceled
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
j.queueSceneJobs(ctx, g, ss, queue, &totals)
|
2021-10-22 23:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(scenes) != batchSize {
|
|
|
|
more = false
|
|
|
|
} else {
|
|
|
|
*findFilter.Page++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
if !errors.Is(err, context.Canceled) {
|
|
|
|
logger.Errorf("Error encountered queuing files to scan: %s", err.Error())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return totals
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
func getGeneratePreviewOptions(optionsInput models.GeneratePreviewOptionsInput) generate.PreviewOptions {
|
|
|
|
config := config.GetInstance()
|
|
|
|
|
|
|
|
ret := generate.PreviewOptions{
|
|
|
|
Segments: config.GetPreviewSegments(),
|
|
|
|
SegmentDuration: config.GetPreviewSegmentDuration(),
|
|
|
|
ExcludeStart: config.GetPreviewExcludeStart(),
|
|
|
|
ExcludeEnd: config.GetPreviewExcludeEnd(),
|
|
|
|
Preset: config.GetPreviewPreset().String(),
|
|
|
|
Audio: config.GetPreviewAudio(),
|
|
|
|
}
|
|
|
|
|
|
|
|
if optionsInput.PreviewSegments != nil {
|
|
|
|
ret.Segments = *optionsInput.PreviewSegments
|
|
|
|
}
|
|
|
|
|
|
|
|
if optionsInput.PreviewSegmentDuration != nil {
|
|
|
|
ret.SegmentDuration = *optionsInput.PreviewSegmentDuration
|
|
|
|
}
|
|
|
|
|
|
|
|
if optionsInput.PreviewExcludeStart != nil {
|
|
|
|
ret.ExcludeStart = *optionsInput.PreviewExcludeStart
|
|
|
|
}
|
|
|
|
|
|
|
|
if optionsInput.PreviewExcludeEnd != nil {
|
|
|
|
ret.ExcludeEnd = *optionsInput.PreviewExcludeEnd
|
|
|
|
}
|
|
|
|
|
|
|
|
if optionsInput.PreviewPreset != nil {
|
|
|
|
ret.Preset = optionsInput.PreviewPreset.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *GenerateJob) queueSceneJobs(ctx context.Context, g *generate.Generator, scene *models.Scene, queue chan<- Task, totals *totalsGenerate) {
|
2021-10-22 23:48:42 +00:00
|
|
|
if utils.IsTrue(j.input.Sprites) {
|
|
|
|
task := &GenerateSpriteTask{
|
|
|
|
Scene: *scene,
|
|
|
|
Overwrite: j.overwrite,
|
|
|
|
fileNamingAlgorithm: j.fileNamingAlgo,
|
|
|
|
}
|
|
|
|
|
|
|
|
if j.overwrite || task.required() {
|
|
|
|
totals.sprites++
|
|
|
|
totals.tasks++
|
|
|
|
queue <- task
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
generatePreviewOptions := j.input.PreviewOptions
|
|
|
|
if generatePreviewOptions == nil {
|
|
|
|
generatePreviewOptions = &models.GeneratePreviewOptionsInput{}
|
|
|
|
}
|
|
|
|
options := getGeneratePreviewOptions(*generatePreviewOptions)
|
|
|
|
|
2021-10-22 23:48:42 +00:00
|
|
|
if utils.IsTrue(j.input.Previews) {
|
|
|
|
|
|
|
|
task := &GeneratePreviewTask{
|
|
|
|
Scene: *scene,
|
|
|
|
ImagePreview: utils.IsTrue(j.input.ImagePreviews),
|
2022-04-18 00:50:10 +00:00
|
|
|
Options: options,
|
2021-10-22 23:48:42 +00:00
|
|
|
Overwrite: j.overwrite,
|
|
|
|
fileNamingAlgorithm: j.fileNamingAlgo,
|
2022-04-18 00:50:10 +00:00
|
|
|
generator: g,
|
2021-10-22 23:48:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sceneHash := scene.GetHash(task.fileNamingAlgorithm)
|
|
|
|
addTask := false
|
|
|
|
if j.overwrite || !task.doesVideoPreviewExist(sceneHash) {
|
|
|
|
totals.previews++
|
|
|
|
addTask = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if utils.IsTrue(j.input.ImagePreviews) && (j.overwrite || !task.doesImagePreviewExist(sceneHash)) {
|
|
|
|
totals.imagePreviews++
|
|
|
|
addTask = true
|
|
|
|
}
|
|
|
|
|
|
|
|
if addTask {
|
|
|
|
totals.tasks++
|
|
|
|
queue <- task
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if utils.IsTrue(j.input.Markers) {
|
|
|
|
task := &GenerateMarkersTask{
|
|
|
|
TxnManager: j.txnManager,
|
|
|
|
Scene: scene,
|
|
|
|
Overwrite: j.overwrite,
|
|
|
|
fileNamingAlgorithm: j.fileNamingAlgo,
|
|
|
|
ImagePreview: utils.IsTrue(j.input.MarkerImagePreviews),
|
|
|
|
Screenshot: utils.IsTrue(j.input.MarkerScreenshots),
|
2022-04-18 00:50:10 +00:00
|
|
|
|
|
|
|
generator: g,
|
2021-10-22 23:48:42 +00:00
|
|
|
}
|
|
|
|
|
Hoist context, enable errchkjson (#2488)
* Make the script scraper context-aware
Connect the context to the command execution. This means command
execution can be aborted if the context is canceled. The context is
usually bound to user-interaction, i.e., a scraper operation issued
by the user. Hence, it seems correct to abort a command if the user
aborts.
* Enable errchkjson
Some json marshal calls are *safe* in that they can never fail. This is
conditional on the types of the the data being encoded. errchkjson finds
those calls which are unsafe, and also not checked for errors.
Add logging warnings to the place where unsafe encodings might happen.
This can help uncover usage bugs early in stash if they are tripped,
making debugging easier.
While here, keep the checker enabled in the linter to capture future
uses of json marshalling.
* Pass the context for zip file scanning.
* Pass the context in scanning
* Pass context, replace context.TODO()
Where applicable, pass the context down toward the lower functions in
the call stack. Replace uses of context.TODO() with the passed context.
This makes the code more context-aware, and you can rely on aborting
contexts to clean up subsystems to a far greater extent now.
I've left the cases where there is a context in a struct. My gut feeling
is that they have solutions that are nice, but they require more deep
thinking to unveil how to handle it.
* Remove context from task-structs
As a rule, contexts are better passed explicitly to functions than they
are passed implicitly via structs. In the case of tasks, we already
have a valid context in scope when creating the struct, so remove ctx
from the struct and use the scoped context instead.
With this change it is clear that the scanning functions are under a
context, and the task-starting caller has jurisdiction over the context
and its lifetime. A reader of the code don't have to figure out where
the context are coming from anymore.
While here, connect context.TODO() to the newly scoped context in most
of the scan code.
* Remove context from autotag struct too
* Make more context-passing explicit
In all of these cases, there is an applicable context which is close
in the call-tree. Hook up to this context.
* Simplify context passing in manager
The managers context handling generally wants to use an outer context
if applicable. However, the code doesn't pass it explicitly, but stores
it in a struct. Pull out the context from the struct and use it to
explicitly pass it.
At a later point in time, we probably want to handle this by handing
over the job to a different (program-lifetime) context for background
jobs, but this will do for a start.
2022-04-15 01:34:53 +00:00
|
|
|
markers := task.markersNeeded(ctx)
|
2021-10-22 23:48:42 +00:00
|
|
|
if markers > 0 {
|
|
|
|
totals.markers += int64(markers)
|
|
|
|
totals.tasks++
|
|
|
|
|
|
|
|
queue <- task
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if utils.IsTrue(j.input.Transcodes) {
|
2021-12-20 00:45:36 +00:00
|
|
|
forceTranscode := utils.IsTrue(j.input.ForceTranscodes)
|
2021-10-22 23:48:42 +00:00
|
|
|
task := &GenerateTranscodeTask{
|
|
|
|
Scene: *scene,
|
|
|
|
Overwrite: j.overwrite,
|
2021-12-20 00:45:36 +00:00
|
|
|
Force: forceTranscode,
|
2021-10-22 23:48:42 +00:00
|
|
|
fileNamingAlgorithm: j.fileNamingAlgo,
|
2022-04-18 00:50:10 +00:00
|
|
|
g: g,
|
2021-10-22 23:48:42 +00:00
|
|
|
}
|
|
|
|
if task.isTranscodeNeeded() {
|
|
|
|
totals.transcodes++
|
|
|
|
totals.tasks++
|
|
|
|
queue <- task
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if utils.IsTrue(j.input.Phashes) {
|
|
|
|
task := &GeneratePhashTask{
|
|
|
|
Scene: *scene,
|
|
|
|
fileNamingAlgorithm: j.fileNamingAlgo,
|
|
|
|
txnManager: j.txnManager,
|
|
|
|
Overwrite: j.overwrite,
|
|
|
|
}
|
|
|
|
|
|
|
|
if task.shouldGenerate() {
|
|
|
|
totals.phashes++
|
|
|
|
totals.tasks++
|
|
|
|
queue <- task
|
|
|
|
}
|
|
|
|
}
|
2021-12-13 02:41:07 +00:00
|
|
|
|
|
|
|
if utils.IsTrue(j.input.InteractiveHeatmapsSpeeds) {
|
|
|
|
task := &GenerateInteractiveHeatmapSpeedTask{
|
|
|
|
Scene: *scene,
|
|
|
|
Overwrite: j.overwrite,
|
|
|
|
fileNamingAlgorithm: j.fileNamingAlgo,
|
|
|
|
TxnManager: j.txnManager,
|
|
|
|
}
|
|
|
|
|
|
|
|
if task.shouldGenerate() {
|
|
|
|
totals.interactiveHeatmapSpeeds++
|
|
|
|
totals.tasks++
|
|
|
|
queue <- task
|
|
|
|
}
|
|
|
|
}
|
2021-10-22 23:48:42 +00:00
|
|
|
}
|
|
|
|
|
2022-04-18 00:50:10 +00:00
|
|
|
func (j *GenerateJob) queueMarkerJob(g *generate.Generator, marker *models.SceneMarker, queue chan<- Task, totals *totalsGenerate) {
|
2021-10-22 23:48:42 +00:00
|
|
|
task := &GenerateMarkersTask{
|
|
|
|
TxnManager: j.txnManager,
|
|
|
|
Marker: marker,
|
|
|
|
Overwrite: j.overwrite,
|
|
|
|
fileNamingAlgorithm: j.fileNamingAlgo,
|
2022-04-18 00:50:10 +00:00
|
|
|
generator: g,
|
2021-10-22 23:48:42 +00:00
|
|
|
}
|
|
|
|
totals.markers++
|
|
|
|
totals.tasks++
|
|
|
|
queue <- task
|
|
|
|
}
|