feat: post-download script hook#561
Conversation
📝 WalkthroughWalkthroughAdds an optional per-site asynchronous post-download script hook: new config field, execution function with validation/timeout, integration into live/VOD download flows (non-blocking with waitgroup at shutdown), tests, and README/docs. Changes
Sequence DiagramsequenceDiagram
participant Client
participant Downloader
participant FS
participant PostExecutor
participant HookScript
Client->>Downloader: start download
Downloader->>FS: write temp file
FS-->>Downloader: temp save ok
Downloader->>FS: move file to final location
FS-->>Downloader: move complete
alt post_script configured
Downloader->>PostExecutor: runPostScript(async)(scriptPath, filePath, user, site, type)
end
Downloader-->>Client: report download complete
PostExecutor->>FS: stat script & check exec perms
FS-->>PostExecutor: stat result
PostExecutor->>HookScript: exec (arg=filePath) + env STREAMDL_*
HookScript-->>PostExecutor: exit (success/failure)
PostExecutor->>PostExecutor: log result (errors logged, not propagated)
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches📝 Generate docstrings
🧪 Generate unit tests (beta)
Comment |
Pass site and postScript through the downloadStream signature and invoke runPostScript in a goroutine after a successful file move on the live stream path.
There was a problem hiding this comment.
Actionable comments posted: 1
🧹 Nitpick comments (2)
README.md (1)
153-175: Document the timeout and executability requirements.The runtime also requires the hook file to be executable and honors
STREAMDL_POST_SCRIPT_TIMEOUT, but neither detail is called out in this section. Adding both will make hook failures much easier for users to diagnose.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@README.md` around lines 153 - 175, Update the "Post-Download Script Hook" README section to state that the specified post_script file must be executable by the runtime (ensure users set executable bits, e.g., chmod +x) and that StreamDL enforces a timeout controlled by the STREAMDL_POST_SCRIPT_TIMEOUT environment variable (document the env var name and its effect on script execution and logging when the timeout is reached). Mention that if the script is not executable or times out, an error will be logged and StreamDL will continue processing other downloads.post_script_test.go (1)
10-103: Add a timeout-path regression test.These tests cover success, missing files, and non-zero exits, but the
STREAMDL_POST_SCRIPT_TIMEOUTbranch inrunPostScriptis still untested. A short timeout case with a sleeping script would protect the new cancellation logic from regressing.🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@post_script_test.go` around lines 10 - 103, Add a new unit test exercising the STREAMDL_POST_SCRIPT_TIMEOUT branch by creating a short-lived temp script that sleeps (e.g., "sleep 2") and setting the STREAMDL_POST_SCRIPT_TIMEOUT env var to a smaller value (e.g., "1s") before calling runPostScript; assert that runPostScript returns a timeout/cancellation error, and ensure you unset or restore the env var after the test and mark the script executable (refer to runPostScript, STREAMDL_POST_SCRIPT_TIMEOUT, and use t.TempDir(), os.WriteFile, os.Setenv/Unsetenv in the test).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@post_script_test.go`:
- Around line 10-103: Add a new unit test exercising the
STREAMDL_POST_SCRIPT_TIMEOUT branch by creating a short-lived temp script that
sleeps (e.g., "sleep 2") and setting the STREAMDL_POST_SCRIPT_TIMEOUT env var to
a smaller value (e.g., "1s") before calling runPostScript; assert that
runPostScript returns a timeout/cancellation error, and ensure you unset or
restore the env var after the test and mark the script executable (refer to
runPostScript, STREAMDL_POST_SCRIPT_TIMEOUT, and use t.TempDir(), os.WriteFile,
os.Setenv/Unsetenv in the test).
In `@README.md`:
- Around line 153-175: Update the "Post-Download Script Hook" README section to
state that the specified post_script file must be executable by the runtime
(ensure users set executable bits, e.g., chmod +x) and that StreamDL enforces a
timeout controlled by the STREAMDL_POST_SCRIPT_TIMEOUT environment variable
(document the env var name and its effect on script execution and logging when
the timeout is reached). Mention that if the script is not executable or times
out, an error will be logged and StreamDL will continue processing other
downloads.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: ed652bd6-7e23-4278-843d-b68b8af57f72
📒 Files selected for processing (7)
README.mdconfig.goconfig_reader_test.godownload_stream.gopost_script.gopost_script_test.gostreamdl.go
Add a marker-based hook script to the integration test that writes context to a file when fired. Both live stream and VOD phases now verify the post_script hook ran with correct env vars. Also add a manual test hook script for verifying on a live instance.
Summary
Add support for running a user-defined script after a file has been successfully downloaded and moved to its final location. This enables use cases like auto-transcribing, remuxing to different formats, and copying files to additional locations.
Closes #478
Design decisions
pre_scripttrivial later if needed.post_scriptfield in the YAML config at the site level, making it configurable per-site.Test plan