feat: Status event hooks (onSuccess, onFailed, onKilled) #31

Open
opened 2026-10-04 19:28:38 +00:00 by daniel · 0 comments
Owner

Imported from GitHub samson-media/barn#31 — opened by @daniel-samson on 2026-01-22 10:32:30 UTC

Overview

Add lifecycle hooks that execute commands when a job changes state. This enables job chaining, webhooks, and automated workflows.

Proposed CLI

# Chain jobs - transcode after download succeeds
barn run --on-success="barn run -- ffmpeg -i input.mkv output.mp4" -- curl -o input.mkv $URL

# Notify on failure
barn run --on-failed="curl -X POST https://webhook.site/notify -d 'Job failed'" -- ffmpeg ...

# Cleanup on kill
barn run --on-killed="rm -f /tmp/partial.*" -- long-running-job.sh

# Log when job starts
barn run --on-start="curl -X POST $WEBHOOK -d 'Job started'" -- ffmpeg ...

# Notify on any completion
barn run --on-complete="curl -X POST $WEBHOOK -d 'Job finished'" -- ffmpeg ...

# Multiple hooks
barn run \
  --on-start="echo 'Starting transcode'" \
  --on-success="barn run -- upload.sh" \
  --on-failed="curl -X POST $SLACK_WEBHOOK -d 'Transcode failed'" \
  -- ffmpeg -i input.mkv output.mp4

Hooks

Hook Triggers When
--on-start Job transitions to running state
--on-success Job exits with code 0 (succeeded state)
--on-failed Job exits with non-zero code (failed state)
--on-killed Job is killed by user (killed state)
--on-canceled Job is canceled before starting (canceled state)
--on-skipped Job is skipped due to dependency failure (skipped state) - see #32
--on-complete Job reaches any terminal state (succeeded, failed, killed, canceled, skipped)

Implementation Notes

Manifest Changes

Add optional hook fields to JobManifest:

public record JobManifest(
    String id,
    List<String> command,
    String tag,
    Instant createdAt,
    int maxRetries,
    int retryDelaySeconds,
    double retryBackoffMultiplier,
    String onStart,      // new
    String onSuccess,    // new
    String onFailed,     // new
    String onKilled,     // new
    String onCanceled,   // new
    String onSkipped,    // new
    String onComplete    // new
) { }

Execution

In JobRunner and JobRepository, when job state changes:

  1. Check if relevant hook exists
  2. Execute hook command via shell (sh -c on Unix, cmd /c on Windows)
  3. Hook execution is fire-and-forget (don't block, don't retry)
  4. Log hook execution result but don't affect parent job state

Environment Variables

Pass job context to hook via environment:

Variable Value
BARN_JOB_ID The job ID
BARN_JOB_STATE running, succeeded, failed, killed, canceled, or skipped
BARN_JOB_EXIT_CODE Exit code (if applicable)
BARN_JOB_DIR Path to job directory
BARN_JOB_TAG Job tag (if set)

Example Use Cases

1. Job Chaining (Download → Transcode → Upload)

barn run \
  --on-success="barn run --on-success='barn run -- rclone copy output.mp4 remote:' -- ffmpeg -i input.mkv output.mp4" \
  -- curl -o input.mkv $URL

2. Slack Notification on Failure

barn run \
  --on-failed="curl -X POST $SLACK_WEBHOOK -H 'Content-Type: application/json' -d '{\"text\":\"Job '\$BARN_JOB_ID' failed\"}'" \
  -- ffmpeg -i input.mkv output.mp4

3. Webhook on Start and Complete

barn run \
  --on-start="curl -X POST $WEBHOOK -d 'state=started&job=\$BARN_JOB_ID'" \
  --on-complete="curl -X POST $WEBHOOK -d 'state=\$BARN_JOB_STATE&job=\$BARN_JOB_ID'" \
  -- ffmpeg -i input.mkv output.mp4

4. Cleanup Temp Files on Any Termination

barn run \
  --on-complete="rm -f /tmp/input.mkv" \
  -- ffmpeg -i /tmp/input.mkv output.mp4

Future Considerations

  • Retry logic for webhook delivery
  • Hook timeout configuration
  • Store hook output in job logs
  • Config file defaults for hooks (e.g., always notify on failure)

Related

  • #32 (Job dependencies) - introduces skipped state, complementary feature for job chaining

Acceptance Criteria

  • --on-start executes when job starts running
  • --on-success executes when job succeeds
  • --on-failed executes when job fails
  • --on-killed executes when job is killed
  • --on-canceled executes when job is canceled
  • --on-skipped executes when job is skipped (dependency failure)
  • --on-complete executes on any terminal state
  • Hook commands receive job context via environment variables
  • Hooks are stored in job manifest (visible in barn describe)
  • Hook execution is logged
  • Hook failure does not affect parent job state
  • Works on Windows, macOS, and Linux
  • Documentation updated
  • mvn verify passes
