const std = @import("std"); const runtime = @import("runtime.zig"); const wap = @import("wap"); const posix = std.posix; const db = @import("db/sqlite.zig"); const backend = @import("db/backend.zig"); const broker = @import("broker.zig"); const lease_storage = @import("leases/storage.zig"); const routes = @import("api/routes.zig"); const static_ui = @import("api/static_ui.zig"); const events = @import("api/events.zig"); const logs_api = @import("api/logs.zig"); const task_runs_subs = @import("api/task_runs_subscriptions.zig"); const worker_channel = @import("api/worker_channel.zig"); const task_workers_mod = @import("task_workers.zig"); const observability = @import("observability.zig"); const log = @import("logging.zig"); const services_mod = @import("services.zig"); const docket_runtime = @import("docket_runtime.zig"); const env = @import("utilities/env.zig"); const auth_util = @import("api/auth.zig"); const settings = @import("utilities/settings.zig"); const time_util = @import("utilities/time.zig"); const logfire = @import("logfire"); const mem = std.mem; /// Override std.Options.debug_io with our explicit Threaded instance so /// std.debug.print is thread-safe when multiple OS threads exist. pub const std_options: std.Options = .{ .log_level = .debug, .logFn = log.stdLog }; pub const std_options_debug_threaded_io: ?*std.Io.Threaded = &runtime.debug_io_backend; /// owns the services for this process; allocated in main() once io is set up. var services: ?*services_mod.Services = null; // Graceful shutdown state var shutdown_requested: bool = false; // Owned server handle so the signal handler can stop it. var wap_server: ?*wap.Server = null; var lf: ?*logfire.Logfire = null; // Combined WebSocket routes from each API module that exposes streams. const combined_ws_routes: []const wap.ws.Route = &events_ws_routes ++ logs_ws_routes ++ task_runs_subs_ws_routes ++ worker_channel_ws_routes; // Pull each module's route table into a comptime-known fixed array so we // can `++` them. Adding a new stream module: add another const + extend // the concat above. const events_ws_routes = blk: { const src = events.wapRoutes(); var arr: [src.len]wap.ws.Route = undefined; for (src, 0..) |r, i| arr[i] = r; break :blk arr; }; const logs_ws_routes = blk: { const src = logs_api.wapRoutes(); var arr: [src.len]wap.ws.Route = undefined; for (src, 0..) |r, i| arr[i] = r; break :blk arr; }; const task_runs_subs_ws_routes = blk: { const src = task_runs_subs.wapRoutes(); var arr: [src.len]wap.ws.Route = undefined; for (src, 0..) |r, i| arr[i] = r; break :blk arr; }; const worker_channel_ws_routes = blk: { const src = worker_channel.wapRoutes(); var arr: [src.len]wap.ws.Route = undefined; for (src, 0..) |r, i| arr[i] = r; break :blk arr; }; fn blockShutdownSignals() void { var set: std.c.sigset_t = undefined; _ = std.c.sigemptyset(&set); _ = std.c.sigaddset(&set, std.c.SIG.INT); _ = std.c.sigaddset(&set, std.c.SIG.TERM); var old: std.c.sigset_t = undefined; _ = std.c.pthread_sigmask(std.c.SIG.BLOCK, &set, &old); } fn shutdownWatcher(_: void) void { var set: std.c.sigset_t = undefined; _ = std.c.sigemptyset(&set); _ = std.c.sigaddset(&set, std.c.SIG.INT); _ = std.c.sigaddset(&set, std.c.SIG.TERM); var sig: c_int = 0; _ = std.c.sigwait(&set, &sig); shutdown_requested = true; log.shutdownBegin(); if (wap_server) |s| s.stop(); } fn setupSignalHandlers() void { // Block in this (eventually main) thread; the mask is inherited by any // thread spawned later, including the wap server workers and the // broker consumer threads. Together with the dedicated watcher below, // this guarantees only one thread (the watcher) ever runs sigwait. blockShutdownSignals(); const t = std.Thread.spawn(.{}, shutdownWatcher, .{{}}) catch |err| { log.err("shutdown", "failed to spawn signal watcher: {}", .{err}); return; }; t.detach(); } fn handleRequest(req: *wap.Request, res: *wap.Response) anyerror!void { const method = @tagName(req.method); const path = req.path; observability.requests_total.inc(); const span = logfire.httpSpan(method, path, .{}); defer span.end(); if (static_ui.tryServe(req, res) catch |err| { span.recordError(err); observability.requests_errors_total.inc(); log.err("server", "{s} {s} - static UI error: {}", .{ method, path, err }); if (!res.is_sent) { _ = res.setStatus(.internal_server_error) .setHeader("content-type", "application/json"); try res.sendBody("{\"detail\":\"internal error\"}"); } return; }) { log.debug("server", "{s} {s} - ui", .{ method, path }); return; } if (!auth_util.authorizeRequest(req)) { _ = res.setStatus(.unauthorized) .setHeader("content-type", "application/json"); try res.sendBody("{\"exception_message\":\"Unauthorized\"}"); return; } routes.handle(req, res) catch |err| { span.recordError(err); observability.requests_errors_total.inc(); log.err("server", "{s} {s} - error: {}", .{ method, path, err }); if (!res.is_sent) { _ = res.setStatus(.internal_server_error) .setHeader("content-type", "application/json"); try res.sendBody("{\"detail\":\"internal error\"}"); } return; }; log.debug("server", "{s} {s}", .{ method, path }); } const Command = enum { server, services_only, help }; const Args = struct { command: Command = .server, /// HA webapp tier: only `webserver_only` services run (the broadcaster /// is the only one today). Loop services + batch consumers run elsewhere. webserver_only: bool = false, }; fn parseArgs(process_args: std.process.Args) Args { var args = Args{}; var iter = process_args.iterate(); _ = iter.next(); // skip program name while (iter.next()) |arg| { if (std.mem.eql(u8, arg, "services")) { args.command = .services_only; } else if (std.mem.eql(u8, arg, "--webserver-only")) { args.webserver_only = true; } else if (std.mem.eql(u8, arg, "--no-services")) { // Deprecated alias for --webserver-only. Kept so existing // compose.yml / Dockerfile CMDs don't break mid-migration. args.webserver_only = true; } else if (std.mem.eql(u8, arg, "--help") or std.mem.eql(u8, arg, "-h")) { args.command = .help; } } return args; } fn printHelp() void { const help = \\prefect-server - zig implementation of prefect server \\ \\usage: \\ prefect-server [options] run API server + all services (default; dev / single-binary) \\ prefect-server services run background services only (no API server) \\ prefect-server --webserver-only HA webapp tier: API + webserver-tier services only \\ (broker-consumer event broadcaster; runs in every replica) \\ \\service tiers mirror python prefect's RunInWebservers / RunInEphemeralServers / Service split. \\ \\options: \\ --webserver-only run API server with webserver-tier services only (HA mode) \\ --no-services deprecated alias for --webserver-only \\ --help, -h show this help message \\ \\environment variables: \\ PREFECT_SERVER_PORT port to listen on (default: 4200) \\ PREFECT_SERVER_API_HOST host to bind to (default: 127.0.0.1) \\ PREFECT_DATABASE_BACKEND sqlite or postgres (default: sqlite) \\ PREFECT_DATABASE_PATH sqlite database path (default: prefect.db) \\ PREFECT_DATABASE_URL postgres connection URL \\ PREFECT_BROKER_BACKEND memory or redis (default: memory) \\ PREFECT_REDIS_MESSAGING_HOST redis host (default: localhost) \\ PREFECT_REDIS_MESSAGING_PORT redis port (default: 6379) \\ PREFECT_SERVER_LOGGING_LEVEL DEBUG, INFO, WARNING, ERROR (default: INFO) \\ \\horizontal scaling: \\ prefect-server --no-services # run N API server instances \\ prefect-server services # run 1 background services instance \\ ; std.debug.print("{s}", .{help}); } pub fn main(init: std.process.Init.Minimal) void { const allocator = std.heap.smp_allocator; runtime.init(allocator); defer runtime.deinit(); // Load prefect.toml (CWD by default, PREFECT_CONFIG_PATH overrides). // Must run before log.init / first env.getenv so the file's overrides // are visible. Missing file is a no-op. settings.init(allocator); defer settings.deinit(); log.init(); defer log.finishPresentation(); // initialize logfire telemetry lf = logfire.configure(.{ .service_name = "prefect-server", .service_version = "0.0.1", }) catch |err| blk: { log.warn("telemetry", "logfire init failed: {} - continuing without tracing", .{err}); break :blk null; }; defer if (lf) |l| { l.flush(); l.shutdown(); }; const args = parseArgs(init.args); switch (args.command) { .help => { printHelp(); return; }, .services_only => { runServicesOnly() catch |err| { if (shutdown_requested) return; // clean shutdown log.startupFail(err); log.err("server", "fatal: {}", .{err}); std.process.exit(1); }; }, .server => { const tier: services_mod.Tier = if (args.webserver_only) .webserver_only else .all; runServer(tier) catch |err| { if (shutdown_requested) return; // clean shutdown log.startupFail(err); log.err("server", "fatal: {}", .{err}); std.process.exit(1); }; }, } } fn runServicesOnly() !void { setupSignalHandlers(); log.startupBegin(); try initInfra(); defer { log.info("shutdown", "cleaning up infrastructure...", .{}); deinitInfra(); } var services_instance = services_mod.Services.init(std.heap.smp_allocator, runtime.app_io, .all); services = &services_instance; defer services = null; log.startupStage(.orchestration, "starting services"); try services_instance.startAll(); log.startupDone(.orchestration, "services started"); defer { log.info("shutdown", "stopping services...", .{}); services_instance.stopAll(); } // the services process has no API, but it writes flow run states (late // marking, cancellation cleanup, pause expiry) and counts them, so it // serves its own registry on the metrics port for prometheus to add to // the API process's. cluster-internal: no auth, no database gauges. const metrics_port: u16 = blk: { const raw = env.getenv("PREFECT_SERVER_METRICS_PORT") orelse "9000"; break :blk std.fmt.parseInt(u16, raw, 10) catch 9000; }; const host = env.getenv("PREFECT_SERVER_API_HOST") orelse "127.0.0.1"; log.startupStage(.api, "binding listener"); var server = try wap.Server.init(std.heap.smp_allocator, runtime.appIo(), .{ .port = metrics_port, .address = host, .handler = handleMetricsOnly, .on_ready = log.startupReady, .ready_label = "metrics", .workers = 1, }); defer server.deinit(); wap_server = &server; defer wap_server = null; try server.run(); log.shutdownBegin(); log.shutdownProgress(.api, "connections closed", true); } /// The services process's only HTTP surface: this process's registry and a /// liveness answer. Everything else is 404 so nothing mistakes it for the API. fn handleMetricsOnly(req: *wap.Request, res: *wap.Response) anyerror!void { if (std.mem.eql(u8, req.path, "/metrics") or std.mem.eql(u8, req.path, "/api/metrics")) { var buf: std.Io.Writer.Allocating = .init(res.arena); observability.writeMetrics(&buf.writer, runtime.appIo()) catch { _ = res.setStatus(.internal_server_error).setHeader("content-type", "text/plain"); try res.sendBody("metrics formatter failed\n"); return; }; _ = res.setHeader("content-type", "text/plain; version=0.0.4; charset=utf-8"); try res.sendBody(buf.written()); return; } if (std.mem.eql(u8, req.path, "/health")) { _ = res.setHeader("content-type", "application/json"); try res.sendBody("true"); return; } _ = res.setStatus(.not_found).setHeader("content-type", "text/plain"); try res.sendBody("services process: only /metrics and /health are served here\n"); } fn runServer(tier: services_mod.Tier) !void { const port: u16 = blk: { const port_str = env.getenv("PREFECT_SERVER_API_PORT") orelse env.getenv("PREFECT_SERVER_PORT") orelse "4200"; break :blk std.fmt.parseInt(u16, port_str, 10) catch 4200; }; const host = env.getenv("PREFECT_SERVER_API_HOST") orelse "127.0.0.1"; const keepalive_timeout_seconds: u32 = blk: { const raw = env.getenv("PREFECT_SERVER_API_KEEPALIVE_TIMEOUT") orelse "5"; break :blk std.fmt.parseInt(u32, raw, 10) catch 5; }; setupSignalHandlers(); log.startupBegin(); try initInfra(); defer { log.info("shutdown", "cleaning up infrastructure...", .{}); deinitInfra(); } var services_instance = services_mod.Services.init(std.heap.smp_allocator, runtime.app_io, tier); services = &services_instance; log.info("server", "service tier: {s}", .{@tagName(tier)}); log.startupStage(.orchestration, "starting services"); try services_instance.startAll(); log.startupDone(.orchestration, "services started"); defer { log.info("shutdown", "stopping services...", .{}); services_instance.stopAll(); services = null; } log.startupStage(.api, "binding listener"); var server = try wap.Server.init(std.heap.smp_allocator, runtime.appIo(), .{ .port = port, .address = host, .handler = handleRequest, .on_ready = log.startupReady, .ready_label = "API", .ws_routes = combined_ws_routes, .workers = 4, .max_body_size = 16 * 1024 * 1024, .keepalive_timeout_seconds = keepalive_timeout_seconds, }); defer server.deinit(); wap_server = &server; defer wap_server = null; try server.run(); log.shutdownBegin(); log.shutdownProgress(.api, "connections closed", true); } fn initInfra() !void { log.startupStage(.storage, "connecting database"); log.info("database", "initializing...", .{}); try db.init(); log.info("database", "ready", .{}); log.startupDone(.storage, @tagName(backend.getDialect())); log.startupStage(.messaging, "starting broker and leases"); const broker_dialect: broker.Dialect = blk: { const broker_str = env.getenv("PREFECT_BROKER_BACKEND") orelse "memory"; if (std.mem.eql(u8, broker_str, "redis")) { break :blk .redis; } break :blk .memory; }; broker.initBroker(std.heap.page_allocator, broker_dialect) catch |err| { log.err("broker", "failed to initialize: {}", .{err}); return err; }; log.info("broker", "ready ({s})", .{@tagName(broker_dialect)}); // Subscribe the worker-channel snapshot fanout to the broker now that // it's initialized. Must run AFTER broker.initBroker — the consumer // group is created lazily via subscribeEphemeral. worker_channel.init() catch |err| { log.err("worker_channel", "failed to initialize snapshot subscriber: {}", .{err}); return err; }; // Initialize lease storage (matches broker backend: redis for HA, database otherwise) lease_storage.init() catch |err| { log.err("leases", "failed to initialize lease storage: {}", .{err}); return err; }; // Initialize the docket runtime — memory:// or redis://... per // PREFECT_BROKER_BACKEND, mirroring the broker's dialect choice. docket_runtime.init(std.heap.page_allocator, runtime.appIo()) catch |err| { log.err("docket", "failed to initialize: {}", .{err}); return err; }; const docket_url = docket_runtime.resolveUrl(std.heap.page_allocator) catch "?"; log.info("docket", "ready (url={s})", .{docket_url}); // Background-task queue + worker tracker. Backend dialect mirrors the // broker — memory for single-replica, redis for multi-replica safety // (PrefectHQ/prefect#21218). const tw_dialect: task_workers_mod.Dialect = if (broker_dialect == .redis) .redis else .memory; try task_workers_mod.init(std.heap.page_allocator, runtime.appIo(), tw_dialect); log.startupDone(.messaging, @tagName(broker_dialect)); } fn deinitInfra() void { log.shutdownProgress(.messaging, "closing broker and queues", false); task_workers_mod.deinit(); worker_channel.deinit(); docket_runtime.deinit(); broker.deinitBroker(); log.shutdownProgress(.messaging, "connections closed", true); log.shutdownProgress(.storage, "closing database", false); db.close(); log.shutdownProgress(.storage, "database closed", true); } test "handler propagates allocation failure while preparing an error response" { const httpz = @import("httpz"); var request = httpz.testing.init(.{}); defer request.deinit(); request.url("/api/__allocation_failure__"); var failing = std.testing.FailingAllocator.init(std.testing.allocator, .{ .fail_index = 0 }); request.res.arena = failing.allocator(); var req = wap.Request.from(request.req); var res: wap.Response = .{ .inner = request.res, .arena = req.arena }; try std.testing.expectError(error.OutOfMemory, handleRequest(&req, &res)); try std.testing.expect(failing.has_induced_failure); try request.expectStatusCode(.internal_server_error); try std.testing.expect(!res.is_sent); } test { _ = @import("broker/core.zig"); _ = @import("db/atproto/repo.zig"); _ = @import("db/backend.zig"); _ = @import("db/dialect.zig"); _ = @import("db/flows.zig"); _ = @import("db/events.zig"); _ = @import("db/flow_runs.zig"); _ = @import("db/task_runs.zig"); _ = @import("db/block_types.zig"); _ = @import("db/block_schemas.zig"); _ = @import("db/block_documents.zig"); _ = @import("db/flow_run_inputs.zig"); _ = @import("db/deployment_schedules.zig"); _ = @import("db/schema/sqlite.zig"); _ = @import("db/configuration.zig"); _ = @import("db/work_pools.zig"); _ = @import("utilities/hashing.zig"); _ = @import("utilities/hydration.zig"); _ = @import("utilities/parameter_schema.zig"); _ = @import("utilities/text_search.zig"); _ = @import("utilities/time.zig"); _ = @import("api/auth.zig"); _ = @import("api/bulk_filter.zig"); _ = @import("api/flow_runs.zig"); _ = @import("api/artifacts.zig"); _ = @import("api/automations.zig"); _ = @import("api/task_runs.zig"); _ = @import("api/block_documents.zig"); _ = @import("api/block_schemas.zig"); _ = @import("api/block_types.zig"); _ = @import("api/deployments.zig"); _ = @import("api/deployment_schedules.zig"); _ = @import("api/events_api.zig"); _ = @import("api/flows.zig"); _ = @import("api/saved_searches.zig"); _ = @import("api/variables.zig"); _ = @import("api/work_pools.zig"); _ = @import("api/worker_channel.zig"); _ = @import("orchestration/types.zig"); _ = @import("orchestration/flow_rules.zig"); _ = @import("orchestration/transforms.zig"); _ = @import("orchestration/estimates.zig"); _ = @import("services/event_broadcaster.zig"); _ = @import("services/log_broadcaster.zig"); _ = @import("services/pause_expirations.zig"); _ = @import("services/automations.zig"); _ = @import("services/automations/actions.zig"); _ = @import("services/automations/templates.zig"); _ = @import("services/automations/triggers.zig"); } test "flow handlers allocate database results in the request owner" { backend.db = try backend.Backend.initSqlite(std.testing.allocator, ":memory:"); defer backend.db.deinit(); try backend.db.exec("CREATE TABLE flow (id TEXT, created TEXT, updated TEXT, name TEXT, tags TEXT)", .{}); try backend.db.exec("INSERT INTO flow VALUES ('00000000-0000-4000-8000-000000000001', '2026-10-11T00:00:00.000000Z', '2026-10-11T00:00:00.000000Z', 'owned-flow', '[]')", .{}); const httpz = @import("httpz"); var request = httpz.testing.init(.{}); defer request.deinit(); request.url("/api/flows/00000000-0000-4000-8000-000000000001"); var owner = std.heap.ArenaAllocator.init(std.testing.allocator); defer owner.deinit(); var counted = std.testing.FailingAllocator.init(owner.allocator(), .{}); var req = wap.Request.from(request.req); req.arena = counted.allocator(); var res: wap.Response = .{ .inner = request.res, .arena = req.arena }; try @import("api/flows.zig").handle(&req, &res); try request.expectStatusCode(.ok); try std.testing.expect(counted.alloc_index > 0); try std.testing.expect(std.mem.indexOf(u8, request.res.body, "owned-flow") != null); }