code refactoring

This commit is contained in:
2024-06-14 11:14:39 +02:00
parent 4066a6d5e3
commit 38683bfe85
2 changed files with 31 additions and 17 deletions

View File

@@ -202,14 +202,15 @@ func (p *Process) Kill() error {
// has been spawned with setPgid = true. To properly kill // has been spawned with setPgid = true. To properly kill
// all subprocesses a SIGTERM need to be sent to the correct // all subprocesses a SIGTERM need to be sent to the correct
// process group // process group
if p.proc != nil { if p.proc == nil {
pgid, err := syscall.Getpgid(p.proc.Pid) return errors.New("*os.Process not set")
if err != nil { }
return err
}
err = syscall.Kill(-pgid, syscall.SIGTERM)
p.Logger.Info("killed process", slog.String("id", p.Id)) pgid, err := syscall.Getpgid(p.proc.Pid)
if err != nil {
return err
}
if err := syscall.Kill(-pgid, syscall.SIGTERM); err != nil {
return err return err
} }
@@ -223,10 +224,7 @@ func (p *Process) GetFormatsSync() (DownloadFormats, error) {
stdout, err := cmd.Output() stdout, err := cmd.Output()
if err != nil { if err != nil {
p.Logger.Error( p.Logger.Error("failed to retrieve metadata", slog.String("err", err.Error()))
"failed to retrieve metadata",
slog.String("err", err.Error()),
)
return DownloadFormats{}, err return DownloadFormats{}, err
} }

View File

@@ -1,6 +1,7 @@
package rpc package rpc
import ( import (
"errors"
"log/slog" "log/slog"
"github.com/marcopeocchi/yt-dlp-web-ui/server/internal" "github.com/marcopeocchi/yt-dlp-web-ui/server/internal"
@@ -99,13 +100,19 @@ func (s *Service) Kill(args string, killed *string) error {
return err return err
} }
if proc != nil { if proc == nil {
err = proc.Kill() return errors.New("nil process")
s.db.Delete(proc.Id) }
if err := proc.Kill(); err != nil {
s.logger.Info("failed killing process", slog.String("id", proc.Id))
return err
} }
s.db.Delete(proc.Id) s.db.Delete(proc.Id)
return err s.logger.Info("succesfully killed process", slog.String("id", proc.Id))
return nil
} }
// KillAll kills all process unconditionally and removes them from // KillAll kills all process unconditionally and removes them from
@@ -125,8 +132,17 @@ func (s *Service) KillAll(args NoArgs, killed *string) error {
} }
if proc != nil { if proc != nil {
proc.Kill() err := proc.Kill()
s.db.Delete(proc.Id) if err != nil {
s.logger.Info(
"failed killing process",
slog.String("id", proc.Id),
slog.String("err", err.Error()),
)
return err
}
s.logger.Info("succesfully killed process", slog.String("id", proc.Id))
} }
} }