oxios_kernel/kernel_handle/
infra_api.rs1use crate::config::OxiosConfig;
4use crate::cron::{CronJob, CronJobUpdate, CronScheduler};
5use crate::event_bus::{EventBus, KernelEvent};
6use crate::git_layer::{GitLayer, LogEntry};
7use crate::resource_monitor::{ResourceMonitor, ResourceSnapshot};
8use crate::scheduler::{AgentScheduler, ScheduledTask, SchedulerStats};
9use std::sync::Arc;
10use std::time::{Duration, Instant};
11
12pub struct InfraApi {
14 pub(crate) git_layer: Arc<GitLayer>,
15 pub(crate) scheduler: Arc<AgentScheduler>,
16 pub(crate) cron_scheduler: Arc<CronScheduler>,
17 pub(crate) resource_monitor: Arc<ResourceMonitor>,
18 pub(crate) event_bus: EventBus,
19 pub(crate) config: OxiosConfig,
20 pub(crate) start_time: Instant,
21 pub(crate) orchestrator_config: parking_lot::RwLock<crate::config::OrchestratorConfig>,
23}
24
25impl InfraApi {
26 pub fn new(
28 git_layer: Arc<GitLayer>,
29 scheduler: Arc<AgentScheduler>,
30 cron_scheduler: Arc<CronScheduler>,
31 resource_monitor: Arc<ResourceMonitor>,
32 event_bus: EventBus,
33 config: OxiosConfig,
34 start_time: Instant,
35 ) -> Self {
36 Self {
37 git_layer,
38 scheduler,
39 cron_scheduler,
40 resource_monitor,
41 event_bus,
42 config,
43 start_time,
44 orchestrator_config: parking_lot::RwLock::new(
45 crate::config::OrchestratorConfig::default(),
46 ),
47 }
48 }
49 pub fn git(&self) -> &GitLayer {
51 &self.git_layer
52 }
53
54 pub fn git_log(&self, max: usize) -> anyhow::Result<Vec<LogEntry>> {
56 self.git_layer.log(max)
57 }
58
59 pub fn git_tag(&self, name: &str, message: &str) -> anyhow::Result<()> {
61 self.git_layer.tag(name, message)
62 }
63
64 pub fn git_restore(&self, path: &str, hash: &str) -> anyhow::Result<()> {
66 self.git_layer.restore_file(path, hash)
67 }
68
69 pub fn git_verify(&self) -> anyhow::Result<bool> {
71 self.git_layer.verify()
72 }
73
74 pub fn git_tags(&self) -> anyhow::Result<Vec<String>> {
76 self.git_layer.list_tags()
77 }
78
79 pub fn scheduler_stats(&self) -> SchedulerStats {
81 self.scheduler.stats()
82 }
83
84 pub fn queued_tasks(&self) -> Vec<ScheduledTask> {
86 self.scheduler.queued_tasks()
87 }
88
89 pub fn running_tasks(&self) -> Vec<ScheduledTask> {
91 self.scheduler.running_tasks()
92 }
93
94 pub async fn add_cron(&self, job: CronJob) -> anyhow::Result<uuid::Uuid> {
96 self.cron_scheduler.add_job(job).await
97 }
98
99 pub fn get_cron(&self, id: uuid::Uuid) -> Option<CronJob> {
101 self.cron_scheduler.get_job(id)
102 }
103
104 pub async fn update_cron(&self, id: uuid::Uuid, update: CronJobUpdate) -> anyhow::Result<()> {
106 self.cron_scheduler.update_job(id, update).await
107 }
108
109 pub async fn remove_cron(&self, id: uuid::Uuid) -> anyhow::Result<()> {
111 self.cron_scheduler.remove_job(id).await
112 }
113
114 pub fn trigger_cron(&self, id: uuid::Uuid) -> anyhow::Result<CronJob> {
116 self.cron_scheduler.trigger_job(id)
117 }
118
119 pub async fn complete_cron(&self, id: uuid::Uuid, success: bool, summary: String) {
121 self.cron_scheduler
122 .mark_job_completed(id, success, summary)
123 .await
124 }
125
126 pub fn list_crons(&self) -> Vec<CronJob> {
128 self.cron_scheduler.list_jobs()
129 }
130
131 pub fn resource_snapshot(&self) -> ResourceSnapshot {
133 self.resource_monitor.snapshot()
134 }
135
136 pub fn resource_history(&self, last_n: usize) -> Vec<ResourceSnapshot> {
138 self.resource_monitor.history(last_n)
139 }
140
141 pub fn is_overloaded(&self) -> bool {
143 self.resource_monitor.is_overloaded()
144 }
145
146 pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<KernelEvent> {
148 self.event_bus.subscribe()
149 }
150
151 pub fn publish(&self, event: KernelEvent) -> anyhow::Result<()> {
153 self.event_bus
154 .publish(event)
155 .map_err(|e| anyhow::anyhow!("broadcast error: {e}"))
156 }
157
158 pub fn config(&self) -> &OxiosConfig {
160 &self.config
161 }
162
163 pub fn scheduler(&self) -> &Arc<AgentScheduler> {
165 &self.scheduler
166 }
167
168 pub fn resource_monitor(&self) -> &Arc<ResourceMonitor> {
170 &self.resource_monitor
171 }
172
173 pub fn update_orchestrator_config(&self, config: crate::config::OrchestratorConfig) {
175 *self.orchestrator_config.write() = config;
176 }
177
178 pub fn uptime(&self) -> Duration {
180 self.start_time.elapsed()
181 }
182}