const std = @import("std"); const runtime = @import("../runtime.zig"); const wap = @import("wap"); const mem = std.mem; const json = std.json; const Allocator = std.mem.Allocator; const db = @import("../db/sqlite.zig"); const uuid_util = @import("../utilities/uuid.zig"); const time_util = @import("../utilities/time.zig"); // sub-handlers const queues = @import("work_pool_queues.zig"); const workers = @import("work_pool_workers.zig"); const schedule = @import("work_pool_schedule.zig"); const worker_channel = @import("worker_channel.zig"); const admin = @import("admin.zig"); // ===== typed bodies — mirror python's WorkPoolCreate / WorkPoolUpdate ===== const WorkPoolCreateBody = struct { name: []const u8, description: ?[]const u8 = null, type: ?[]const u8 = null, base_job_template: ?json.Value = null, is_paused: bool = false, concurrency_limit: ?i64 = null, storage_configuration: ?json.Value = null, }; const WorkPoolUpdateBody = struct { description: ?[]const u8 = null, base_job_template: ?json.Value = null, is_paused: ?bool = null, concurrency_limit: ?i64 = null, storage_configuration: ?json.Value = null, }; const FilterBody = struct { limit: ?usize = null, offset: ?usize = null, work_pools: ?json.Value = null, sort: ?[]const u8 = null, }; pub fn sendValidationError(res: *wap.Response, detail: []const u8) !void { _ = res.setStatus(.unprocessable_entity); try res.writeJson(.{ .detail = detail }); } pub fn sendError(res: *wap.Response, status: std.http.Status, detail: []const u8) !void { _ = res.setStatus(status); try res.writeJson(.{ .detail = detail }); } pub fn stringifyValue(alloc: Allocator, value: json.Value) ![]const u8 { var buf: std.Io.Writer.Allocating = .init(alloc); errdefer buf.deinit(); var jw: json.Stringify = .{ .writer = &buf.writer }; try jw.write(value); return buf.toOwnedSlice(); } pub fn stringifyOrDefault(alloc: Allocator, value: ?json.Value, default_literal: []const u8) ![]const u8 { if (value) |v| return try stringifyValue(alloc, v); return default_literal; } pub fn stringifyOrNull(alloc: Allocator, value: ?json.Value) !?[]const u8 { if (value) |v| return try stringifyValue(alloc, v); return null; } pub fn handle(req: *wap.Request, res: *wap.Response) !void { const target = req.path; const method = @tagName(req.method); // POST /work_pools/filter - list work pools if (mem.eql(u8, method, "POST") and mem.endsWith(u8, target, "/filter")) { if (mem.indexOf(u8, target, "/queues/filter") != null) { try queues.filter(req, res, target); return; } if (mem.indexOf(u8, target, "/workers/filter") != null) { try workers.filter(req, res, target); return; } try filterPools(req, res); return; } // POST /work_pools/count - count work pools if (mem.eql(u8, method, "POST") and mem.endsWith(u8, target, "/count")) { try countPools(res); return; } // POST /work_pools/{name}/workers/heartbeat if (mem.eql(u8, method, "POST") and mem.indexOf(u8, target, "/workers/heartbeat") != null) { try workers.heartbeat(req, res, target); return; } // POST /work_pools/{name}/get_scheduled_flow_runs if (mem.eql(u8, method, "POST") and mem.endsWith(u8, target, "/get_scheduled_flow_runs")) { try schedule.handle(req, res, target); return; } // POST /work_pools/{name}/queues/ - create queue if (mem.eql(u8, method, "POST") and mem.indexOf(u8, target, "/queues/") != null) { const queues_idx = mem.indexOf(u8, target, "/queues/") orelse { try sendError(res, .bad_request, "invalid path"); return; }; const after_queues = target[queues_idx + 8 ..]; if (after_queues.len == 0 or mem.eql(u8, after_queues, "/")) { try queues.create(req, res, target); return; } } // POST /work_pools/ - create pool if (mem.eql(u8, method, "POST")) { const is_root = mem.endsWith(u8, target, "/work_pools/") or mem.endsWith(u8, target, "/work_pools"); if (is_root) { try createPool(req, res); return; } } // GET /work_pools/{name}/queues/{queue_name} if (mem.eql(u8, method, "GET") and mem.indexOf(u8, target, "/queues/") != null) { try queues.get(res, target); return; } // PATCH /work_pools/{name}/queues/{queue_name} if (mem.eql(u8, method, "PATCH") and mem.indexOf(u8, target, "/queues/") != null) { try queues.update(req, res, target); return; } // DELETE /work_pools/{name}/queues/{queue_name} if (mem.eql(u8, method, "DELETE") and mem.indexOf(u8, target, "/queues/") != null) { try queues.delete(res, target); return; } // DELETE /work_pools/{name}/workers/{worker_name} if (mem.eql(u8, method, "DELETE") and mem.indexOf(u8, target, "/workers/") != null) { try workers.delete(res, target); return; } // GET /work_pools/{name} if (mem.eql(u8, method, "GET")) { const name = extractPoolName(target) orelse { try sendError(res, .bad_request, "work pool name required"); return; }; try getPool(res, name); return; } // PATCH /work_pools/{name} if (mem.eql(u8, method, "PATCH")) { const name = extractPoolName(target) orelse { try sendError(res, .bad_request, "work pool name required"); return; }; try updatePool(req, res, name); return; } // DELETE /work_pools/{name} if (mem.eql(u8, method, "DELETE")) { const name = extractPoolName(target) orelse { try sendError(res, .bad_request, "work pool name required"); return; }; try deletePool(res, name); return; } try sendError(res, .not_implemented, "not implemented"); } // Path extraction helpers (pub for sub-handlers) pub fn extractPoolName(target: []const u8) ?[]const u8 { const prefix = if (mem.startsWith(u8, target, "/api/work_pools/")) "/api/work_pools/" else if (mem.startsWith(u8, target, "/work_pools/")) "/work_pools/" else return null; if (target.len <= prefix.len) return null; const after = target[prefix.len..]; const end = mem.indexOf(u8, after, "/") orelse after.len; if (end == 0) return null; return after[0..end]; } pub fn extractQueueName(target: []const u8) ?[]const u8 { const idx = mem.indexOf(u8, target, "/queues/") orelse return null; const start = idx + 8; if (start >= target.len) return null; const after = target[start..]; const end = mem.indexOf(u8, after, "/") orelse after.len; if (end == 0) return null; return after[0..end]; } pub fn extractWorkerName(target: []const u8) ?[]const u8 { const idx = mem.indexOf(u8, target, "/workers/") orelse return null; const start = idx + 9; if (start >= target.len) return null; const after = target[start..]; if (mem.startsWith(u8, after, "heartbeat") or mem.startsWith(u8, after, "filter")) return null; const end = mem.indexOf(u8, after, "/") orelse after.len; if (end == 0) return null; return after[0..end]; } fn isReservedPool(name: []const u8) bool { if (name.len < 7) return false; var lower: [7]u8 = undefined; for (name[0..7], 0..) |c, i| { lower[i] = std.ascii.toLower(c); } return mem.eql(u8, &lower, "prefect"); } // Pool CRUD handlers fn createPool(req: *wap.Request, res: *wap.Response) !void { const alloc = req.arena; const body = req.parseJson(WorkPoolCreateBody) catch |err| { return switch (err) { error.OutOfMemory => return err, error.UnknownField => sendValidationError(res, "unknown field in request body"), else => sendValidationError(res, "invalid json"), }; }; if (isReservedPool(body.name)) { try sendError(res, .forbidden, "Work pools starting with 'Prefect' are reserved."); return; } if (try db.work_pools.getByName(alloc, body.name)) |_| { const detail = std.fmt.allocPrint(alloc, "Work pool '{s}' already exists.", .{body.name}) catch "Work pool already exists"; try sendError(res, .conflict, detail); return; } const pool_type = body.type orelse "process"; const base_job_template = try stringifyOrDefault(alloc, body.base_job_template, "{}"); var pool_id_buf: [36]u8 = undefined; const pool_id = uuid_util.generate(runtime.appIo(), &pool_id_buf); var queue_id_buf: [36]u8 = undefined; const queue_id = uuid_util.generate(runtime.appIo(), &queue_id_buf); var ts_buf: [32]u8 = undefined; const now = time_util.timestamp(runtime.appIo(), &ts_buf); const status: db.work_pools.Status = if (body.is_paused) .paused else .not_ready; db.work_pools.insert( pool_id, body.name, body.description, pool_type, base_job_template, body.is_paused, body.concurrency_limit, queue_id, status, now, ) catch { try sendError(res, .internal_server_error, "insert failed"); return; }; db.work_queues.insert(queue_id, "default", "", false, null, 1, pool_id, .not_ready, now) catch { try sendError(res, .internal_server_error, "failed to create default queue"); return; }; const pool = try db.work_pools.getByName(alloc, body.name) orelse { try sendError(res, .internal_server_error, "pool not found after insert"); return; }; const resp = writePool(alloc, pool) catch { try sendError(res, .internal_server_error, "serialize error"); return; }; _ = res.setStatus(.created); _ = res.setHeader("content-type", "application/json"); try res.sendBody(resp); } fn getPool(res: *wap.Response, name: []const u8) !void { const alloc = res.arena; if (try db.work_pools.getByName(alloc, name)) |pool| { const resp = writePool(alloc, pool) catch { try sendError(res, .internal_server_error, "serialize error"); return; }; _ = res.setHeader("content-type", "application/json"); try res.sendBody(resp); } else { try sendError(res, .not_found, "Work pool not found."); } } fn updatePool(req: *wap.Request, res: *wap.Response, name: []const u8) !void { const alloc = req.arena; const body = req.parseJson(WorkPoolUpdateBody) catch |err| { return switch (err) { error.OutOfMemory => return err, error.UnknownField => sendValidationError(res, "unknown field in request body"), else => sendValidationError(res, "invalid json"), }; }; const existing = try db.work_pools.getByName(alloc, name) orelse { try sendError(res, .not_found, "Work pool not found."); return; }; if (isReservedPool(name)) { const has_other = body.description != null or body.base_job_template != null; if (has_other) { try sendError(res, .forbidden, "Cannot modify reserved work pool."); return; } } const base_job_template = try stringifyOrNull(alloc, body.base_job_template); var new_status: ?db.work_pools.Status = null; if (body.is_paused) |paused| { if (paused) { new_status = .paused; } else if (existing.status == .paused) { const has_workers = try db.work_pools.hasOnlineWorkers(existing.id); new_status = if (has_workers) .ready else .not_ready; } } var ts_buf: [32]u8 = undefined; const now = time_util.timestamp(runtime.appIo(), &ts_buf); const did_update = db.work_pools.updateByName( name, body.description, base_job_template, body.is_paused, body.concurrency_limit, new_status, now, ) catch { try sendError(res, .internal_server_error, "update failed"); return; }; if (!did_update) { try sendError(res, .not_found, "Work pool not found."); return; } try worker_channel.publishInvalidation(alloc, existing.id, "work_pool_updated"); try res.sendEmpty(.no_content); } fn deletePool(res: *wap.Response, name: []const u8) !void { if (isReservedPool(name)) { try sendError(res, .forbidden, "Cannot delete reserved work pool."); return; } // Capture id BEFORE delete so the invalidation can name the deleted pool. const alloc = res.arena; const pool_for_id = try db.work_pools.getByName(alloc, name); const deleted = db.work_pools.deleteByName(name) catch { try sendError(res, .internal_server_error, "delete failed"); return; }; if (!deleted) { try sendError(res, .not_found, "Work pool not found."); return; } if (pool_for_id) |p| { try worker_channel.publishInvalidation(alloc, p.id, "work_pool_deleted"); } try res.sendEmpty(.no_content); } fn filterPools(req: *wap.Request, res: *wap.Response) !void { const alloc = req.arena; var limit: usize = 200; var offset: usize = 0; if (req.body.len > 0) { if (req.parseJson(FilterBody)) |body| { if (body.limit) |l| limit = l; if (body.offset) |o| offset = o; } else |parse_err| { if (parse_err == error.OutOfMemory) return parse_err; } } const pools = db.work_pools.list(alloc, limit, offset) catch { try sendError(res, .internal_server_error, "database error"); return; }; var output: std.Io.Writer.Allocating = .init(alloc); var jw: json.Stringify = .{ .writer = &output.writer }; jw.beginArray() catch { try sendError(res, .internal_server_error, "serialize error"); return; }; for (pools) |pool| { try writePoolObject(alloc, &jw, pool); } try jw.endArray(); _ = res.setHeader("content-type", "application/json"); try res.sendBody(try output.toOwnedSlice()); } fn countPools(res: *wap.Response) !void { const total = db.work_pools.count() catch { try sendError(res, .internal_server_error, "database error"); return; }; _ = res.setHeader("content-type", "application/json"); try res.writeJson(total); } // JSON serialization pub fn writePool(alloc: std.mem.Allocator, pool: db.work_pools.WorkPoolRow) ![]const u8 { var output: std.Io.Writer.Allocating = .init(alloc); var jw: json.Stringify = .{ .writer = &output.writer }; try writePoolObject(alloc, &jw, pool); return output.toOwnedSlice(); } pub fn writePoolObject(alloc: std.mem.Allocator, jw: *json.Stringify, pool: db.work_pools.WorkPoolRow) !void { try jw.beginObject(); try jw.objectField("id"); try jw.write(pool.id); try jw.objectField("created"); try jw.write(pool.created); try jw.objectField("updated"); try jw.write(pool.updated); try jw.objectField("name"); try jw.write(pool.name); try jw.objectField("description"); if (pool.description) |d| { try jw.write(d); } else { try jw.write(null); } try jw.objectField("type"); try jw.write(pool.type); try jw.objectField("base_job_template"); try jw.beginWriteRaw(); try jw.writer.writeAll(pool.base_job_template); jw.endWriteRaw(); try jw.objectField("is_paused"); try jw.write(pool.is_paused); try jw.objectField("concurrency_limit"); if (pool.concurrency_limit) |c| { try jw.write(c); } else { try jw.write(null); } try jw.objectField("default_queue_id"); if (pool.default_queue_id) |q| { try jw.write(q); } else { try jw.write(null); } try jw.objectField("status"); try jw.write(pool.status.toString()); // python's WorkPoolResponse carries storage_configuration with the // server's default result storage block id (or null if unset). This is // the same value served by `GET /api/admin/storage`. try jw.objectField("storage_configuration"); try jw.beginObject(); try jw.objectField("default_result_storage_block_id"); if (try admin.readDefaultStorageBlockId(alloc)) |block_id| { try jw.write(block_id); } else { try jw.write(null); } try jw.endObject(); try jw.endObject(); } test "supplied JSON allocation failure cannot substitute a default payload" { const Check = struct { fn encode(alloc: Allocator, value: json.Value) ![]const u8 { return stringifyOrDefault(alloc, value, "null"); } }; try @import("json_test.zig").checkSerialization(Check.encode); var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 }); try std.testing.expectEqualStrings("null", try stringifyOrDefault(failing.allocator(), null, "null")); } test "request allocation failure is not a validation response" { try @import("response_test.zig").expectRequestAllocationError(handle, "/api/work_pools/", "{\"name\": \"x\", \"type\": \"process\"}"); }