Crates.io | tokio-task-scheduler |
lib.rs | tokio-task-scheduler |
version | 1.0.0 |
source | src |
created_at | 2024-12-01 08:53:55.204529 |
updated_at | 2024-12-01 08:53:55.204529 |
description | A non-blocking task scheduler for Rust with fluent API, supporting interval-based and cron-like scheduling |
homepage | https://github.com/cploutarchou/scheduler |
repository | https://github.com/cploutarchou/scheduler |
max_upload_size | |
id | 1467280 |
size | 72,891 |
A non-blocking, flexible task scheduler for Rust with asynchronous execution.
Add to your Cargo.toml
:
[dependencies]
scheduler = { git = "https://github.com/cploutarchou/scheduler" }
tokio = { version = "1.0", features = ["full"] }
use tokio_task_scheduler::{Scheduler, TaskBuilder};
use tokio;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let scheduler = Scheduler::new();
// Schedule a task to run every second
let task1 = TaskBuilder::new("print_task", || {
println!("Task executed!");
Ok(())
})
.every_seconds(1)
.build();
// Schedule a daily task at a 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 rx = scheduler.start().await;
// Run for a short duration
tokio::time::sleep(std::time::Duration::from_secs(5)).await;
// Stop the scheduler
scheduler.stop().await?;
Ok(())
}
The Scheduler provides robust error handling with detailed error types and recovery strategies.
TaskExecutionFailed
: Occurs when a task fails to executeTaskNotFound
: Indicates a task with a specific ID was not foundInvalidTimeFormat
: Signals an incorrect time formatInvalidSchedule
: Represents an invalid scheduling configurationTaskAlreadyExists
: Prevents duplicate task registrationMaxRetriesExceeded
: Tracks repeated task failuresSchedulingConflict
: Detects scheduling conflictsEach error type comes with a suggested recovery strategy:
Retry
: Automatically retry the taskIgnore
: Skip the error and continueCorrect
: Attempt to fix the errorAbort
: Stop further executionReschedule
: Attempt to schedule the task at a different timeThe Scheduler provides robust persistent task storage using SQLite, enabling you to save, retrieve, and manage tasks across application restarts.
use tokio_task_scheduler::persistence::TaskPersistenceManager;
use tokio_task_scheduler::task::TaskBuilder;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a persistence manager with a database file
let persistence_manager = TaskPersistenceManager::new("tasks.db").await?;
// Create a daily backup task
let backup_task = TaskBuilder::new("backup_task", || {
// Backup logic
Ok(())
})
.every_hours(24)
.build();
// Save the task to persistent storage
persistence_manager.save_task(&backup_task).await?;
// Retrieve all persisted tasks
let tasks = persistence_manager.list_tasks().await?;
println!("Persisted Tasks: {}", tasks.len());
// Retrieve a specific task by ID
let task_details = persistence_manager.get_task(&backup_task.id().to_string()).await?;
// Delete a specific task
persistence_manager.delete_task(&backup_task.id().to_string()).await?;
// Clear all tasks from storage
persistence_manager.clear_tasks().await?;
Ok(())
}
The PersistableTask
struct captures comprehensive task information:
The persistence manager supports concurrent task storage and retrieval, making it suitable for multi-threaded applications.
Robust error handling with detailed PersistenceError
variants:
Contributions are welcome! Please follow these steps:
# Clone the repository
git clone https://github.com/cploutarchou/scheduler.git
# Change to project directory
cd scheduler
# Run tests
cargo test
# Run benchmarks
cargo bench
This project is licensed under the MIT License. See the LICENSE
file for details.
For questions, issues, or suggestions, please open an issue on GitHub.