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}
22
23impl InfraApi {
24 pub fn new(
26 git_layer: Arc<GitLayer>,
27 scheduler: Arc<AgentScheduler>,
28 cron_scheduler: Arc<CronScheduler>,
29 resource_monitor: Arc<ResourceMonitor>,
30 event_bus: EventBus,
31 config: OxiosConfig,
32 start_time: Instant,
33 ) -> Self {
34 Self {
35 git_layer,
36 scheduler,
37 cron_scheduler,
38 resource_monitor,
39 event_bus,
40 config,
41 start_time,
42 }
43 }
44 pub fn git(&self) -> &GitLayer {
46 &self.git_layer
47 }
48
49 pub fn git_log(&self, max: usize) -> anyhow::Result<Vec<LogEntry>> {
51 self.git_layer.log(max)
52 }
53
54 pub fn git_tag(&self, name: &str, message: &str) -> anyhow::Result<()> {
56 self.git_layer.tag(name, message)
57 }
58
59 pub fn git_restore(&self, path: &str, hash: &str) -> anyhow::Result<()> {
61 self.git_layer.restore_file(path, hash)
62 }
63
64 pub fn git_verify(&self) -> anyhow::Result<bool> {
66 self.git_layer.verify()
67 }
68
69 pub fn git_tags(&self) -> anyhow::Result<Vec<String>> {
71 self.git_layer.list_tags()
72 }
73
74 pub fn scheduler_stats(&self) -> SchedulerStats {
76 self.scheduler.stats()
77 }
78
79 pub fn queued_tasks(&self) -> Vec<ScheduledTask> {
81 self.scheduler.queued_tasks()
82 }
83
84 pub fn running_tasks(&self) -> Vec<ScheduledTask> {
86 self.scheduler.running_tasks()
87 }
88
89 pub async fn add_cron(&self, job: CronJob) -> anyhow::Result<uuid::Uuid> {
91 self.cron_scheduler.add_job(job).await
92 }
93
94 pub fn get_cron(&self, id: uuid::Uuid) -> Option<CronJob> {
96 self.cron_scheduler.get_job(id)
97 }
98
99 pub async fn update_cron(&self, id: uuid::Uuid, update: CronJobUpdate) -> anyhow::Result<()> {
101 self.cron_scheduler.update_job(id, update).await
102 }
103
104 pub async fn remove_cron(&self, id: uuid::Uuid) -> anyhow::Result<()> {
106 self.cron_scheduler.remove_job(id).await
107 }
108
109 pub fn trigger_cron(&self, id: uuid::Uuid) -> anyhow::Result<CronJob> {
111 self.cron_scheduler.trigger_job(id)
112 }
113
114 pub async fn complete_cron(&self, id: uuid::Uuid, success: bool, summary: String) {
116 self.cron_scheduler
117 .mark_job_completed(id, success, summary)
118 .await
119 }
120
121 pub fn list_crons(&self) -> Vec<CronJob> {
123 self.cron_scheduler.list_jobs()
124 }
125
126 pub fn resource_snapshot(&self) -> ResourceSnapshot {
128 self.resource_monitor.snapshot()
129 }
130
131 pub fn resource_history(&self, last_n: usize) -> Vec<ResourceSnapshot> {
133 self.resource_monitor.history(last_n)
134 }
135
136 pub fn is_overloaded(&self) -> bool {
138 self.resource_monitor.is_overloaded()
139 }
140
141 pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<KernelEvent> {
143 self.event_bus.subscribe()
144 }
145
146 pub fn publish(&self, event: KernelEvent) -> anyhow::Result<()> {
148 self.event_bus
149 .publish(event)
150 .map_err(|e| anyhow::anyhow!("broadcast error: {e}"))
151 }
152
153 pub fn config(&self) -> &OxiosConfig {
155 &self.config
156 }
157
158 pub fn uptime(&self) -> Duration {
160 self.start_time.elapsed()
161 }
162}