forge_orchestration/sdk/
mod.rs1pub mod client;
31pub mod lifecycle;
32pub mod port;
33
34pub use client::{ForgeClient, MetricsReport, start_heartbeat};
35pub use lifecycle::{graceful_shutdown, is_ready, notify_shutdown, ready, shutdown_requested, shutdown_signal};
36pub use port::{allocate_port, allocate_specific_port, allocated_ports, is_port_available, release_port, PortAllocator};
37
38use crate::error::ForgeError;
39
40pub const FORGE_API_ENV: &str = "FORGE_API";
42
43pub const FORGE_ALLOC_ID_ENV: &str = "FORGE_ALLOC_ID";
45
46pub const FORGE_TASK_NAME_ENV: &str = "FORGE_TASK_NAME";
48
49pub fn forge_api_url() -> Option<String> {
51 std::env::var(FORGE_API_ENV).ok()
52}
53
54pub fn alloc_id() -> Option<String> {
56 std::env::var(FORGE_ALLOC_ID_ENV).ok()
57}
58
59pub fn task_name() -> Option<String> {
61 std::env::var(FORGE_TASK_NAME_ENV).ok()
62}
63
64#[derive(Debug, thiserror::Error)]
66pub enum SdkError {
67 #[error("Forge API not configured: set {0} environment variable")]
69 NotConfigured(&'static str),
70
71 #[error("API error: {0}")]
73 Api(String),
74
75 #[error("Port allocation failed: {0}")]
77 PortAllocation(String),
78
79 #[error("Lifecycle error: {0}")]
81 Lifecycle(String),
82
83 #[error("IO error: {0}")]
85 Io(#[from] std::io::Error),
86
87 #[error("Serialization error: {0}")]
89 Serialization(#[from] serde_json::Error),
90
91 #[error("HTTP error: {0}")]
93 Http(#[from] reqwest::Error),
94}
95
96impl SdkError {
97 pub fn api(msg: impl Into<String>) -> Self {
99 Self::Api(msg.into())
100 }
101
102 pub fn port(msg: impl Into<String>) -> Self {
104 Self::PortAllocation(msg.into())
105 }
106
107 pub fn lifecycle(msg: impl Into<String>) -> Self {
109 Self::Lifecycle(msg.into())
110 }
111}
112
113impl From<SdkError> for ForgeError {
114 fn from(err: SdkError) -> Self {
115 ForgeError::Internal(err.to_string())
116 }
117}
118
119pub type SdkResult<T> = std::result::Result<T, SdkError>;