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
// all subprocesses a SIGTERM need to be sent to the correct
// process group
if p.proc != nil {
pgid, err := syscall.Getpgid(p.proc.Pid)
if err != nil {
return err
}
err = syscall.Kill(-pgid, syscall.SIGTERM)
if p.proc == nil {
return errors.New("*os.Process not set")
}
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
}
@@ -223,10 +224,7 @@ func (p *Process) GetFormatsSync() (DownloadFormats, error) {
stdout, err := cmd.Output()
if err != nil {
p.Logger.Error(
"failed to retrieve metadata",
slog.String("err", err.Error()),
)
p.Logger.Error("failed to retrieve metadata", slog.String("err", err.Error()))
return DownloadFormats{}, err
}

View File

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