rsclaw 0.0.1-alpha.1

rsclaw: High-performance AI agent (BETA). Optimized for M4 Max and 2GB VPS. 100% compatible with openclaw
Documentation
use super::types::CronTask;
use crate::store::Store;
use anyhow::Result;
use std::sync::Arc;

/// Cron task store for persistence.
pub struct CronStore {
    store: Arc<Store>,
}

impl CronStore {
    /// Create a new cron store.
    pub fn new(store: Arc<Store>) -> Self {
        Self { store }
    }

    /// Save a task.
    pub fn save(&self, task: &CronTask) -> Result<()> {
        let serialized = serde_json::to_vec(task)?;
        self.store
            .save_message(&format!("cron:{}", task.id), &serialized)
    }

    /// Load a task by ID.
    pub fn load(&self, id: &str) -> Result<Option<CronTask>> {
        match self.store.get_message(&format!("cron:{}", id))? {
            Some(data) => {
                let task: CronTask = serde_json::from_slice(&data)?;
                Ok(Some(task))
            }
            None => Ok(None),
        }
    }

    /// Delete a task.
    pub fn delete(&self, id: &str) -> Result<bool> {
        self.store.delete_message(&format!("cron:{}", id))
    }

    /// List all task IDs.
    pub fn list_ids(&self) -> Result<Vec<String>> {
        let all_keys = self.store.list_messages()?;
        Ok(all_keys
            .into_iter()
            .filter(|k| k.starts_with("cron:"))
            .map(|k| k.trim_start_matches("cron:").to_string())
            .collect())
    }

    /// List all tasks.
    pub fn list_all(&self) -> Result<Vec<CronTask>> {
        let ids = self.list_ids()?;
        let mut tasks = Vec::new();

        for id in ids {
            if let Some(task) = self.load(&id)? {
                tasks.push(task);
            }
        }

        Ok(tasks)
    }
}