Something went wrong. Try again.
prefect server in zig
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346const std = @import("std");const runtime = @import("../runtime.zig");const wap = @import("wap");const mem = std.mem;const json = std.json;
const db = @import("../db/sqlite.zig");const uuid_util = @import("../utilities/uuid.zig");const time_util = @import("../utilities/time.zig");const pool_helpers = @import("work_pools.zig");const worker_channel = @import("worker_channel.zig");
// ===== typed bodies — mirror python's WorkQueueCreate / Update =====
const WorkQueueCreateBody = struct { name: []const u8, description: ?[]const u8 = null, is_paused: bool = false, concurrency_limit: ?i64 = null, priority: ?i64 = null, filter: ?json.Value = null, // accepted but unused (deprecated in python)};
const WorkQueueUpdateBody = struct { name: ?[]const u8 = null, description: ?[]const u8 = null, is_paused: ?bool = null, concurrency_limit: ?i64 = null, priority: ?i64 = null, filter: ?json.Value = null,};
const FilterBody = struct { limit: ?usize = null, offset: ?usize = null, work_queues: ?json.Value = null, sort: ?[]const u8 = null,};
pub fn create(req: *wap.Request, res: *wap.Response, target: []const u8) !void { const alloc = req.arena;
const pool_name = pool_helpers.extractPoolName(target) orelse { try pool_helpers.sendError(res, .bad_request, "work pool name required"); return; };
const pool = try db.work_pools.getByName(alloc, pool_name) orelse { try pool_helpers.sendError(res, .not_found, "Work pool not found."); return; };
const body = req.parseJson(WorkQueueCreateBody) catch |err| { return switch (err) { error.OutOfMemory => return err, error.UnknownField => pool_helpers.sendValidationError(res, "unknown field in request body"), else => pool_helpers.sendValidationError(res, "invalid json"), }; };
if (try db.work_queues.getByPoolAndName(alloc, pool.id, body.name)) |_| { try pool_helpers.sendError(res, .conflict, "Work queue already exists."); return; }
const description = body.description orelse ""; const priority = body.priority orelse (db.work_queues.nextPriority(pool.id) catch 1);
var id_buf: [36]u8 = undefined; const queue_id = uuid_util.generate(runtime.appIo(), &id_buf);
var ts_buf: [32]u8 = undefined; const now = time_util.timestamp(runtime.appIo(), &ts_buf);
const status: db.work_queues.Status = if (body.is_paused) .paused else .not_ready;
db.work_queues.insert( queue_id, body.name, description, body.is_paused, body.concurrency_limit, priority, pool.id, status, now, ) catch { try pool_helpers.sendError(res, .internal_server_error, "insert failed"); return; };
const queue = try db.work_queues.getByPoolAndName(alloc, pool.id, body.name) orelse { try pool_helpers.sendError(res, .internal_server_error, "queue not found after insert"); return; };
const resp = writeQueue(alloc, queue, pool_name) catch { try pool_helpers.sendError(res, .internal_server_error, "serialize error"); return; };
try worker_channel.publishInvalidation(alloc, pool.id, "work_queue_created");
_ = res.setStatus(.created); _ = res.setHeader("content-type", "application/json"); try res.sendBody(resp);}
pub fn get(res: *wap.Response, target: []const u8) !void { const alloc = res.arena;
const pool_name = pool_helpers.extractPoolName(target) orelse { try pool_helpers.sendError(res, .bad_request, "work pool name required"); return; };
const queue_name = pool_helpers.extractQueueName(target) orelse { try pool_helpers.sendError(res, .bad_request, "queue name required"); return; };
const pool = try db.work_pools.getByName(alloc, pool_name) orelse { try pool_helpers.sendError(res, .not_found, "Work pool not found."); return; };
if (try db.work_queues.getByPoolAndName(alloc, pool.id, queue_name)) |queue| { const resp = writeQueue(alloc, queue, pool_name) catch { try pool_helpers.sendError(res, .internal_server_error, "serialize error"); return; }; _ = res.setHeader("content-type", "application/json"); try res.sendBody(resp); } else { try pool_helpers.sendError(res, .not_found, "Work queue not found."); }}
pub fn update(req: *wap.Request, res: *wap.Response, target: []const u8) !void { const alloc = req.arena;
const pool_name = pool_helpers.extractPoolName(target) orelse { try pool_helpers.sendError(res, .bad_request, "work pool name required"); return; };
const queue_name = pool_helpers.extractQueueName(target) orelse { try pool_helpers.sendError(res, .bad_request, "queue name required"); return; };
const pool = try db.work_pools.getByName(alloc, pool_name) orelse { try pool_helpers.sendError(res, .not_found, "Work pool not found."); return; };
const body = req.parseJson(WorkQueueUpdateBody) catch |err| { return switch (err) { error.OutOfMemory => return err, error.UnknownField => pool_helpers.sendValidationError(res, "unknown field in request body"), else => pool_helpers.sendValidationError(res, "invalid json"), }; };
var new_status: ?db.work_queues.Status = null; if (body.is_paused) |paused| { new_status = if (paused) .paused else .not_ready; }
var ts_buf: [32]u8 = undefined; const now = time_util.timestamp(runtime.appIo(), &ts_buf);
const did_update = db.work_queues.updateByPoolAndName( pool.id, queue_name, body.name, body.description, body.is_paused, body.concurrency_limit, body.priority, new_status, now, ) catch { try pool_helpers.sendError(res, .internal_server_error, "update failed"); return; };
if (!did_update) { try pool_helpers.sendError(res, .not_found, "Work queue not found."); return; }
try worker_channel.publishInvalidation(alloc, pool.id, "work_queue_updated");
try res.sendEmpty(.no_content);}
pub fn delete(res: *wap.Response, target: []const u8) !void { const alloc = res.arena;
const pool_name = pool_helpers.extractPoolName(target) orelse { try pool_helpers.sendError(res, .bad_request, "work pool name required"); return; };
const queue_name = pool_helpers.extractQueueName(target) orelse { try pool_helpers.sendError(res, .bad_request, "queue name required"); return; };
const pool = try db.work_pools.getByName(alloc, pool_name) orelse { try pool_helpers.sendError(res, .not_found, "Work pool not found."); return; };
if (pool.default_queue_id) |default_id| { if (try db.work_queues.getByPoolAndName(alloc, pool.id, queue_name)) |queue| { if (mem.eql(u8, queue.id, default_id)) { try pool_helpers.sendError(res, .bad_request, "Cannot delete the default work queue."); return; } } }
const deleted = db.work_queues.deleteByPoolAndName(pool.id, queue_name) catch { try pool_helpers.sendError(res, .internal_server_error, "delete failed"); return; };
if (!deleted) { try pool_helpers.sendError(res, .not_found, "Work queue not found."); return; }
try worker_channel.publishInvalidation(alloc, pool.id, "work_queue_deleted");
try res.sendEmpty(.no_content);}
pub fn filter(req: *wap.Request, res: *wap.Response, target: []const u8) !void { const alloc = req.arena;
const pool_name = pool_helpers.extractPoolName(target) orelse { try pool_helpers.sendError(res, .bad_request, "work pool name required"); return; };
const pool = try db.work_pools.getByName(alloc, pool_name) orelse { try pool_helpers.sendError(res, .not_found, "Work pool not found."); return; };
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 queues = db.work_queues.listByPool(alloc, pool.id, limit, offset) catch { try pool_helpers.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 pool_helpers.sendError(res, .internal_server_error, "serialize error"); return; };
for (queues) |queue| { try writeQueueObject(&jw, queue, pool_name); }
try jw.endArray();
_ = res.setHeader("content-type", "application/json"); try res.sendBody(try output.toOwnedSlice());}
// JSON serialization
pub fn writeQueue(alloc: std.mem.Allocator, queue: db.work_queues.WorkQueueRow, pool_name: []const u8) ![]const u8 { var output: std.Io.Writer.Allocating = .init(alloc); var jw: json.Stringify = .{ .writer = &output.writer }; try writeQueueObject(&jw, queue, pool_name); return output.toOwnedSlice();}
fn writeQueueObject(jw: *json.Stringify, queue: db.work_queues.WorkQueueRow, pool_name: []const u8) !void { try jw.beginObject();
try jw.objectField("id"); try jw.write(queue.id);
try jw.objectField("created"); try jw.write(queue.created);
try jw.objectField("updated"); try jw.write(queue.updated);
try jw.objectField("name"); try jw.write(queue.name);
try jw.objectField("description"); try jw.write(queue.description);
try jw.objectField("is_paused"); try jw.write(queue.is_paused);
try jw.objectField("concurrency_limit"); if (queue.concurrency_limit) |c| { try jw.write(c); } else { try jw.write(null); }
try jw.objectField("priority"); try jw.write(queue.priority);
try jw.objectField("work_pool_id"); try jw.write(queue.work_pool_id);
try jw.objectField("work_pool_name"); try jw.write(pool_name);
try jw.objectField("last_polled"); if (queue.last_polled) |lp| { try jw.write(lp); } else { try jw.write(null); }
try jw.objectField("status"); try jw.write(queue.status.toString());
try jw.endObject();}