Something went wrong. Try again.
prefect server in zig
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272const std = @import("std");const runtime = @import("../runtime.zig");const wap = @import("wap");const json = std.json;
const db = @import("../db/sqlite.zig");const time_util = @import("../utilities/time.zig");const pool_helpers = @import("work_pools.zig");
// ===== typed body — mirror python's get_scheduled_flow_runs args =====
const GetScheduledFlowRunsBody = struct { work_queue_names: ?[]const []const u8 = null, scheduled_before: ?[]const u8 = null, scheduled_after: ?[]const u8 = null, limit: ?usize = null,};
/// Handle POST /work_pools/{name}/get_scheduled_flow_runspub fn handle(req: *wap.Request, res: *wap.Response, target: []const u8) !void { const logging = @import("../logging.zig"); logging.info("work_pool_schedule", "request fd={d} path={s} bytes={d}", .{ req.connection_fd, target, req.body.len }); const alloc = req.arena;
const pool_name = 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 scheduled_before: ?[]const u8 = null; var work_queue_names: []const []const u8 = &.{};
if (req.body.len > 0) { if (req.parseJson(GetScheduledFlowRunsBody)) |body| { if (body.limit) |l| limit = l; if (body.scheduled_before) |s| scheduled_before = s; if (body.work_queue_names) |names| work_queue_names = names; } else |parse_err| { if (parse_err == error.OutOfMemory) return parse_err; } }
// Default scheduled_before to now if not provided var ts_buf: [32]u8 = undefined; const now = time_util.timestamp(runtime.appIo(), &ts_buf); if (scheduled_before == null) { scheduled_before = now; }
// Get work queues var queue_ids = std.ArrayListUnmanaged([]const u8).empty; var work_queues = std.ArrayListUnmanaged(db.work_queues.WorkQueueRow).empty;
if (work_queue_names.len > 0) { // Filter by specific queue names for (work_queue_names) |qname| { if (try db.work_queues.getByPoolAndName(alloc, pool.id, qname)) |queue| { try queue_ids.append(alloc, queue.id); try work_queues.append(alloc, queue); } } } else { // Get all queues in pool const all_queues = db.work_queues.listByPool(alloc, pool.id, 1000, 0) catch { try pool_helpers.sendError(res, .internal_server_error, "database error"); return; }; for (all_queues) |queue| { try queue_ids.append(alloc, queue.id); try work_queues.append(alloc, queue); } }
// Get scheduled flow runs, honoring per-queue concurrency limits. The // worker immediately transitions returned runs to PENDING and submits // Kubernetes Jobs, so PENDING/RUNNING/CANCELLING rows already occupy slots. var flow_runs_list = std.ArrayListUnmanaged(db.flow_runs.FlowRunRow).empty; for (work_queues.items) |queue| { if (queue.is_paused) continue; if (flow_runs_list.items.len >= limit) break;
var queue_limit = limit - flow_runs_list.items.len; if (queue.concurrency_limit) |configured| { if (configured <= 0) continue; const active = try db.work_queues.countActiveSlots(queue.id); const max_slots: usize = @intCast(configured); if (active >= max_slots) continue; const available = max_slots - active; if (available < queue_limit) queue_limit = available; }
const queue_runs = db.flow_runs.getScheduledByWorkQueue( alloc, queue.id, scheduled_before, queue_limit, ) catch { try pool_helpers.sendError(res, .internal_server_error, "database error"); return; };
for (queue_runs) |run| { try flow_runs_list.append(alloc, run); if (flow_runs_list.items.len >= limit) break; } }
const flow_runs = flow_runs_list.items;
// Update last_polled for work queues and mark as ready if needed for (work_queues.items) |queue| { const new_status: db.work_queues.Status = if (queue.is_paused) .paused else .ready; _ = try db.work_queues.updateLastPolled(pool.id, queue.name, now, new_status); }
// Mark pool as ready if it was not_ready if (pool.status == .not_ready) { _ = try db.work_pools.updateStatus(pool_name, .ready, now); }
// Mark deployments as ready (deployments for the queues we polled) try db.deployments.markReadyByWorkQueues(queue_ids.items, now);
// Build response as WorkerFlowRunResponse[] 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 (flow_runs) |run| { try writeWorkerFlowRunResponse(&jw, pool.id, run); }
try jw.endArray();
_ = res.setHeader("content-type", "application/json"); try res.sendBody(try output.toOwnedSlice());}
fn extractPoolName(target: []const u8) ?[]const u8 { const prefix = "/api/work_pools/"; if (!std.mem.startsWith(u8, target, prefix)) return null;
const rest = target[prefix.len..]; const end = std.mem.indexOf(u8, rest, "/") orelse return null; if (end == 0) return null;
return rest[0..end];}
fn writeWorkerFlowRunResponse(jw: *json.Stringify, work_pool_id: []const u8, run: db.flow_runs.FlowRunRow) !void { try jw.beginObject();
try jw.objectField("work_pool_id"); try jw.write(work_pool_id);
try jw.objectField("work_queue_id"); try jw.write(run.work_queue_id);
try jw.objectField("flow_run"); try writeFlowRunObject(jw, run);
try jw.endObject();}
fn writeFlowRunObject(jw: *json.Stringify, run: db.flow_runs.FlowRunRow) !void { try jw.beginObject();
try jw.objectField("id"); try jw.write(run.id);
try jw.objectField("created"); try jw.write(run.created);
try jw.objectField("updated"); try jw.write(run.updated);
try jw.objectField("name"); try jw.write(run.name);
try jw.objectField("flow_id"); try jw.write(run.flow_id);
try jw.objectField("state_type"); try jw.write(run.state_type);
try jw.objectField("state_name"); try jw.write(run.state_name);
try jw.objectField("state_timestamp"); try jw.write(run.state_timestamp);
try jw.objectField("parameters"); try jw.beginWriteRaw(); try jw.writer.writeAll(run.parameters); jw.endWriteRaw();
try jw.objectField("tags"); try jw.beginWriteRaw(); try jw.writer.writeAll(run.tags); jw.endWriteRaw();
try jw.objectField("run_count"); try jw.write(run.run_count);
try jw.objectField("expected_start_time"); if (run.expected_start_time) |t| { try jw.write(t); } else { try jw.write(null); }
try jw.objectField("start_time"); if (run.start_time) |t| { try jw.write(t); } else { try jw.write(null); }
try jw.objectField("end_time"); if (run.end_time) |t| { try jw.write(t); } else { try jw.write(null); }
try jw.objectField("total_run_time"); try jw.write(run.total_run_time);
try jw.objectField("deployment_id"); if (run.deployment_id) |d| { try jw.write(d); } else { try jw.write(null); }
try jw.objectField("deployment_version"); if (run.deployment_version) |v| { try jw.write(v); } else { try jw.write(null); }
try jw.objectField("work_queue_name"); if (run.work_queue_name) |n| { try jw.write(n); } else { try jw.write(null); }
try jw.objectField("work_queue_id"); if (run.work_queue_id) |q| { try jw.write(q); } else { try jw.write(null); }
try jw.objectField("auto_scheduled"); try jw.write(run.auto_scheduled);
try jw.endObject();}