Reduce resources consumed by the hook

This commit is contained in:
Ben Sarmiento
2023-12-01 13:42:44 +01:00
parent 45ca65110e
commit b2144421cb

View File

@@ -1,7 +1,6 @@
package torrent package torrent
import ( import (
"bytes"
"fmt" "fmt"
"os/exec" "os/exec"
@@ -14,22 +13,27 @@ type ScriptExecutor struct {
Args []string Args []string
} }
func (se *ScriptExecutor) Execute() (string, error) { func (se *ScriptExecutor) Execute() error {
if se.Script == "" { if se.Script == "" {
return "", nil return nil
} }
// Prepare the command
cmd := exec.Command("/bin/sh", "-c", se.Script) cmd := exec.Command("/bin/sh", "-c", se.Script)
cmd.Args = append(cmd.Args, "zurg")
cmd.Args = append(cmd.Args, se.Args...) cmd.Args = append(cmd.Args, se.Args...)
var out bytes.Buffer
var stderr bytes.Buffer // Start the command and immediately return, not caring about its state
cmd.Stdout = &out err := cmd.Start()
cmd.Stderr = &stderr
err := cmd.Run()
if err != nil { if err != nil {
return "", fmt.Errorf("error executing script: %v; stderr: %s", err, stderr.String()) return fmt.Errorf("error starting script: %v", err)
} }
return out.String(), nil
// Detach the process
go func() {
cmd.Wait()
}()
return nil
} }
func OnLibraryUpdateHook(paths []string, config config.ConfigInterface, log *zap.SugaredLogger) { func OnLibraryUpdateHook(paths []string, config config.ConfigInterface, log *zap.SugaredLogger) {
@@ -37,12 +41,10 @@ func OnLibraryUpdateHook(paths []string, config config.ConfigInterface, log *zap
Script: config.GetOnLibraryUpdate(), Script: config.GetOnLibraryUpdate(),
Args: paths, Args: paths,
} }
output, err := executor.Execute() err := executor.Execute()
if err != nil { if err != nil {
log.Errorf("Failed to execute hook on_library_update: %v", err) log.Errorf("Failed to execute hook on_library_update: %v", err)
return return
} }
if output != "" { // No need to log the output since we're not capturing it
log.Infof("Output of hook on_library_update:\n%s", output)
}
} }