Skip to main content

oxios_kernel/kernel_handle/
infra_api.rs

1//! Infra API — Git, scheduler, cron, resources, events, system.
2
3use 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
12/// Infrastructure system calls.
13pub 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    /// Create a new InfraApi.
25    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    /// Get a reference to the GitLayer.
45    pub fn git(&self) -> &GitLayer {
46        &self.git_layer
47    }
48
49    /// Get commit log.
50    pub fn git_log(&self, max: usize) -> anyhow::Result<Vec<LogEntry>> {
51        self.git_layer.log(max)
52    }
53
54    /// Tag current state.
55    pub fn git_tag(&self, name: &str, message: &str) -> anyhow::Result<()> {
56        self.git_layer.tag(name, message)
57    }
58
59    /// Restore file from commit.
60    pub fn git_restore(&self, path: &str, hash: &str) -> anyhow::Result<()> {
61        self.git_layer.restore_file(path, hash)
62    }
63
64    /// Verify git repository integrity.
65    pub fn git_verify(&self) -> anyhow::Result<bool> {
66        self.git_layer.verify()
67    }
68
69    /// List git tags.
70    pub fn git_tags(&self) -> anyhow::Result<Vec<String>> {
71        self.git_layer.list_tags()
72    }
73
74    /// Get scheduler stats.
75    pub fn scheduler_stats(&self) -> SchedulerStats {
76        self.scheduler.stats()
77    }
78
79    /// Get queued tasks.
80    pub fn queued_tasks(&self) -> Vec<ScheduledTask> {
81        self.scheduler.queued_tasks()
82    }
83
84    /// Get running tasks.
85    pub fn running_tasks(&self) -> Vec<ScheduledTask> {
86        self.scheduler.running_tasks()
87    }
88
89    /// Add a cron job.
90    pub async fn add_cron(&self, job: CronJob) -> anyhow::Result<uuid::Uuid> {
91        self.cron_scheduler.add_job(job).await
92    }
93
94    /// Get a cron job by ID.
95    pub fn get_cron(&self, id: uuid::Uuid) -> Option<CronJob> {
96        self.cron_scheduler.get_job(id)
97    }
98
99    /// Update a cron job.
100    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    /// Remove a cron job by ID.
105    pub async fn remove_cron(&self, id: uuid::Uuid) -> anyhow::Result<()> {
106        self.cron_scheduler.remove_job(id).await
107    }
108
109    /// Trigger a cron job manually.
110    pub fn trigger_cron(&self, id: uuid::Uuid) -> anyhow::Result<CronJob> {
111        self.cron_scheduler.trigger_job(id)
112    }
113
114    /// Mark cron job completed.
115    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    /// List all cron jobs.
122    pub fn list_crons(&self) -> Vec<CronJob> {
123        self.cron_scheduler.list_jobs()
124    }
125
126    /// Get resource snapshot.
127    pub fn resource_snapshot(&self) -> ResourceSnapshot {
128        self.resource_monitor.snapshot()
129    }
130
131    /// Get resource history snapshots.
132    pub fn resource_history(&self, last_n: usize) -> Vec<ResourceSnapshot> {
133        self.resource_monitor.history(last_n)
134    }
135
136    /// Check if system is overloaded.
137    pub fn is_overloaded(&self) -> bool {
138        self.resource_monitor.is_overloaded()
139    }
140
141    /// Subscribe to kernel events.
142    pub fn subscribe(&self) -> tokio::sync::broadcast::Receiver<KernelEvent> {
143        self.event_bus.subscribe()
144    }
145
146    /// Publish a kernel event.
147    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    /// Get config reference.
154    pub fn config(&self) -> &OxiosConfig {
155        &self.config
156    }
157
158    /// Get system uptime.
159    pub fn uptime(&self) -> Duration {
160        self.start_time.elapsed()
161    }
162}