# pacchettino: file-based job queue for D — full reference pacchettino is a job queue for the D programming language made of plain directories on disk. Producers add jobs (some bytes, or a file), consumers in other processes or threads process them. No server, no database, no dependencies. Linux, macOS, BSD and Windows. MIT license. `import pacchettino;` for the queue, `import pacchettino.uuid;` for the UUID functions. This file documents version 1.1.0; see "Versions" at the end for what 1.0.x lacks. - Rules for agents: https://trikko.github.io/pacchettino/AGENTS.md - API documentation: https://trikko.github.io/pacchettino/ - Source: https://github.com/trikko/pacchettino ## Installation `dub add pacchettino`. dmd or ldc2. The `Duration` arguments below are written with `core.time` literals (`5.seconds`, `10.minutes`): `import core.time;` (or `std.datetime`) in your code. ## Guarantees - A job is in the filesystem before `sendData`/`sendFile` returns: it survives the producer or the consumer stopping or crashing, and a normal reboot. After a power cut, or a crash of the whole system, the last jobs can be lost or empty, unless `durable` is set (see "Durability"). - A job never appears half written: it is written in `tmp/` and moved into the queue with an atomic rename. - Each job is given to one consumer only, even with many consumer processes and threads on the same directory. - If a consumer dies while processing a job, the next `receive*` of any consumer on the same directory moves that job to `interrupted/`. It is not processed again automatically: the callback may have done part of the work. `requeue`/`requeueAll` try it again. - The same machine only: crash detection checks if the consumer that took the job is still alive, by PID or by lock file (see "Crash detection"). Do not share the directory over NFS, SMB or a synced folder. ## Pacchettino ```d this(string baseDir, KeepPolicy keepPolicy = KeepPolicy.ALL); ``` Creates `baseDir` and its subdirectories if missing; throws if `baseDir` exists and is not a directory. There is nothing to open or close: every producer and consumer creates its own `Pacchettino` on the same directory. An instance can be used by many threads for sending and receiving; set the callbacks and `retryDelay` before the threads start. ### KeepPolicy ```d enum KeepPolicy { NONE = 0, SUCCESS = 1, FAILED = 2, INTERRUPTED = 4, ALL = SUCCESS | FAILED | INTERRUPTED } ``` Which processed jobs stay on disk, combined with `|`: `new Pacchettino(dir, KeepPolicy.FAILED | KeepPolicy.INTERRUPTED)`. The jobs not kept are deleted as soon as they are processed. The default `ALL` keeps everything forever: use `cleanup`. ## Sending ```d string sendData(string s, Duration delay = Duration.zero) const; string sendData(const ubyte[] s, Duration delay = Duration.zero) const; string sendFile(const string filePath, bool copyFile = true, Duration delay = Duration.zero) const; ``` - Return the job id, a UUIDv7 string (`"01a0f35b-14fd-7000-903a-e2144b506627"`). UUIDv7 ids are ordered by time. - `sendFile` copies the file into the queue; with `copyFile = false` it moves it (a rename, or copy and delete across filesystems). Throws if the file does not exist or its name is longer than 200 bytes. The consumer gets the original file name back. - `sendData` is for small payloads: the consumer receives all of it in memory. For big data write a file and use `sendFile`. - `delay`: the job waits in `scheduled/` and is moved to the queue by `receive`/`receiveOne` once the delay has expired (`receive` checks every time, `receiveOne` at most every 100 ms). ```d auto id = queue.sendData("send newsletter 12"); queue.sendData(cast(const ubyte[]) serialized); queue.sendFile("/tmp/report.pdf"); // copy queue.sendFile("/tmp/report.pdf", false); // move queue.sendFile("/tmp/report.pdf", true, 1.hours); // copy, not before one hour queue.sendData("reminder", 10.minutes); ``` ## Receiving ```d Result delegate(string id, ubyte[] data) onDataReceived; Result delegate(string id, string name, string path) onFileReceived; Duration retryDelay = Duration.zero; size_t receive(bool randomize = true) const; bool receiveOne(bool randomize = true) const; bool receiveOne(Duration timeout, bool randomize = true, Duration pollInterval = 100.msecs) const; ``` - Set both callbacks if both kinds of job can arrive: the defaults return `Result.FAILED`. - `onDataReceived(id, data)`: `id` is `"raw-"`, `data` the bytes sent. - `onFileReceived(id, name, path)`: `id` is `"fle--"`, `name` the original file name, `path` where the file is now (inside `processing/`). Read it or copy it; do not move or delete it: pacchettino moves or deletes it according to the result. Close it before returning: Windows cannot move an open file. On Windows, when the path is longer than `MAX_PATH` it starts with `\\?\`: `std.file` accepts it, some external programs do not. - The callbacks run inside `receive*`, in the calling thread, one job at a time. There is no background thread: nothing is processed, promoted from `scheduled/` or recovered from a crash except inside a `receive*` call. - `receive()` processes every job currently queued and returns how many. `receiveOne()` processes at most one and returns `true` if it did. `receiveOne(timeout)` waits for a job up to `timeout`, checking every `pollInterval`, and returns `false` if none arrived: it is the loop of a consumer. - `randomize`: random order by default (many consumers do not compete for the same job); `false` processes the oldest job first (FIFO). `receiveOne` lists the queue once and takes the jobs listed one per call, sharing them between the threads of the process: it lists again when they are over, so its cost does not grow with the length of the queue. ### Result ```d enum Result { SUCCESS, FAILED, RETRY } ``` - `SUCCESS`: moved to `success/` (if kept), otherwise deleted. - `FAILED`: moved to `failed/` (if kept), otherwise deleted. An exception thrown by the callback is `FAILED`. - `RETRY`: back in the queue at once, or in `scheduled/` for `retryDelay` if it is not zero. There is no attempt counter: keep one yourself (for example in a map by id, or inside the data) if retries must stop. ```d queue.retryDelay = 30.seconds; queue.onDataReceived = (string id, ubyte[] data) { try { callApi(cast(string) data); return Pacchettino.Result.SUCCESS; } catch (TimeoutException e) return Pacchettino.Result.RETRY; }; ``` ## Following jobs ```d enum Status { UNKNOWN, QUEUED, PROCESSING, SUCCESS, FAILED, INTERRUPTED, SCHEDULED } Status status(string id) const; static SysTime sentAt(string id); bool isScheduled(string id) const; bool isQueued(string id) const; bool isProcessing(string id) const; bool isSuccess(string id) const; // throws if the policy does not keep SUCCESS bool isFailed(string id) const; // throws if the policy does not keep FAILED bool isInterrupted(string id) const; // throws if the policy does not keep INTERRUPTED size_t countScheduled() const; size_t countQueued() const; size_t countProcessing() const; size_t countSuccessful() const; // throws if the policy does not keep SUCCESS size_t countFailed() const; // throws if the policy does not keep FAILED size_t countInterrupted() const; // throws if the policy does not keep INTERRUPTED size_t countAll() const; // every state kept by the policy ``` - All of them accept both the id returned by `send*` and the one given to the callbacks. - `status` never throws: a job that is not on disk (never sent, or processed and not kept) is `UNKNOWN`. - `sentAt` reads the time from the UUIDv7 id (UTC, millisecond precision); throws on an invalid id. It is `static`: `Pacchettino.sentAt(id)`. ## Managing jobs ```d bool requeue(string id) const; size_t requeueAll(KeepPolicy which = KeepPolicy.FAILED | KeepPolicy.INTERRUPTED) const; size_t cleanup(KeepPolicy which = KeepPolicy.ALL, Duration olderThan = Duration.zero) const; ``` - `requeue(id)` moves a job from `failed/`, `interrupted/` or `success/` back to the queue; `false` if not found. `requeueAll(which)` does it for every job in the chosen directories and returns how many. - `cleanup(which, olderThan)` deletes kept jobs, only those whose file was last modified more than `olderThan` ago if given, and returns how many. `cleanup()` empties `success/`, `failed/` and `interrupted/`. ```d queue.requeueAll(Pacchettino.KeepPolicy.INTERRUPTED); // after a crash queue.cleanup(Pacchettino.KeepPolicy.SUCCESS, 7.days); // keep one week of history ``` ## Crash detection ```d enum CrashDetection { PID, LOCK_FILE } CrashDetection crashDetection = CrashDetection.PID; ``` - `PID` (default): a job being processed is `processing/.`; if that process does not exist, the job is moved to `interrupted/`. Fast, but only valid inside one PID namespace. - `LOCK_FILE`: each consumer process holds a lock on `owners/` while alive (`flock`, or a file open without sharing on Windows); its jobs are `processing/.`. The operating system releases the lock when the process dies. Needed when consumers run in **different containers** on the same directory: set it on all of them, otherwise they take each other's jobs for crashed ones. Slightly slower: one file open per job processed by others. - Jobs marked in either way are always recognized by every consumer. Producers do not need the setting. ## Durability ```d bool durable = false; ``` With `durable = true`, `sendData`/`sendFile` return only after the job is flushed to the disk (fsync of the file and of its directory; `F_FULLFSYNC` on macOS; `FlushFileBuffers` and write-through moves on Windows), and every change of state is flushed too. Much slower: a few thousand jobs per second on a SSD instead of tens of thousands, much less on SD cards and hard disks. Set it on producers and consumers. ## Directory layout ``` baseDir/ scheduled/ -raw-, -fle-- delayed jobs queued/ raw-, fle-- waiting processing/ ./ taken by a consumer success/ jobs that returned SUCCESS (if kept) failed/ jobs that returned FAILED (if kept) interrupted/ jobs of consumers that died (if kept) tmp/ files being written owners/ lock files of the consumers (CrashDetection.LOCK_FILE only) ``` The files are the jobs: a `raw-` file contains the bytes, a `fle-` file is the file. They can be inspected with `ls`; do not write into these directories by hand, use `send*`. ## Worked examples ### A worker that uploads files, with retries and history ```d import pacchettino; import core.time; import std.stdio; void main() { auto queue = new Pacchettino("/var/spool/uploader"); queue.retryDelay = 1.minutes; queue.onFileReceived = (string id, string name, string path) { if (!networkUp()) return Pacchettino.Result.RETRY; upload(path, name); return Pacchettino.Result.SUCCESS; }; // Jobs of a previous run that crashed: try them again queue.requeueAll(Pacchettino.KeepPolicy.INTERRUPTED); while (true) { queue.receiveOne(10.seconds); queue.cleanup(Pacchettino.KeepPolicy.SUCCESS, 7.days); } } ``` ### Many consumers in one process ```d import core.thread; auto queue = new Pacchettino("/var/spool/jobs"); queue.onDataReceived = (string id, ubyte[] data) => handle(data); foreach (i; 0 .. 4) new Thread({ while (true) queue.receiveOne(5.seconds); }).start(); ``` Separate processes on the same directory work the same way. ### A web server that hands slow work to a worker The request handler only calls `sendData`/`sendFile` and answers at once with the id; a separate worker program processes the job; the page asks for `status(id)` to show the progress. ```d string id = queue.sendData(`{"user": 42, "report": "monthly"}`); // later with (Pacchettino.Status) final switch (queue.status(id)) { case SCHEDULED, QUEUED: return "waiting"; case PROCESSING: return "working"; case SUCCESS: return "done"; case FAILED, INTERRUPTED: return "failed"; case UNKNOWN: return "unknown"; } ``` ## pacchettino.uuid ```d string UUIDv7(); ubyte[16] UUIDv7!ubyte(); // time ordered, used for job ids string UUIDv4(); ubyte[16] UUIDv4!ubyte(); // random string UUIDv5(string name, ubyte[16] namespace = ubyte[16].init); // SHA-1 string UUIDv3(string name, ubyte[16] namespace = ubyte[16].init); // MD5 enum UUIDNamespace : ubyte[16] { DNS, URL, OID, X500 } ``` `UUIDv5` and `UUIDv3` also take `const(ubyte)[]` names, and have a `!ubyte` form. Strings are lowercase. UUIDv7 ids created by the same process are strictly increasing. ## Limits - One machine, local filesystem. - On Windows, antivirus and indexers keep new files open for a moment: pacchettino retries the moves for a short while, but a file kept open for longer (by the callback, or by another program) makes the move fail. - No priorities (use two directories), no attempt limit, no job dependencies, no scheduling at a date (only a delay from now), no notifications: consumers poll. - File names up to 200 bytes (the filesystem allows 255 per name; pacchettino adds its prefixes). ## Versions Versions 1.0.x (up to 1.0.3) have the constructor, `KeepPolicy`, `Result`, `sendData`/`sendFile` without `delay`, `receive`/`receiveOne` returning `void`, `onDataReceived`/`onFileReceived`, and `isQueued`, `isProcessing`, `isSuccess`, `isFailed`, `isInterrupted` (accepting only the id returned by `send*`), with jobs taken in directory order when `randomize` is false, does not build on Windows, and has no `crashDetection` nor `durable`. Everything else in this file came with 1.1.0.