tokio-simple-fixed-scheduler

Crates.iotokio-simple-fixed-scheduler
lib.rstokio-simple-fixed-scheduler
version0.1.0
created_at2025-10-31 11:51:10.885326+00
updated_at2025-10-31 11:51:10.885326+00
descriptionSimple scheduler to emit events at a fixed interval with tokio
homepage
repositoryhttps://github.com/jacobtread/tokio-simple-fixed-scheduler
max_upload_size
id1909945
size27,248
Jacob (jacobtread)

documentation

README

tokio-simple-fixed-scheduler

Simple scheduler structure, accepts an event type along with events and produces a async stream that produces the event type each time the fixed interval occurs.

Uses tokio and chrono to sleep until the fixed interval is reached.

Providing the events as a stream makes managing them easier than other solutions which require calls backs and other complex handling.

use chrono::Utc;
use futures::StreamExt;
use tokio_simple_fixed_scheduler::{SchedulerEventStream, SchedulerQueueEvent};

use crate::database::{
    DbPool,
    secrets::{delete_excess_secret_versions, delete_scheduled_secrets},
};

mod scheduler;

#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone, Copy)]
pub enum BackgroundEvent {
    TestEventHourly,
    TestEventMinutely,
}

#[tokio::main]
async fn main() {
    let events = vec![
        SchedulerQueueEvent {
            event: BackgroundEvent::TestEventHourly,
            interval: 60 * 60, // Run the event every hour
        },
        SchedulerQueueEvent {
            event: BackgroundEvent::TestEventMinutely,
            interval: 60, // Run the event every minute
        },
    ];

    let mut events = SchedulerEventStream::new(events);
    while let Some(event) = events.next().await {
        match event {
            BackgroundEvent::TestEventHourly => {
                // Run the hourly code
            }

            BackgroundEvent::TestEventMinutely => {
                // Run the per minute code
            }
        }
    }
}
Commit count: 0

cargo fmt