From 822086b39d2112a4b48fa6ba7bf53815a6fd4cb0 Mon Sep 17 00:00:00 2001 From: Daniel Samson <12231216+daniel-samson@users.noreply.github.com> Date: Wed, 21 Jan 2026 11:49:12 +0000 Subject: [PATCH] Add initial files --- .gitignore | 39 ++++ README.md | 90 +++++++- docs/jobs.md | 259 +++++++++++++++++++++++ pom.xml | 17 ++ src/main/java/org/media/samson/Main.java | 17 ++ 5 files changed, 421 insertions(+), 1 deletion(-) create mode 100644 .gitignore create mode 100644 docs/jobs.md create mode 100644 pom.xml create mode 100644 src/main/java/org/media/samson/Main.java diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..480bdf5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,39 @@ +target/ +!.mvn/wrapper/maven-wrapper.jar +!**/src/main/**/target/ +!**/src/test/**/target/ +.kotlin + +### IntelliJ IDEA ### +.idea/modules.xml +.idea/jarRepositories.xml +.idea/compiler.xml +.idea/libraries/ +*.iws +*.iml +*.ipr + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +build/ +!**/src/main/**/build/ +!**/src/test/**/build/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md index 3f86f86..dae80ed 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ This project is designed to run as a **real OS service / daemon** on: - ✅ Linux (systemd) The daemon is **not HTTP-bound**. -It runs independently in the background and exposes control via a local CLI / IPC interface. +It runs independently in the background and exposes control via a local CLI / RPC interface. --- @@ -40,3 +40,91 @@ This project treats FFmpeg jobs as **first-class managed units of work**, not sh --- +## Barn command + +This project creates a simple CLI commandlet with sub-comands. + +- `barn service {start|stop|resart|reload|status|logs"}"` - manages the barn service +- `barn run {command}` - starts job +- `barn status` - gets the status of all jobs running +- `barn describe {jobId}` - collects all the info from /tmp and display it +- `barn kill {jobId}` - kills a job +- `barn clean` - performs clean up + +barn is just one binary for both the service and the client. the client uses IPC to communicate with the service. + +clients typically access barn over ssh: +- fetch from webdav server: `barn run --output=json curl -u username:password -o /tmp/barn/input/video.mp4 https://example.com/dav/remote-file.ext` +- analyse file with ffmpeg: `barn run --output=json curl ffprobe -i /tmp/barn/input/video.mp4` +- get status of the jobs `bar status --output=json` + +barn'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 barn service in order to run jobs (unless you use --offline). You manage the service using the `barn service` sub command. +- `barn service status"` - check the status of the service. up/down, process id etc. +- `barn service start"` - starts barn service process +- `barn service stop"` - stops/kills the barn service +- `barn service restart"` - restarts the barn service +- `barn service reload"` - reloads the configuration and soft restarts the barn service + +### Adding jobs + +`barn run --output=json --tag=downloading }` takes in a command to run, starts a new process/thread, outputs the status of that job: + +| File | Description | +|---------------|--------------------------------------------------------| +| `job_id` | the job of the job | +| `state` | `queued`, `running`, `succeeded`, `failed`, `canceled` | +| `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 | + +Users can tag job for the purpose of filtering using the --tag=string CLI option. + +Developers can use the --offline command line option to test how the service behaves eg creating tmp files etc. This runs the same code as the service but without having to run the service + +> barn's output defaults to a human-readable format, but also supports xml or json via the --output={json|xml} cli option. + +### Get status of jobs + +`bar status` gets the status of all jobs found in /tmp. + +> barn's output defaults to a human-readable format, but also supports xml or json via the --output={json|xml} cli option. + + +### Get Details of a job +`barn describe {jobId}` - collects all the info from /tmp and display it + +> barn's output defaults to a human-readable format, but also supports xml or json via the --output={json|xml} cli option. + +--- + + +### Stop a job +`barn kill {jobId}` - kills a job process + +> barn's output defaults to a human-readable format, but also supports xml or json via the --output={json|xml} cli option. + + +### Stop a job +`barn kill {jobId}` - kills a job + +> barn's output defaults to a human-readable format, but also supports xml or json via the --output={json|xml} cli option. + + +### Clean up jobs +`barn clean` - removes completed jobs and cleans up old jobs (older than max_age config) with errors from /tmp + +> barn's output defaults to a human-readable format, but also supports xml or json via the --output={json|xml} cli option. + + +## Documentation + +- [Barn Temporary Directory Layout](docs/jobs.md) describes how barn tracks jobs and storage \ No newline at end of file diff --git a/docs/jobs.md b/docs/jobs.md new file mode 100644 index 0000000..d8d75a0 --- /dev/null +++ b/docs/jobs.md @@ -0,0 +1,259 @@ +# Barn Temporary Directory Layout + +This document describes the **filesystem-only job state layout** used by the Barn job daemon. + +The design intentionally avoids databases and relies entirely on **atomic filesystem operations** +to provide durability, observability, and crash recovery. + +This layout works consistently across: + +- Linux +- macOS +- Windows + +All paths are rooted under the OS temporary directory. + +--- + +## Base Directory + +``` +/tmp/barn/ +``` + +> On Windows this resolves to: +> +> ``` +> %TEMP%\barn\ +> ``` +> +> In Java, always resolve this via: +> +> ```java +> Paths.get(System.getProperty("java.io.tmpdir"), "barn"); +> ``` + +--- + +## Top-Level Structure + +``` +/tmp/barn/ + jobs/ + locks/ + logs/ +``` + +### Purpose + +| Directory | Purpose | +|---------|--------| +| `jobs/` | Job definitions, state, logs, and artifacts | +| `locks/` | Scheduler and per-job locks | +| `logs/` | Daemon-level logs | + +--- + +## Jobs Directory + +Each job is fully self-contained. + +``` +/tmp/barn/jobs/ + / +``` + +Example: + +``` +/tmp/barn/jobs/job-9f83c/ +``` + +--- + +## Per-Job Directory Layout + +``` +/tmp/barn/jobs// + manifest.json + state + stage + pid + heartbeat + exit_code + error + created_at + started_at + finished_at + + work/ + input/ + output/ + + logs/ + stdout.log + stderr.log + progress.log + upload.log +``` + +--- + +## Job Metadata Files + +### `manifest.json` +Immutable job definition. + +Contains: +- Input WebDAV URL +- Output WebDAV URL +- Job command line arguments arguments +- Retry policy +- Optional metadata + +Written once at job creation. + +--- + +### Lifecycle State Files + +| File | Description | +|---------------|--------------------------------------------------------| +| `state` | `queued`, `running`, `succeeded`, `failed`, `canceled` | +| `tag` | user defined string | +| `created_at` | Job creation timestamp | +| `started_at` | Execution start timestamp | +| `finished_at` | Execution end timestamp | +| `exit_code` | `0` or symbolic error code | +| `error` | Human-readable failure reason | +| `pid` | OS process ID (best-effort) | +| `heartbeat` | Last liveness update | + +All values are stored as **plain text**. + +--- + +## Working Directory + +``` +work/ + input/ + source.tmp + output/ + result.mp4 +``` + +- Temporary artifacts only +- Safe to delete after completion +- Never used for scheduling decisions + +--- + +## Logs Directory + +``` +logs/ + stdout.log + stderr.log + progress.log + upload.log +``` + +### Notes + +- Append-only +- Human-readable +- `progress.log` contains FFmpeg `-progress` output + +--- + +## Locks Directory + +``` +/tmp/barn/locks/ + scheduler.lock + job-.lock +``` + +### Usage + +- `scheduler.lock` prevents multiple schedulers +- `job-.lock` prevents duplicate execution +- Created via atomic filesystem operations + +--- + +## Daemon Logs + +``` +/tmp/barn/logs/ + barn.log +``` + +Contains daemon-level events, not job output. + +--- + +## Example: Running Job + +``` +/tmp/barn/jobs/job-9f83c/ + manifest.json + state → running + stage → transcode + pid → 12345 + heartbeat → 2026-01-21T18:42:10Z + started_at → 2026-01-21T18:40:01Z + + work/ + input/source.tmp + output/result.mp4 + + logs/ + download.log + ffmpeg.stdout.log + ffmpeg.stderr.log + progress.log +``` + +--- + +## Crash Recovery Strategy + +On daemon startup: + +1. Scan `/tmp/barn/jobs/*` +2. Identify jobs where: + - `state = running` + - `heartbeat` is stale +3. Verify process liveness (best-effort) +4. Mark orphaned jobs as: + - `state = failed` + - `exit_code = orphaned_process` + - write `finished_at` + +No database is required. + +--- + +## Design Principles + +- Filesystem is the source of truth +- Atomic writes over in-place mutation +- Human-readable state +- Portable across operating systems +- Easy manual inspection and recovery + +--- + +## Summary + +This directory structure provides: + +- Durable job state +- Clear lifecycle tracking +- Crash-safe recovery +- Cross-platform compatibility +- Zero external dependencies + +It forms the backbone of the Barn job execution model. diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..339e9fe --- /dev/null +++ b/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + org.media.samson + barn + 1.0-SNAPSHOT + + + 25 + 25 + UTF-8 + + + \ No newline at end of file diff --git a/src/main/java/org/media/samson/Main.java b/src/main/java/org/media/samson/Main.java new file mode 100644 index 0000000..52ef3e5 --- /dev/null +++ b/src/main/java/org/media/samson/Main.java @@ -0,0 +1,17 @@ +package org.media.samson; + +//TIP To Run code, press or +// click the icon in the gutter. +public class Main { + static void main() { + //TIP Press with your caret at the highlighted text + // to see how IntelliJ IDEA suggests fixing it. + IO.println(String.format("Hello and welcome!")); + + for (int i = 1; i <= 5; i++) { + //TIP Press to start debugging your code. We have set one breakpoint + // for you, but you can always add more by pressing . + IO.println("i = " + i); + } + } +}