Skip to main content

clawdesk-cron

Cron scheduling system for periodic task execution. Supports cron expression parsing, task management, heartbeat monitoring, and manual triggering via the admin API.

Dependencies

Internal: clawdesk-types

External: tokio, chrono, tracing, serde, thiserror

Modules

ModuleDescription
executorTask execution engine with error handling and retries
heartbeatHealth monitoring for scheduled tasks
parserCron expression parser (standard 5-field + extensions)
managerCronManager — high-level task management

Key Types

/// Cron task definition
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CronTask {
pub id: String,
pub name: String,
pub schedule: CronSchedule,
pub action: CronAction,
pub enabled: bool,
pub last_run: Option<chrono::DateTime<chrono::Utc>>,
pub next_run: Option<chrono::DateTime<chrono::Utc>>,
pub run_count: u64,
}

/// Parsed cron schedule
#[derive(Debug, Clone)]
pub struct CronSchedule {
pub expression: String,
pub minutes: Vec<u8>,
pub hours: Vec<u8>,
pub days_of_month: Vec<u8>,
pub months: Vec<u8>,
pub days_of_week: Vec<u8>,
}

/// Action to perform when task triggers
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum CronAction {
Webhook { url: String, method: String, body: Option<serde_json::Value> },
SessionCleanup { max_age_hours: u64 },
MetricsExport { endpoint: String },
Custom(String),
}

/// Manages scheduled tasks
pub struct CronManager {
tasks: Arc<RwLock<Vec<CronTask>>>,
executor: CronExecutor,
}

impl CronManager {
pub async fn add_task(&self, task: CronTask) -> Result<(), CronError> { /* ... */ }
pub async fn remove_task(&self, id: &str) -> Result<(), CronError> { /* ... */ }
pub async fn trigger(&self, id: &str) -> Result<(), CronError> { /* ... */ }
pub async fn list_tasks(&self) -> Vec<CronTask> { /* ... */ }
}

Cron Expressions

Standard 5-field format: minute hour day_of_month month day_of_week

ExpressionDescription
*/5 * * * *Every 5 minutes
0 */6 * * *Every 6 hours
0 9 * * 1-59 AM weekdays
0 0 1 * *Midnight, first of month

Example Usage

use clawdesk_cron::{CronManager, CronTask, CronAction, CronSchedule};

let manager = CronManager::new();

// Add a session cleanup task
let task = CronTask {
id: "cleanup".into(),
name: "Session Cleanup".into(),
schedule: CronSchedule::parse("0 */6 * * *")?,
action: CronAction::SessionCleanup { max_age_hours: 48 },
enabled: true,
..Default::default()
};

manager.add_task(task).await?;

// Manually trigger
manager.trigger("cleanup").await?;
info

Tasks log execution results to the audit trail when security.enable_audit is enabled.