Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
210 lines
7.6 KiB
Markdown
210 lines
7.6 KiB
Markdown
# Zag Job Daemon (Cross-Platform)
|
|
|
|
> **Status:** early development. Zag is the [Zig](https://ziglang.org) port of
|
|
> [barn](https://gitea.samson.media/samson-media/barn), the Java job daemon. The goal is
|
|
> CLI and on-disk compatibility with barn so the two can be swapped without
|
|
> changing clients.
|
|
|
|
A **cross-platform service** for managing long-running jobs that involve
|
|
downloading and uploading media via WebDAV, using FFmpeg to analyse and transcode video, with strong guarantees around durability,
|
|
observability, and crash recovery.
|
|
|
|
This project is designed to run as a **real OS service / daemon** on:
|
|
|
|
- Windows (Windows Service)
|
|
- macOS (launchd)
|
|
- Linux (systemd)
|
|
|
|
The daemon is **not HTTP-bound**.
|
|
It runs independently in the background and exposes control via a local CLI / RPC interface.
|
|
|
|
---
|
|
|
|
## Why This Exists
|
|
|
|
Running FFmpeg jobs over SSH or ad-hoc scripts quickly breaks down when you need:
|
|
|
|
- Multiple concurrent jobs
|
|
- Reliable handling of failures
|
|
- Recovery after crashes or restarts
|
|
- Cross-platform support
|
|
- Programmatic job inspection and control
|
|
|
|
This project treats FFmpeg jobs as **first-class managed units of work**, not shell commands.
|
|
|
|
## Why Zig
|
|
|
|
Barn needs a JVM on every host that runs it. Zag aims to keep the same behaviour as a
|
|
single small, statically linked native binary, with no runtime to install, fast startup,
|
|
a low idle memory footprint, and easy cross-compilation to every supported platform.
|
|
|
|
---
|
|
|
|
## Core Goals
|
|
|
|
- Run FFmpeg jobs reliably in the background
|
|
- Survive SSH disconnects, restarts, and crashes
|
|
- Explicitly model job state and failure reasons
|
|
- Provide durable, inspectable job state
|
|
- Work consistently on Windows, Linux, and macOS
|
|
- Avoid coupling to HTTP or web frameworks
|
|
- Stay drop-in compatible with barn's CLI, JSON/XML output, and job directory layout
|
|
|
|
---
|
|
|
|
## Building
|
|
|
|
Requires Zig 0.16.0 or newer.
|
|
|
|
```bash
|
|
zig build # build to zig-out/bin/zag
|
|
zig build test # run tests
|
|
zig build -Doptimize=ReleaseSafe # release build
|
|
```
|
|
|
|
Cross-compile with `-Dtarget`, for example:
|
|
|
|
```bash
|
|
zig build -Doptimize=ReleaseSafe -Dtarget=aarch64-macos
|
|
zig build -Doptimize=ReleaseSafe -Dtarget=x86_64-windows
|
|
zig build -Doptimize=ReleaseSafe -Dtarget=aarch64-linux
|
|
```
|
|
|
|
---
|
|
|
|
## Zag command
|
|
|
|
This project creates a simple CLI commandlet with sub-commands.
|
|
|
|
- `zag service {start|stop|restart|reload|status|logs}` - manages the zag service
|
|
- `zag run {command}` - starts job
|
|
- `zag status` - gets the status of all jobs running
|
|
- `zag describe {jobId}` - collects all the info from /tmp and display it
|
|
- `zag usage {jobId}` - shows resource usage (CPU, memory, disk) for a job
|
|
- `zag kill {jobId}` - kills a job
|
|
- `zag clean` - performs clean up
|
|
- `zag config install` - installs default configuration files
|
|
- `zag config show` - shows effective configuration
|
|
|
|
zag is just one binary for both the service and the client. The client uses IPC to communicate with the service.
|
|
|
|
Clients typically access zag over ssh:
|
|
- fetch from webdav server: `zag run --output=json curl -u username:password -o /tmp/zag/input/video.mp4 https://example.com/dav/remote-file.ext`
|
|
- analyse file with ffmpeg: `zag run --output=json ffprobe -i /tmp/zag/input/video.mp4`
|
|
- get status of the jobs `zag status --output=json`
|
|
|
|
zag's output defaults to a human-readable format, but also supports xml or json via the `--output={json|xml}` cli option.
|
|
|
|
### Managing the service
|
|
|
|
There needs to be a zag service in order to run jobs (unless you use `--offline`). You manage the service using the `zag service` sub command.
|
|
- `zag service status` - check the status of the service. up/down, process id etc.
|
|
- `zag service start` - starts zag service process
|
|
- `zag service stop` - stops/kills the zag service
|
|
- `zag service restart` - restarts the zag service
|
|
- `zag service reload` - reloads the configuration and soft restarts the zag service
|
|
|
|
### Adding jobs
|
|
|
|
`zag run --output=json --tag=downloading <cmd>` takes in a command to run, starts a new process/thread, outputs the status of that job:
|
|
|
|
| Field | Description |
|
|
|---------------|--------------------------------------------------------|
|
|
| `job_id` | the id of the job |
|
|
| `state` | `queued`, `running`, `succeeded`, `failed`, `canceled` |
|
|
| `load_level` | `HIGH`, `MEDIUM`, or `LOW` (auto-detected or manual) |
|
|
| `tag` | user defined string |
|
|
| `created_at` | Job creation timestamp |
|
|
| `started_at` | Execution start timestamp |
|
|
| `finished_at` | Execution end timestamp |
|
|
| `exit_code` | '-' if still running, `0` or symbolic error code |
|
|
| `error` | Human-readable failure reason |
|
|
| `pid` | OS process ID (best-effort) |
|
|
| `heartbeat` | Last liveness update |
|
|
| `retryCount` | Current retry attempt (0 if never retried) |
|
|
| `retryAt` | Timestamp for next retry attempt (null if not retrying)|
|
|
|
|
Users can tag jobs for the purpose of filtering using the `--tag=string` CLI option.
|
|
|
|
Jobs are automatically assigned a **load level** based on the command being run (see [Load Levels](#load-levels)). You can override this with `--load-level={high|medium|low}`.
|
|
|
|
Developers can use the `--offline` command line option to test how the service behaves (e.g., creating tmp files). This runs the same code as the service but without having to run the service.
|
|
|
|
### Get status of jobs
|
|
|
|
`zag status` gets the status of all jobs found in /tmp.
|
|
|
|
```
|
|
ID STATE LOAD TAG CREATED EXIT PID RETRIES NEXT RETRY
|
|
job-6d368040 succeeded medium - 2026-01-22 01:48:46 0 17486 - -
|
|
|
|
Total: 1 job (1 succeeded)
|
|
```
|
|
|
|
### Get details of a job
|
|
|
|
`zag describe {jobId}` - collects all the info from /tmp and display it
|
|
|
|
### Stop a job
|
|
|
|
`zag kill {jobId}` - kills a job process
|
|
|
|
### Clean up jobs
|
|
|
|
`zag clean` - removes completed jobs and cleans up old jobs (older than max_age config) from /tmp
|
|
|
|
---
|
|
|
|
## Load Levels
|
|
|
|
Zag supports **per-level job limits** to allow concurrent execution of different types of jobs. For example, you can run multiple downloads while transcoding without overwhelming the system.
|
|
|
|
### Load Level Types
|
|
|
|
| Level | Default Max | Use Case |
|
|
|-------|-------------|----------|
|
|
| HIGH | 2 | CPU/GPU intensive (transcoding, encoding) |
|
|
| MEDIUM | 8 | General purpose commands |
|
|
| LOW | 32 | Network/IO intensive (downloads, uploads) |
|
|
|
|
### Auto-Classification
|
|
|
|
Jobs are automatically classified based on whitelist files in `/etc/zag/`:
|
|
|
|
- `high.load` - ffmpeg, ffprobe, handbrake, x264, x265, etc.
|
|
- `medium.load` - general commands (default if not matched)
|
|
- `low.load` - curl, wget, rclone, rsync, aria2c, etc.
|
|
|
|
### Manual Override
|
|
|
|
```bash
|
|
# Run ffmpeg as LOW load (bypass normal HIGH classification)
|
|
zag run --load-level=low -- ffmpeg -i input.mp4 output.mp4
|
|
|
|
# Run curl as HIGH load (override normal LOW classification)
|
|
zag run --load-level=high -- curl -O https://example.com/file.zip
|
|
```
|
|
|
|
---
|
|
|
|
## Migration from barn
|
|
|
|
| Area | Barn | Zag |
|
|
|------|------|-----|
|
|
| Language | Java 21 | Zig |
|
|
| Binary | `barn` | `zag` |
|
|
| Data dir | `/tmp/barn` | `/tmp/zag` |
|
|
| Config dir | `/etc/barn/` | `/etc/zag/` |
|
|
|
|
The CLI surface, `--output={json|xml}` formats, job states, load levels and retry
|
|
semantics are intended to match barn exactly. Anything that differs will be documented here.
|
|
|
|
## Project Layout
|
|
|
|
```
|
|
build.zig build script
|
|
build.zig.zon package manifest
|
|
src/main.zig CLI entry point
|
|
src/root.zig library root
|
|
```
|