<!-- github-import:issue:31 --> _Imported from GitHub samson-media/barn#31 — opened by **@daniel-samson** on 2026-01-22 10:32:30 UTC_ ## Overview Add lifecycle hooks that execute commands when a job changes state. This enables job chaining, webhooks, and automated workflows. ## Proposed CLI ```bash # Chain jobs - transcode after download succeeds barn run --on-success="barn run -- ffmpeg -i input.mkv output.mp4" -- curl -o input.mkv $URL # Notify on failure barn run --on-failed="curl -X POST https://webhook.site/notify -d 'Job failed'" -- ffmpeg ... # Cleanup on kill barn run --on-killed="rm -f /tmp/partial.*" -- long-running-job.sh # Log when job starts barn run --on-start="curl -X POST $WEBHOOK -d 'Job started'" -- ffmpeg ... # Notify on any completion barn run --on-complete="curl -X POST $WEBHOOK -d 'Job finished'" -- ffmpeg ... # Multiple hooks barn run \ --on-start="echo 'Starting transcode'" \ --on-success="barn run -- upload.sh" \ --on-failed="curl -X POST $SLACK_WEBHOOK -d 'Transcode failed'" \ -- ffmpeg -i input.mkv output.mp4 ``` ## Hooks | Hook | Triggers When | |------|---------------| | `--on-start` | Job transitions to `running` state | | `--on-success` | Job exits with code 0 (`succeeded` state) | | `--on-failed` | Job exits with non-zero code (`failed` state) | | `--on-killed` | Job is killed by user (`killed` state) | | `--on-canceled` | Job is canceled before starting (`canceled` state) | | `--on-skipped` | Job is skipped due to dependency failure (`skipped` state) - see #32 | | `--on-complete` | Job reaches any terminal state (succeeded, failed, killed, canceled, skipped) | ## Implementation Notes ### Manifest Changes Add optional hook fields to `JobManifest`: ```java public record JobManifest( String id, List<String> command, String tag, Instant createdAt, int maxRetries, int retryDelaySeconds, double retryBackoffMultiplier, String onStart, // new String onSuccess, // new String onFailed, // new String onKilled, // new String onCanceled, // new String onSkipped, // new String onComplete // new ) { } ``` ### Execution In `JobRunner` and `JobRepository`, when job state changes: 1. Check if relevant hook exists 2. Execute hook command via shell (`sh -c` on Unix, `cmd /c` on Windows) 3. Hook execution is fire-and-forget (don't block, don't retry) 4. Log hook execution result but don't affect parent job state ### Environment Variables Pass job context to hook via environment: | Variable | Value | |----------|-------| | `BARN_JOB_ID` | The job ID | | `BARN_JOB_STATE` | `running`, `succeeded`, `failed`, `killed`, `canceled`, or `skipped` | | `BARN_JOB_EXIT_CODE` | Exit code (if applicable) | | `BARN_JOB_DIR` | Path to job directory | | `BARN_JOB_TAG` | Job tag (if set) | ### Example Use Cases **1. Job Chaining (Download → Transcode → Upload)** ```bash barn run \ --on-success="barn run --on-success='barn run -- rclone copy output.mp4 remote:' -- ffmpeg -i input.mkv output.mp4" \ -- curl -o input.mkv $URL ``` **2. Slack Notification on Failure** ```bash barn run \ --on-failed="curl -X POST $SLACK_WEBHOOK -H 'Content-Type: application/json' -d '{\"text\":\"Job '\$BARN_JOB_ID' failed\"}'" \ -- ffmpeg -i input.mkv output.mp4 ``` **3. Webhook on Start and Complete** ```bash barn run \ --on-start="curl -X POST $WEBHOOK -d 'state=started&job=\$BARN_JOB_ID'" \ --on-complete="curl -X POST $WEBHOOK -d 'state=\$BARN_JOB_STATE&job=\$BARN_JOB_ID'" \ -- ffmpeg -i input.mkv output.mp4 ``` **4. Cleanup Temp Files on Any Termination** ```bash barn run \ --on-complete="rm -f /tmp/input.mkv" \ -- ffmpeg -i /tmp/input.mkv output.mp4 ``` ## Future Considerations - Retry logic for webhook delivery - Hook timeout configuration - Store hook output in job logs - Config file defaults for hooks (e.g., always notify on failure) ## Related - #32 (Job dependencies) - introduces `skipped` state, complementary feature for job chaining ## Acceptance Criteria - [ ] `--on-start` executes when job starts running - [ ] `--on-success` executes when job succeeds - [ ] `--on-failed` executes when job fails - [ ] `--on-killed` executes when job is killed - [ ] `--on-canceled` executes when job is canceled - [ ] `--on-skipped` executes when job is skipped (dependency failure) - [ ] `--on-complete` executes on any terminal state - [ ] Hook commands receive job context via environment variables - [ ] Hooks are stored in job manifest (visible in `barn describe`) - [ ] Hook execution is logged - [ ] Hook failure does not affect parent job state - [ ] Works on Windows, macOS, and Linux - [ ] Documentation updated - [ ] `mvn verify` passes
daniel added the executionenhancement labels 2026-10-04 19:28:38 +00:00
This repo is archived. You cannot comment on issues.