const std = @import("std"); const config = @import("../config.zig"); const http_state = @import("../protocol/http_state.zig"); const runner_log = @import("../runner/log.zig"); const runner_control = @import("../runner/control.zig"); const runtime = @import("../runtime.zig"); const template = @import("../template.zig"); const workflow_host = @import("host.zig"); const workflow_recovery = @import("recovery.zig"); const CookieJar = http_state.CookieJar; const WorkflowHostState = workflow_host.WorkflowHostState; pub const TeardownOutcome = enum { not_present, completed, skipped, failed, }; pub const ScenarioLifecycle = struct { host: *WorkflowHostState, instance: runtime.WorkflowInstance, pub fn init( allocator: std.mem.Allocator, shared_store: *template.VarStore, scenario: *const config.ScenarioConfig, logger: *runner_log.Logger, ) !ScenarioLifecycle { const host = try allocator.create(WorkflowHostState); errdefer allocator.destroy(host); var store = template.VarStore.init(allocator); errdefer store.deinit(); try store.cloneFrom(shared_store); host.* = .{ .allocator = allocator, .store = store, .cookie_jar = CookieJar.init(allocator), .prng = std.Random.DefaultPrng.init(0x5CEDFACE), .set_var_mode = .setup_only, .allow_stop_abort = true, .logger = logger, .log_phase = "setup", }; errdefer host.deinit(); const backend = runtime.fromConfigBackend(scenario.scenario.wasm_backend); var instance = try runtime.WorkflowInstance.create(allocator, scenario.scenario.wasm.?, backend, .{ .user_data = host, .rand_u64 = workflow_host.workflowHostRandU64, .now_nanos = workflow_host.workflowHostNowNanos, .log = workflow_host.workflowHostLog, .get_var = workflow_host.workflowHostGetVar, .set_var = workflow_host.workflowHostSetVar, .get_response_header = workflow_host.workflowHostGetResponseHeader, .check_abort = runner_control.workflowHostCheckAbort, }); errdefer instance.deinit(); return .{ .host = host, .instance = instance, }; } pub fn deinit(self: *ScenarioLifecycle) void { self.instance.deinit(); const allocator = self.host.allocator; self.host.deinit(); allocator.destroy(self.host); self.* = undefined; } pub fn runSetup(self: *ScenarioLifecycle, shared_store: *template.VarStore, scenario: *const config.ScenarioConfig) !void { self.host.set_var_mode = .setup_only; self.host.allow_stop_abort = true; self.host.log_phase = "setup"; self.host.iteration_no = null; const timeout = scenario.execution.setup_timeout; const call_started = workflow_recovery.beginScriptCall(self.host, timeout); const setup_result = self.instance.setup(&.{}) catch { _ = workflow_recovery.finishScriptCall(self.host, call_started, timeout, runner_control.loadStopRequested()); return error.ScenarioSetupFailed; }; const timing = workflow_recovery.finishScriptCall(self.host, call_started, timeout, runner_control.loadStopRequested()); if (timing != .none) return error.ScenarioSetupFailed; if (setup_result) |result| { if (result != 0) return error.ScenarioSetupFailed; shared_store.clearAll(); try shared_store.cloneFrom(&self.host.store); } } pub fn runTeardown(self: *ScenarioLifecycle, scenario: *const config.ScenarioConfig, interrupted: bool) TeardownOutcome { self.host.set_var_mode = .none; self.host.allow_stop_abort = !interrupted; self.host.log_phase = "teardown"; self.host.iteration_no = null; const timeout = teardownTimeoutBudget(scenario, interrupted); const call_started = workflow_recovery.beginScriptCall(self.host, timeout); const teardown_result = self.instance.teardown() catch { const timing = workflow_recovery.finishScriptCall(self.host, call_started, timeout, false); return if (interrupted and timing != .none) .skipped else .failed; }; const timing = workflow_recovery.finishScriptCall(self.host, call_started, timeout, false); if (teardown_result == null) return .not_present; if (interrupted and timing != .none) return .skipped; if (timing != .none) return .failed; if (teardown_result.? != 0) return .failed; return .completed; } }; fn teardownTimeoutBudget(scenario: *const config.ScenarioConfig, interrupted: bool) config.Duration { if (!interrupted) return scenario.execution.setup_timeout; return .{ .nanos = @min(scenario.execution.setup_timeout.nanos, scenario.execution.shutdown_timeout.nanos), }; }