| Crates.io | task_scheduler_rs |
| lib.rs | task_scheduler_rs |
| version | 0.4.0 |
| created_at | 2024-12-22 13:10:01.903793+00 |
| updated_at | 2024-12-23 10:12:18.312112+00 |
| description | A Rust-based scheduler with basic functionality, focused on adding tasks and handling actions like execute, pause, and shutdown using channels for task communication. |
| homepage | |
| repository | https://github.com/yangsnapify/task-scheduler-rs |
| max_upload_size | |
| id | 1491934 |
| size | 33,002 |
A flexible, async task scheduler for Rust that supports both one-time and recurring tasks using tokio, with thread-safe implementation.
Arc and Mutex for safe concurrent accessAdd this to your Cargo.toml:
[dependencies]
task-scheduler-rs = "0.4.0"
tokio = { version = "1.0", features = ["full"] }
use std::sync::Arc;
use tokio::sync::Mutex;
use tokio::time::Duration;
use task_scheduler_rs::{Scheduler, Task};
use task_scheduler_rs::scheduler::task::RecurrenceType;
#[tokio::main]
async fn main() {
// Create a thread-safe scheduler
let scheduler = Arc::new(Mutex::new(Scheduler::new()));
// Add tasks with lock
{
let mut scheduler = scheduler.lock().await;
// Add a one-time task
scheduler.add_task(
"one_time_task".to_string(),
|task| println!("Executing task: {}", task.name),
Duration::from_secs(5) // Runs after 5 seconds
);
// Add a recurring task
scheduler.add_recurring_task(
"recurring_task".to_string(),
|task| println!("Running recurring task: {}", task.name),
Duration::from_secs(0), // No initial delay
RecurrenceType::Fixed(Duration::from_secs(2)) // Repeats every 2 seconds
);
}
// Run scheduler in separate task
let scheduler_clone = Arc::clone(&scheduler);
tokio::spawn(async move {
let mut scheduler = scheduler_clone.lock().await;
scheduler.execute().await;
});
}
// Remove a task
let scheduler_clone = Arc::clone(&scheduler);
tokio::spawn(async move {
tokio::time::sleep(Duration::from_secs(5)).await;
let mut scheduler = scheduler_clone.lock().await;
scheduler.remove_task_by_name("one_time_task");
println!("Current tasks: {:?}", scheduler.list_tasks());
});
new(): Create a new scheduler instanceadd_task(name: String, callback: impl FnMut(&Task) + Send + 'static, delay: Duration): Add a one-time taskadd_recurring_task(name: String, callback: impl FnMut(&Task) + Send + 'static, delay: Duration, recurrence: RecurrenceType): Add a recurring taskexecute(): Start executing scheduled tasksremove_task_by_name(name: &str): Remove a task by its namelist_tasks(): Get a list of all task namesstop(): Stop all running taskspub enum RecurrenceType {
None, // One-time task
Fixed(Duration), // Recurring task with fixed interval
}
The scheduler is designed to be thread-safe using Arc<Mutex<Scheduler>>. This allows:
.lock().awaitfull features enabledThis is a learning project as I'm still getting familiar with Rust. The current implementation focuses on basic functionality and might not include advanced features or optimizations. If you're looking for a production-ready task scheduler with more advanced features, please consider using other established crates like:
#[tokio::main]
async fn main() {
let scheduler = Arc::new(Mutex::new(Scheduler::new()));
{
let mut scheduler = scheduler.lock().await; // Lock for adding tasks
scheduler.add_task(
"daily_backup".to_string(),
Box::new(|task: &Task| println!("Backing up... {}", task.name)),
Duration::from_secs(1),
);
scheduler.add_task(
"hourly_check".to_string(),
Box::new(|task: &Task| println!("Checking... {}", task.name)),
Duration::from_secs(2),
);
scheduler.add_recurring_task(
"recurring_task".to_string(),
|task| println!("Running task: {}", task.name),
Duration::from_secs(0), // initial delay
RecurrenceType::Fixed(Duration::from_secs(5)) // repeat every 5 seconds
);
};
let scheduler_clone = Arc::clone(&scheduler);
// Task 1: Execute the scheduler
tokio::spawn(async move {
let mut scheduler = scheduler_clone.lock().await;
scheduler.execute().await;
});
let scheduler_clone = Arc::clone(&scheduler);
// Task 2: Remove 'daily_backup' after 5 seconds
tokio::spawn(async move {
sleep(Duration::from_secs(5)).await; // Wait 5 seconds
let mut scheduler = scheduler_clone.lock().await;
scheduler.remove_task_by_name("daily_backup");
println!("Removed 'daily_backup'");
println!("Current tasks: {:?}", scheduler.list_tasks());
});
// Main thread keeps running
loop {
sleep(Duration::from_secs(1)).await;
println!("Main thread is still running...");
}
}