mirror of
https://github.com/stashapp/stash.git
synced 2025-12-17 20:34:37 +03:00
Close streams/encodes before deleting file
This commit is contained in:
@@ -1,11 +1,13 @@
|
||||
package ffmpeg
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"os/exec"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/stashapp/stash/pkg/logger"
|
||||
)
|
||||
@@ -14,12 +16,63 @@ type Encoder struct {
|
||||
Path string
|
||||
}
|
||||
|
||||
var runningEncoders map[string][]*os.Process = make(map[string][]*os.Process)
|
||||
|
||||
func NewEncoder(ffmpegPath string) Encoder {
|
||||
return Encoder{
|
||||
Path: ffmpegPath,
|
||||
}
|
||||
}
|
||||
|
||||
func registerRunningEncoder(path string, process *os.Process) {
|
||||
processes := runningEncoders[path]
|
||||
|
||||
runningEncoders[path] = append(processes, process)
|
||||
}
|
||||
|
||||
func deregisterRunningEncoder(path string, process *os.Process) {
|
||||
processes := runningEncoders[path]
|
||||
|
||||
for i, v := range processes {
|
||||
if v == process {
|
||||
runningEncoders[path] = append(processes[:i], processes[i+1:]...)
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func waitAndDeregister(path string, cmd *exec.Cmd) error {
|
||||
err := cmd.Wait()
|
||||
deregisterRunningEncoder(path, cmd.Process)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func KillRunningEncoders(path string) {
|
||||
processes := runningEncoders[path]
|
||||
|
||||
for _, process := range processes {
|
||||
// assume it worked, don't check for error
|
||||
fmt.Printf("Killing encoder process for file: %s", path)
|
||||
process.Kill()
|
||||
|
||||
// wait for the process to die before returning
|
||||
// don't wait more than a few seconds
|
||||
done := make(chan error)
|
||||
go func() {
|
||||
_, err := process.Wait()
|
||||
done <- err
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-done:
|
||||
return
|
||||
case <-time.After(5 * time.Second):
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (e *Encoder) run(probeResult VideoFile, args []string) (string, error) {
|
||||
cmd := exec.Command(e.Path, args...)
|
||||
|
||||
@@ -56,7 +109,10 @@ func (e *Encoder) run(probeResult VideoFile, args []string) (string, error) {
|
||||
stdoutData, _ := ioutil.ReadAll(stdout)
|
||||
stdoutString := string(stdoutData)
|
||||
|
||||
if err := cmd.Wait(); err != nil {
|
||||
registerRunningEncoder(probeResult.Path, cmd.Process)
|
||||
err = waitAndDeregister(probeResult.Path, cmd)
|
||||
|
||||
if err != nil {
|
||||
logger.Errorf("ffmpeg error when running command <%s>", strings.Join(cmd.Args, " "))
|
||||
return stdoutString, err
|
||||
}
|
||||
@@ -76,5 +132,8 @@ func (e *Encoder) stream(probeResult VideoFile, args []string) (io.ReadCloser, *
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
registerRunningEncoder(probeResult.Path, cmd.Process)
|
||||
go waitAndDeregister(probeResult.Path, cmd)
|
||||
|
||||
return stdout, cmd.Process, nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user