//! docket — distributed background task system for zig 0.17. //! //! a zig port of chrisguidry/docket (`pydocket` on PyPI), which is the //! original work of Chris Guidry (https://bsky.app/profile/guid.foo). all //! credit for the design, the API shape, the Lua scripts, and the conceptual //! model goes to him — this is just a translation. //! //! schedules immediate and future work onto a Redis-backed queue, with workers //! that consume the queue and execute tasks under explicit io discipline. //! //! v0 scope: //! - register tasks with comptime-erased Params types (JSON-serialized over the wire) //! - `add(name, params, .{ .when_micros = ... })` — immediate + future scheduling //! - Perpetual tasks (interval-based auto-rescheduling) //! - Worker with concurrency limit, heartbeat, clean Io-cancellation shutdown //! - Multiple workers consuming the same queue (the horizontal-scale story) //! - ack-on-success / re-park-on-failure (simplest retry semantics) //! //! intentionally deferred: //! - cron expressions (no `cron` dep) //! - explicit retry policies (Retry, ExponentialRetry) //! - replace, cancel-by-key, strikes //! - result storage //! - dependency injection (Depends, Shared, TaskLogger, Cooldown/Debounce/RateLimit/ConcurrencyLimit) //! - agenda batch helper //! //! reference: docs/design.md (TODO), chrisguidry/docket python source. const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const task_mod = @import("task.zig"); const execution_mod = @import("execution.zig"); const worker_mod = @import("worker.zig"); const backend_mod = @import("backend.zig"); pub const Task = task_mod.Task; pub const TaskRegistry = task_mod.TaskRegistry; pub const TaskOptions = task_mod.TaskOptions; pub const Perpetual = task_mod.Perpetual; pub const TaskContext = execution_mod.TaskContext; pub const ExecutionHandle = execution_mod.ExecutionHandle; pub const Worker = worker_mod.Worker; pub const WorkerConfig = worker_mod.WorkerConfig; pub const WORKER_GROUP = worker_mod.WORKER_GROUP; pub const Backend = backend_mod.Backend; pub const Disposition = execution_mod.Disposition; /// Central Docket — registers tasks, schedules executions, owns the backend. pub const Docket = struct { allocator: Allocator, io: Io, name: []const u8, backend: Backend, registry: TaskRegistry, execution_ttl_seconds: i64, pub const Config = struct { /// Logical name for this docket; used as Redis key prefix. name: []const u8 = "docket", /// Redis URL (`redis://...` or `unix:///path/to/sock`). /// Future: `memory://name` for the embedded backend. url: []const u8 = "redis://localhost:6379/0", /// How long a terminal `{prefix}:runs:{key}` tombstone is retained /// after ack. 0 deletes it immediately (no post-hoc observability). /// /// This MUST stay bounded. Perpetual tasks mint a new task key every /// reschedule, so an unexpiring tombstone is an unbounded key leak — /// it OOM-killed a production Redis every other day before this /// existed. Mirrors pydocket's `execution_ttl` (15 minutes). execution_ttl_seconds: i64 = 15 * std.time.s_per_min, }; pub fn init(allocator: Allocator, io: Io, cfg: Config) !Docket { const backend = try backend_mod.connect(allocator, io, cfg.url, cfg.name); return .{ .allocator = allocator, .io = io, .name = cfg.name, .backend = backend, .registry = TaskRegistry.init(allocator), .execution_ttl_seconds = cfg.execution_ttl_seconds, }; } pub fn deinit(self: *Docket) void { self.registry.deinit(); self.backend.close(self.io); } /// Register a task for execution. Comptime-introspects the function signature /// to verify it matches `fn(Io, *TaskContext, Params) !void` and generates a /// type-erased dispatch shim that JSON-parses params before invoking the user fn. pub fn register( self: *Docket, comptime name: []const u8, comptime func: anytype, comptime ParamsType: type, options: TaskOptions, ) !void { const task = task_mod.makeTask(name, func, ParamsType, options); try self.registry.put(name, task); } /// Schedule a task. `when_micros = null` means immediate; otherwise unix-epoch microseconds. pub fn add( self: *Docket, comptime name: []const u8, params: anytype, opts: AddOptions, ) !execution_mod.ExecutionHandle { if (self.registry.get(name) == null) return error.TaskNotRegistered; return execution_mod.schedule(self, name, params, opts); } pub const AddOptions = struct { /// Unix-epoch microseconds for delayed scheduling. Null = immediate. when_micros: ?i64 = null, /// Idempotency key. If null, docket generates a UUID v7 (time-ordered). key: ?[]const u8 = null, }; /// Redis key with the docket prefix. pub fn key(self: *const Docket, alloc: Allocator, suffix: []const u8) ![]const u8 { return std.fmt.allocPrint(alloc, "{s}:{s}", .{ self.name, suffix }); } }; test { std.testing.refAllDecls(@This()); _ = @import("task.zig"); _ = @import("execution.zig"); _ = @import("worker.zig"); _ = @import("backend.zig"); _ = @import("backend/redis.zig"); _ = @import("backend/memory.zig"); }