Something went wrong. Try again.
zig port of github.com/chrisguidry/docket
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113//! 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://<name>` — embeds burner-redis in-process; `<name>`/// 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;}