//! Redis backend — talks to a real Redis (or compatible) server via the //! `tangled.org/zzstoatzz.io/redis` client. Composite ops are implemented as //! Lua scripts in `src/scripts/*.lua`, executed via EVAL. const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const redis = @import("redis"); /// Embed Lua scripts at build time. One source of truth per atomic op. const SCHEDULE_LUA = @embedFile("../scripts/schedule.lua"); const PROMOTE_DUE_LUA = @embedFile("../scripts/promote_due.lua"); const ACK_LUA = @embedFile("../scripts/ack.lua"); pub const ScheduleArgs = struct { task_key: []const u8, when_micros: i64, is_immediate: bool, /// Stream id of the delivered message rescheduling itself under its own /// key; empty for every other schedule. replacing: []const u8 = "", /// Alternating name/value pairs. Caller owns the storage; refs must stay /// valid for the duration of the call. fields: []const []const u8, }; pub const PromoteResult = struct { total_in_queue: i64, promoted: i64, }; pub const Disposition = enum { scheduled, already_scheduled, struck }; pub const RedisBackend = struct { allocator: Allocator, io: Io, client: ?redis.Client, host: []u8, port: u16, username: []u8, password: []u8, db: u8, /// Logical docket name — used as the prefix for all our keys. /// Owned by Docket, borrowed here for key construction. prefix: []const u8, pub fn connect(allocator: Allocator, io: Io, url: []const u8, prefix: []const u8) !RedisBackend { var parsed = try parseUrl(allocator, url); defer parsed.deinit(); const host = try allocator.dupe(u8, parsed.host); errdefer allocator.free(host); const username = try allocator.dupe(u8, parsed.username); errdefer allocator.free(username); const password = try allocator.dupe(u8, parsed.password); errdefer allocator.free(password); const client = try redis.Client.connectWithConfig(io, allocator, .{ .host = parsed.host, .port = parsed.port, .username = parsed.username, .password = parsed.password, .db = parsed.db, }); return .{ .allocator = allocator, .io = io, .client = client, .host = host, .port = parsed.port, .username = username, .password = password, .db = parsed.db, .prefix = prefix, }; } pub fn close(self: *RedisBackend, _: Io) void { if (self.client) |*client| client.close(); self.client = null; self.allocator.free(self.host); self.allocator.free(self.username); @memset(self.password, 0); self.allocator.free(self.password); self.allocator.destroy(self); } fn discardPoisonedClient(self: *RedisBackend) void { if (self.client) |*client| { if (!client.isPoisoned()) return; client.close(); self.client = null; } } fn ensureConnected(self: *RedisBackend) !void { if (self.client != null) return; self.client = try redis.Client.connectWithConfig(self.io, self.allocator, .{ .host = self.host, .port = self.port, .username = self.username, .password = self.password, .db = self.db, }); } fn sendCommand(self: *RedisBackend, args: []const []const u8) !redis.Value { try self.ensureConnected(); return self.client.?.sendCommand(args) catch |err| { self.discardPoisonedClient(); return err; }; } // ----------------------------------------------------------------------- // composite ops — backed by the embedded Lua scripts // ----------------------------------------------------------------------- /// EVAL schedule.lua: place the task on the stream (immediate) or the /// sorted-set queue (future). pub fn scheduleTask(self: *RedisBackend, args: ScheduleArgs) !Disposition { var arena = std.heap.ArenaAllocator.init(self.allocator); defer arena.deinit(); const a = arena.allocator(); const stream_key = try keyJoin(a, &.{ self.prefix, "stream" }); const parked_key = try keyJoin(a, &.{ self.prefix, "parked", args.task_key }); const queue_key = try keyJoin(a, &.{ self.prefix, "queue" }); const runs_key = try keyJoin(a, &.{ self.prefix, "runs", args.task_key }); const when_str = try std.fmt.allocPrint(a, "{d}", .{args.when_micros}); const is_immediate_str: []const u8 = if (args.is_immediate) "1" else "0"; // EVAL