Crate tokio_task_scheduler

Source
Expand description

A non-blocking task scheduler for Rust

This library provides a flexible, non-blocking task scheduler that can be used to schedule and execute tasks at specified intervals or times. It supports:

  • Scheduling tasks at fixed intervals (seconds, minutes, hours, days)
  • Daily tasks at specific times
  • Task persistence across application restarts
  • Error handling and retry mechanisms
  • Async/await support with Tokio

§Features

  • Non-blocking execution: Tasks run asynchronously without blocking the main thread
  • Flexible scheduling: Support for various scheduling patterns
  • Persistence: Optional task persistence using SQLite
  • Error handling: Comprehensive error types and recovery strategies
  • Builder pattern: Fluent interface for creating tasks

§Example

use tokio_task_scheduler::{Scheduler, TaskBuilder, SchedulerError};
use std::time::Duration;
use tokio;
 
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    let scheduler = Scheduler::new();
     
    // Schedule a task to run every minute
    let task1 = TaskBuilder::new("print_task", || {
        println!("Task executed!");
        Ok(())
    })
    .every_seconds(1)
    .build();
     
    // Schedule a task to run at specific time
    let task2 = TaskBuilder::new("daily_task", || {
        println!("Daily task at 10:30");
        Ok(())
    })
    .daily()
    .at("10:30")
    .unwrap()
    .build();
     
    // Add tasks to the scheduler
    scheduler.add_task(task1).await?;
    scheduler.add_task(task2).await?;
     
    // Start the scheduler
    let mut rx = scheduler.start().await;
     
    // Wait for a short duration
    tokio::time::sleep(Duration::from_secs(2)).await;
     
    // Stop the scheduler
    scheduler.stop().await?;
     
    Ok(())
}

§Modules

  • error: Error types and handling
  • task: Task definition and execution
  • scheduler: Core scheduling functionality
  • persistence: Task persistence and storage

Re-exports§

pub use error::SchedulerError;
pub use scheduler::Scheduler;
pub use scheduler::Job;
pub use task::Task;
pub use task::TaskStatus;

Modules§

error
Error types for the scheduler library
persistence
Task persistence and storage
scheduler
task
Task management and execution

Structs§

TaskBuilder
A builder for creating tasks with a fluent interface