//! docket runtime — process-wide singleton. //! //! Mirrors the broker/db pattern: a single `Docket` instance lives at module //! scope, initialised once in main() (or initInfra()) and accessed by //! services that register perpetual tasks against it. The DocketWorker //! service polls its stream and dispatches. //! //! URL resolution mirrors the broker dialect: //! - PREFECT_BROKER_BACKEND=memory → `memory://prefect` (burner-redis embed) //! - PREFECT_BROKER_BACKEND=redis → `redis://{HOST}:{PORT}/{DB}` from //! PREFECT_REDIS_MESSAGING_* env vars const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const docket = @import("docket"); const env = @import("utilities/env.zig"); const broker_core = @import("broker/core.zig"); pub const DOCKET_NAME: []const u8 = "prefect"; var initialized: bool = false; var allocator_cache: Allocator = undefined; var url_buf: ?[]u8 = null; /// The shared engine. Initialised by `init()`; null until then. pub var docket_instance: ?docket.Docket = null; /// Resolve the URL for the configured broker backend. Caller does not own /// the returned slice — it lives in module storage until `deinit()`. pub fn resolveUrl(allocator: Allocator) ![]const u8 { if (url_buf) |u| return u; const backend = env.getenv("PREFECT_BROKER_BACKEND") orelse "memory"; if (std.mem.eql(u8, backend, "memory")) { url_buf = try allocator.dupe(u8, "memory://prefect"); return url_buf.?; } const cfg = broker_core.parseRedisConfig(); url_buf = try std.fmt.allocPrint(allocator, "redis://{s}:{d}/{d}", .{ cfg.host, cfg.port, cfg.db }); return url_buf.?; } /// Initialise the engine. Idempotent. pub fn init(allocator: Allocator, io: Io) !void { if (initialized) return; allocator_cache = allocator; const url = try resolveUrl(allocator); docket_instance = try docket.Docket.init(allocator, io, .{ .name = DOCKET_NAME, .url = url, }); initialized = true; } pub fn deinit() void { if (docket_instance) |*d| d.deinit(); docket_instance = null; if (url_buf) |u| allocator_cache.free(u); url_buf = null; initialized = false; } /// Accessor used by services that register tasks / schedule work. pub fn instance() *docket.Docket { return &docket_instance.?; } pub fn isInitialised() bool { return initialized; }