Something went wrong. Try again.
prefect server in zig
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556const 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-handlersconst 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\"}");}