-
Notifications
You must be signed in to change notification settings - Fork 0
feat: post-download script hook #561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
2fe9f7a
feat: post-download script hook (WIP)
biodrone 3eeb2d9
feat: add post_script field to Config struct
biodrone a17f403
feat: add runPostScript function with tests
biodrone b8b10fe
fix: add executability check to runPostScript
biodrone 88e22f3
feat: wire post_script into downloadStream
biodrone ab67aab
feat: wire post_script into downloadVOD
biodrone e1a3551
docs: add post_script hook documentation to README
biodrone 5be9793
fix: add graceful shutdown and timeout for post-script hooks
biodrone 9a81e2a
fix: use process group for post-script timeout cleanup
biodrone 52a45c3
test: add post_script hook verification to integration test
biodrone File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "os" | ||
| "os/exec" | ||
| "syscall" | ||
| "time" | ||
|
|
||
| log "github.com/sirupsen/logrus" | ||
| ) | ||
|
|
||
| // runPostScript executes a user-defined script after a successful download. | ||
| // The file path is passed as the first argument, and context is provided via | ||
| // STREAMDL_FILE, STREAMDL_USER, STREAMDL_SITE, and STREAMDL_TYPE env vars. | ||
| // Returns nil immediately if scriptPath is empty (no hook configured). | ||
| func runPostScript(scriptPath, filePath, user, site, dlType string) error { | ||
| if scriptPath == "" { | ||
| return nil | ||
| } | ||
|
|
||
| info, err := os.Stat(scriptPath) | ||
| if err != nil { | ||
| return fmt.Errorf("post_script not found: %w", err) | ||
| } | ||
| if info.Mode().Perm()&0111 == 0 { | ||
| return fmt.Errorf("post_script %s is not executable", scriptPath) | ||
| } | ||
|
|
||
| log.Infof("Running post_script %s for %s (%s)", scriptPath, user, filePath) | ||
|
|
||
| timeout := time.Duration(parseIntEnvOrDefault("STREAMDL_POST_SCRIPT_TIMEOUT", 1800)) * time.Second | ||
| ctx, cancel := context.WithTimeout(context.Background(), timeout) | ||
| defer cancel() | ||
|
|
||
| cmd := exec.CommandContext(ctx, scriptPath, filePath) | ||
| cmd.Env = append(os.Environ(), | ||
| "STREAMDL_FILE="+filePath, | ||
| "STREAMDL_USER="+user, | ||
| "STREAMDL_SITE="+site, | ||
| "STREAMDL_TYPE="+dlType, | ||
| ) | ||
| cmd.Stdout = os.Stdout | ||
| cmd.Stderr = os.Stderr | ||
| cmd.SysProcAttr = &syscall.SysProcAttr{Setpgid: true} | ||
| cmd.Cancel = func() error { | ||
| return syscall.Kill(-cmd.Process.Pid, syscall.SIGKILL) | ||
| } | ||
|
|
||
| if err := cmd.Run(); err != nil { | ||
| return fmt.Errorf("post_script %s failed: %w", scriptPath, err) | ||
| } | ||
|
|
||
| log.Infof("post_script %s completed for %s", scriptPath, user) | ||
| return nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| package main | ||
|
|
||
| import ( | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestRunPostScript_Success(t *testing.T) { | ||
| dir := t.TempDir() | ||
| marker := filepath.Join(dir, "marker.txt") | ||
|
|
||
| // Create a script that writes env vars to a marker file | ||
| script := filepath.Join(dir, "hook.sh") | ||
| scriptContent := "#!/bin/sh\necho \"$STREAMDL_FILE|$STREAMDL_USER|$STREAMDL_SITE|$STREAMDL_TYPE\" > " + marker + "\n" | ||
| if err := os.WriteFile(script, []byte(scriptContent), 0755); err != nil { | ||
| t.Fatalf("write script: %v", err) | ||
| } | ||
|
|
||
| err := runPostScript(script, "/data/complete/user_2026.mp4", "testuser", "twitch.tv", "live") | ||
| if err != nil { | ||
| t.Fatalf("runPostScript error: %v", err) | ||
| } | ||
|
|
||
| got, err := os.ReadFile(marker) | ||
| if err != nil { | ||
| t.Fatalf("read marker: %v", err) | ||
| } | ||
|
|
||
| expected := "/data/complete/user_2026.mp4|testuser|twitch.tv|live\n" | ||
| if string(got) != expected { | ||
| t.Errorf("marker content = %q, want %q", string(got), expected) | ||
| } | ||
| } | ||
|
|
||
| func TestRunPostScript_EmptyPath_Noop(t *testing.T) { | ||
| err := runPostScript("", "/data/file.mp4", "user", "twitch.tv", "live") | ||
| if err != nil { | ||
| t.Fatalf("expected nil for empty script path, got: %v", err) | ||
| } | ||
| } | ||
|
|
||
| func TestRunPostScript_MissingScript(t *testing.T) { | ||
| err := runPostScript("/nonexistent/script.sh", "/data/file.mp4", "user", "twitch.tv", "live") | ||
| if err == nil { | ||
| t.Fatal("expected error for missing script") | ||
| } | ||
| } | ||
|
|
||
| func TestRunPostScript_ScriptFails(t *testing.T) { | ||
| dir := t.TempDir() | ||
| script := filepath.Join(dir, "fail.sh") | ||
| if err := os.WriteFile(script, []byte("#!/bin/sh\nexit 1\n"), 0755); err != nil { | ||
| t.Fatalf("write script: %v", err) | ||
| } | ||
|
|
||
| err := runPostScript(script, "/data/file.mp4", "user", "twitch.tv", "vod") | ||
| if err == nil { | ||
| t.Fatal("expected error for failing script") | ||
| } | ||
| } | ||
|
|
||
| func TestRunPostScript_FilePathAsFirstArg(t *testing.T) { | ||
| dir := t.TempDir() | ||
| marker := filepath.Join(dir, "arg.txt") | ||
|
|
||
| script := filepath.Join(dir, "checkarg.sh") | ||
| scriptContent := "#!/bin/sh\necho \"$1\" > " + marker + "\n" | ||
| if err := os.WriteFile(script, []byte(scriptContent), 0755); err != nil { | ||
| t.Fatalf("write script: %v", err) | ||
| } | ||
|
|
||
| err := runPostScript(script, "/data/complete/test.mp4", "user", "twitch.tv", "live") | ||
| if err != nil { | ||
| t.Fatalf("runPostScript error: %v", err) | ||
| } | ||
|
|
||
| got, err := os.ReadFile(marker) | ||
| if err != nil { | ||
| t.Fatalf("read marker: %v", err) | ||
| } | ||
|
|
||
| if string(got) != "/data/complete/test.mp4\n" { | ||
| t.Errorf("arg content = %q, want %q", string(got), "/data/complete/test.mp4\n") | ||
| } | ||
| } | ||
|
|
||
| func TestRunPostScript_NotExecutable(t *testing.T) { | ||
| dir := t.TempDir() | ||
| script := filepath.Join(dir, "noexec.sh") | ||
| if err := os.WriteFile(script, []byte("#!/bin/sh\necho hi\n"), 0644); err != nil { | ||
| t.Fatalf("write script: %v", err) | ||
| } | ||
|
|
||
| err := runPostScript(script, "/data/file.mp4", "user", "twitch.tv", "live") | ||
| if err == nil { | ||
| t.Fatal("expected error for non-executable script") | ||
| } | ||
| if !strings.Contains(err.Error(), "not executable") { | ||
| t.Errorf("expected 'not executable' in error, got: %v", err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.