//! Backend abstraction. //! //! For v0 there's a single concrete backend (`RedisBackend`). The future //! embedded backend will live alongside it; both expose the same //! composite-op surface used by docket internals. //! //! The composite ops are the Lua scripts (schedule, promote_due, ack). //! Redis backend executes them remotely; embedded backend will execute //! them locally via an in-process liblua + in-memory store. const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const redis = @import("redis"); pub const redis_backend = @import("backend/redis.zig"); pub const memory_backend = @import("backend/memory.zig"); pub const RedisBackend = redis_backend.RedisBackend; pub const MemoryBackend = memory_backend.MemoryBackend; pub const ScheduleArgs = redis_backend.ScheduleArgs; pub const PromoteResult = redis_backend.PromoteResult; pub const Disposition = redis_backend.Disposition; /// Tagged union over all backend variants. Both expose the same composite-op /// surface; consumers (Docket / Worker) don't know which one they have. pub const Backend = union(enum) { redis: *RedisBackend, memory: *MemoryBackend, pub fn close(self: Backend, io: Io) void { switch (self) { inline else => |b| b.close(io), } } pub fn scheduleTask(self: Backend, args: ScheduleArgs) !Disposition { return switch (self) { inline else => |b| b.scheduleTask(args), }; } pub fn promoteDueTasks(self: Backend, now_micros: i64) !PromoteResult { return switch (self) { inline else => |b| b.promoteDueTasks(now_micros), }; } pub fn ackMessage( self: Backend, group: []const u8, msg_id: []const u8, task_key: []const u8, terminal_state: []const u8, execution_ttl_seconds: i64, ) !void { return switch (self) { inline else => |b| b.ackMessage(group, msg_id, task_key, terminal_state, execution_ttl_seconds), }; } pub fn ensureConsumerGroup(self: Backend, group: []const u8) !void { return switch (self) { inline else => |b| b.ensureConsumerGroup(group), }; } pub fn reclaimIdle( self: Backend, group: []const u8, consumer: []const u8, min_idle_ms: u32, ) !?redis.StreamEntry { return switch (self) { inline else => |b| b.reclaimIdle(group, consumer, min_idle_ms), }; } pub fn consumeStream( self: Backend, group: []const u8, consumer: []const u8, block_ms: u32, ) !?redis.StreamEntry { return switch (self) { inline else => |b| b.consumeStream(group, consumer, block_ms), }; } }; /// Resolve a URL to a concrete backend. /// /// Supported schemes: /// - `redis://host[:port][/db]` — talks to a real Redis (or compatible). /// - `memory://` — embeds burner-redis in-process; `` /// is informational (key prefix is owned /// by the Docket name, not the URL). pub fn connect(allocator: Allocator, io: Io, url: []const u8, prefix: []const u8) !Backend { if (std.mem.startsWith(u8, url, "redis://") or std.mem.startsWith(u8, url, "unix://")) { const rb = try allocator.create(RedisBackend); errdefer allocator.destroy(rb); rb.* = try RedisBackend.connect(allocator, io, url, prefix); return .{ .redis = rb }; } if (std.mem.startsWith(u8, url, "memory://")) { const mb = try allocator.create(MemoryBackend); errdefer allocator.destroy(mb); mb.* = try MemoryBackend.connect(allocator, io, prefix); mb.bindEngine(); return .{ .memory = mb }; } return error.UnsupportedBackendUrl; }