// supervisor.reef — Service lifecycle management // // Handles starting, stopping, and restarting services using process // contracts. Implements restart policies (always, on-failure, never). import config.{ServiceDef, ServiceType, RestartPolicy, StopMethod} import contract.{Contract, setup_template, get_latest_contract, kill_contract} // Runtime state of a service enum ServiceState { Disabled, // not enabled Waiting, // waiting for dependencies Starting, // exec'd, waiting for confirmation Running, // running (has contract) Stopping, // stop signal sent, waiting for exit Stopped, // cleanly stopped Failed, // exited with error Maintenance, // exceeded max retries } // Runtime info for a managed service struct ServiceRuntime { def: ServiceDef, state: ServiceState, contract: Option, pid: Option, restart_count: i32, last_exit_code: Option, last_start_time: i64, // unix timestamp } // Start a service — fork, set up contract, exec fn start_service(svc: ServiceDef) -> Result { // TODO: implement // 1. setup_template() for contract // 2. fork() // 3. In child: setsid(), exec(svc.start_cmd) // 4. In parent: get_latest_contract(), ct_tmpl_clear() // 5. Return ServiceRuntime with Running state Err("not yet implemented") } // Stop a service fn stop_service(rt: ServiceRuntime) -> Result { // TODO: implement // Based on svc.stop_method: // Contract: kill_contract(contract_id, signal) // Exec: fork/exec the stop command // Wait up to stop_timeout, then SIGKILL Err("not yet implemented") } // Handle a contract event (service exited) fn handle_exit(rt: ServiceRuntime, exit_code: i32) -> ServiceRuntime { // TODO: implement // Based on restart_policy: // Always: schedule restart after delay // OnFailure: restart only if exit_code != 0 // Never: mark as Stopped // Check max_retries — enter Maintenance if exceeded rt }