From fc38a1b20c5ece8f54c2e9da278c75182a868e6b Mon Sep 17 00:00:00 2001 From: Leonid Nikitin Date: Sat, 20 Jan 2024 14:58:16 +0600 Subject: [PATCH] Fix after closing the program so that ffmpeg would also stop if it was running. --- src/convertor/service.go | 20 ++++++++++++++++++-- src/main.go | 11 +++++++++-- 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/src/convertor/service.go b/src/convertor/service.go index bb313fa..56e1b8d 100644 --- a/src/convertor/service.go +++ b/src/convertor/service.go @@ -17,6 +17,7 @@ type ServiceContract interface { GetFFprobeVersion() (string, error) ChangeFFmpegPath(path string) (bool, error) ChangeFFprobePath(path string) (bool, error) + GetRunningProcesses() map[int]*exec.Cmd } type ProgressContract interface { @@ -29,8 +30,14 @@ type FFPathUtilities struct { FFprobe string } +type runningProcesses struct { + items map[int]*exec.Cmd + numberOfStarts int +} + type Service struct { - ffPathUtilities *FFPathUtilities + ffPathUtilities *FFPathUtilities + runningProcesses runningProcesses } type File struct { @@ -51,7 +58,8 @@ type ConvertData struct { func NewService(ffPathUtilities FFPathUtilities) *Service { return &Service{ - ffPathUtilities: &ffPathUtilities, + ffPathUtilities: &ffPathUtilities, + runningProcesses: runningProcesses{items: map[int]*exec.Cmd{}, numberOfStarts: 0}, } } @@ -77,10 +85,14 @@ func (s Service) RunConvert(setting ConvertSetting, progress ProgressContract) e if err != nil { return err } + index := s.runningProcesses.numberOfStarts + s.runningProcesses.numberOfStarts++ + s.runningProcesses.items[index] = cmd errProgress := progress.Run(stdOut, stdErr) err = cmd.Wait() + delete(s.runningProcesses.items, index) if errProgress != nil { return errProgress } @@ -155,3 +167,7 @@ func (s Service) ChangeFFprobePath(path string) (bool, error) { s.ffPathUtilities.FFprobe = path return true, nil } + +func (s Service) GetRunningProcesses() map[int]*exec.Cmd { + return s.runningProcesses.items +} diff --git a/src/main.go b/src/main.go index e4955c5..46aec63 100644 --- a/src/main.go +++ b/src/main.go @@ -38,7 +38,7 @@ func main() { return } - defer appClose(db) + defer appCloseWithDb(db) err = migration.Run(db) if err != nil { @@ -66,6 +66,7 @@ func main() { convertorView := convertor.NewView(w) settingView := setting.NewView(w) convertorService := convertor.NewService(ffPathUtilities) + defer appCloseWithConvert(convertorService) mainHandler := handler.NewConvertorHandler(convertorService, convertorView, settingView, settingRepository) mainHandler.GetConvertor() @@ -73,13 +74,19 @@ func main() { w.ShowAndRun() } -func appClose(db *gorm.DB) { +func appCloseWithDb(db *gorm.DB) { sqlDB, err := db.DB() if err == nil { _ = sqlDB.Close() } } +func appCloseWithConvert(convertorService convertor.ServiceContract) { + for _, cmd := range convertorService.GetRunningProcesses() { + _ = cmd.Process.Kill() + } +} + func canCreateFile(path string) bool { file, err := os.OpenFile(path, os.O_CREATE|os.O_WRONLY, 0666) if err != nil {