Skip to main content

cron_task_scheduler/
models.rs

1use async_trait::async_trait;
2use chrono::{DateTime, Utc};
3use std::collections::HashMap;
4use std::fmt::Debug;
5
6#[derive(Debug, Clone)]
7pub struct TaskContext {
8    pub scheduled_time: DateTime<Utc>,
9    pub actual_time: DateTime<Utc>,
10    pub weight: i8, // -20 to +19, default 0. Lower is a higher priority (like nice).
11    #[allow(dead_code)]
12    pub metadata: HashMap<String, String>,
13}
14
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16pub enum TaskType {
17    Async,      // pure async
18    Blocking,   // CPU-heavy or sync task
19}
20
21#[async_trait]
22pub trait ReactiveTask: Send + Sync + Debug {
23    fn id(&self) -> &str;
24    fn task_type(&self) -> TaskType {
25        TaskType::Async
26    }
27    async fn execute(
28        &self,
29        context: TaskContext,
30    ) -> Result<(), Box<dyn std::error::Error + Send + Sync>>;
31}
32
33#[derive(Debug, Clone, Copy, PartialEq, Eq)]
34pub enum ExecutionPolicy {
35    /// Skip execution if the previous task is still running
36    SkipIfRunning,
37    /// Run execution in parallel even if the previous one is still running
38    Parallel,
39    /// Queue the execution to run sequentially after the previous one completes
40    #[allow(dead_code)]
41    Sequential,
42}
43
44#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
45pub enum SchedulingPolicy {
46    FirstInFirstOut,
47    Priority,
48    Fair,
49    Delayed,
50    RateLimited,
51}