Reduce resources consumed by the hook
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
package torrent
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"os/exec"
|
||||
|
||||
@@ -14,22 +13,27 @@ type ScriptExecutor struct {
|
||||
Args []string
|
||||
}
|
||||
|
||||
func (se *ScriptExecutor) Execute() (string, error) {
|
||||
func (se *ScriptExecutor) Execute() error {
|
||||
if se.Script == "" {
|
||||
return "", nil
|
||||
return nil
|
||||
}
|
||||
|
||||
// Prepare the command
|
||||
cmd := exec.Command("/bin/sh", "-c", se.Script)
|
||||
cmd.Args = append(cmd.Args, "zurg")
|
||||
cmd.Args = append(cmd.Args, se.Args...)
|
||||
var out bytes.Buffer
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stdout = &out
|
||||
cmd.Stderr = &stderr
|
||||
err := cmd.Run()
|
||||
|
||||
// Start the command and immediately return, not caring about its state
|
||||
err := cmd.Start()
|
||||
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) {
|
||||
@@ -37,12 +41,10 @@ func OnLibraryUpdateHook(paths []string, config config.ConfigInterface, log *zap
|
||||
Script: config.GetOnLibraryUpdate(),
|
||||
Args: paths,
|
||||
}
|
||||
output, err := executor.Execute()
|
||||
err := executor.Execute()
|
||||
if err != nil {
|
||||
log.Errorf("Failed to execute hook on_library_update: %v", err)
|
||||
return
|
||||
}
|
||||
if output != "" {
|
||||
log.Infof("Output of hook on_library_update:\n%s", output)
|
||||
}
|
||||
// No need to log the output since we're not capturing it
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user