Job queueing (#1379)

This commit is contained in:
WithoutPants
2021-05-24 14:24:18 +10:00
committed by GitHub
parent 9aa2dfd96c
commit 0e01374537
42 changed files with 2571 additions and 1110 deletions

View File

@@ -1,25 +1,19 @@
package manager
import (
"time"
"context"
"fmt"
"github.com/stashapp/stash/pkg/job"
"github.com/stashapp/stash/pkg/logger"
"github.com/stashapp/stash/pkg/models"
"github.com/stashapp/stash/pkg/plugin/common"
)
func (s *singleton) RunPluginTask(pluginID string, taskName string, args []*models.PluginArgInput, serverConnection common.StashServerConnection) {
if s.Status.Status != Idle {
return
}
s.Status.SetStatus(PluginOperation)
s.Status.indefiniteProgress()
go func() {
defer s.returnToIdleState()
progress := make(chan float64)
task, err := s.PluginCache.CreateTask(pluginID, taskName, serverConnection, args, progress)
func (s *singleton) RunPluginTask(pluginID string, taskName string, args []*models.PluginArgInput, serverConnection common.StashServerConnection) int {
j := job.MakeJobExec(func(ctx context.Context, progress *job.Progress) {
pluginProgress := make(chan float64)
task, err := s.PluginCache.CreateTask(pluginID, taskName, serverConnection, args, pluginProgress)
if err != nil {
logger.Errorf("Error creating plugin task: %s", err.Error())
return
@@ -48,24 +42,20 @@ func (s *singleton) RunPluginTask(pluginID string, taskName string, args []*mode
}
}()
// TODO - refactor stop to use channels
// check for stop every five seconds
pollingTime := time.Second * 5
stopPoller := time.Tick(pollingTime)
for {
select {
case <-done:
return
case p := <-progress:
s.Status.setProgressPercent(p)
case <-stopPoller:
if s.Status.stopping {
if err := task.Stop(); err != nil {
logger.Errorf("Error stopping plugin operation: %s", err.Error())
}
return
case p := <-pluginProgress:
progress.SetPercent(p)
case <-ctx.Done():
if err := task.Stop(); err != nil {
logger.Errorf("Error stopping plugin operation: %s", err.Error())
}
return
}
}
}()
})
return s.JobManager.Add(fmt.Sprintf("Running plugin task: %s", taskName), j)
}