//! Task registration with comptime-erased Params types. //! //! Users register a typed function: //! //! ```zig //! const GreetParams = struct { greeting: []const u8, name: []const u8 }; //! fn greet(io: Io, ctx: *docket.TaskContext, p: GreetParams) !void { ... } //! //! try docket.register(.{ //! .name = "greet", //! .func = greet, //! .params = GreetParams, //! .perpetual = .{ .every_micros = 5 * std.time.us_per_s }, //! }); //! ``` //! //! `makeTask` comptime-introspects the function signature, validates it matches //! `fn(Io, *TaskContext, Params) !void`, and generates an erased dispatch shim //! that JSON-parses params on the worker side before invoking the user fn. const std = @import("std"); const Allocator = std.mem.Allocator; const Io = std.Io; const TaskContext = @import("execution.zig").TaskContext; /// Options for a task registration. Fields are runtime-storable (no anytype/type fields). /// The user function and Params type are separate comptime arguments to `register`. pub const TaskOptions = struct { /// Optional perpetual config — task auto-reschedules after each success. perpetual: ?Perpetual = null, }; pub const Perpetual = struct { /// Interval between completions and next scheduled run. every_micros: i64, }; /// A type-erased task ready for storage in the registry. pub const Task = struct { /// JSON-parses `params_json` and invokes the user fn under `io` with `ctx`. /// The allocator is for the JSON parse arena; callers should pass a per-task arena. invoke: *const fn (alloc: Allocator, io: Io, ctx: *TaskContext, params_json: []const u8) anyerror!void, perpetual: ?Perpetual, }; /// Comptime build of a `Task` from a typed user function. Validates signature and /// generates the dispatch shim. /// /// `options` is runtime — only the signature + ParamsType need to be comptime. /// This lets callers pass env-derived intervals into `Perpetual.every_micros` /// without forcing the whole config into a comptime literal. pub fn makeTask( comptime name: []const u8, comptime user_fn: anytype, comptime ParamsType: type, options: TaskOptions, ) Task { // Comptime signature validation. const fn_info = @typeInfo(@TypeOf(user_fn)).@"fn"; if (fn_info.param_types.len != 3) { @compileError("task '" ++ name ++ "' must have signature fn(Io, *TaskContext, " ++ @typeName(ParamsType) ++ ") !void"); } if (fn_info.param_types[0] != Io) { @compileError("task '" ++ name ++ "' first parameter must be std.Io"); } if (fn_info.param_types[1] != *TaskContext) { @compileError("task '" ++ name ++ "' second parameter must be *TaskContext"); } if (fn_info.param_types[2] != ParamsType) { @compileError("task '" ++ name ++ "' third parameter must match the declared Params type"); } const ret_info = @typeInfo(fn_info.return_type.?); if (ret_info != .error_union or ret_info.error_union.payload != void) { @compileError("task '" ++ name ++ "' must return !void"); } const Erased = struct { fn invoke(alloc: Allocator, io: Io, ctx: *TaskContext, params_json: []const u8) anyerror!void { const parsed = try std.json.parseFromSlice(ParamsType, alloc, params_json, .{ .ignore_unknown_fields = true, }); defer parsed.deinit(); return user_fn(io, ctx, parsed.value); } }; return .{ .invoke = Erased.invoke, .perpetual = options.perpetual, }; } /// Runtime registry mapping task name → erased Task. pub const TaskRegistry = struct { map: std.StringHashMap(Task), pub fn init(alloc: Allocator) TaskRegistry { return .{ .map = std.StringHashMap(Task).init(alloc) }; } pub fn deinit(self: *TaskRegistry) void { self.map.deinit(); } pub fn put(self: *TaskRegistry, name: []const u8, task: Task) !void { try self.map.put(name, task); } pub fn get(self: *TaskRegistry, name: []const u8) ?Task { return self.map.get(name); } pub fn iterator(self: *TaskRegistry) std.StringHashMap(Task).Iterator { return self.map.iterator(); } }; test "makeTask validates signature and dispatches JSON params" { const P = struct { greeting: []const u8, name: []const u8 }; const Probe = struct { var last_greeting: []const u8 = ""; var last_name: []const u8 = ""; fn run(_: Io, _: *TaskContext, p: P) !void { last_greeting = p.greeting; last_name = p.name; } }; const task = comptime makeTask("greet", Probe.run, P, .{}); var threaded = Io.Threaded.init(std.testing.allocator, .{}); defer threaded.deinit(); const io = threaded.io(); var ctx: TaskContext = .{ .key = "k", .attempt = 1, .generation = 0 }; try task.invoke(std.testing.allocator, io, &ctx, \\{"greeting":"hello","name":"jane"} ); try std.testing.expectEqualStrings("hello", Probe.last_greeting); try std.testing.expectEqualStrings("jane", Probe.last_name); }