| Crates.io | cronline |
| lib.rs | cronline |
| version | 0.2.1 |
| created_at | 2025-11-03 13:10:58.986397+00 |
| updated_at | 2025-11-03 13:10:58.986397+00 |
| description | Lightweight Task Scheduling for Rust |
| homepage | |
| repository | https://github.com/TickTockBent/Cronline |
| max_upload_size | |
| id | 1914606 |
| size | 285,018 |
Cronline is a minimalist yet powerful cron-style task scheduler built with Rust. Designed for simplicity, performance, and ease of integration, Cronline enables developers to effortlessly manage scheduled asynchronous tasks within Rust applications.
Cronline addresses the common need among Rust developers for reliable task scheduling without the overhead of external cron utilities or complex frameworks. It's ideal for:
0 0 * * *)Task::with_interval()log, env_logger, or tracing)Add Cronline to your Cargo.toml:
[dependencies]
cronline = "0.2.1"
use cronline::{Scheduler, Task};
use tokio;
#[tokio::main]
async fn main() {
// Create a new scheduler
let scheduler = Scheduler::new();
// Add a task that runs every day at 6am
scheduler.add("0 6 * * *", Task::new(|| async {
println!("Performing daily maintenance task...");
// Task implementation here
Ok(())
})).await.unwrap();
// Start the scheduler (this blocks until the scheduler is stopped)
scheduler.run().await;
}
use cronline::{Scheduler, Task, TaskConfig, SchedulerConfig};
use std::time::Duration;
#[tokio::main]
async fn main() {
// Create a scheduler with custom configuration
let scheduler = Scheduler::with_config(SchedulerConfig {
check_interval: Duration::from_millis(100),
continue_on_error: true,
shutdown_grace_period: Duration::from_secs(10),
});
// Add a task with custom retry configuration
scheduler.add("*/5 * * * *", Task::new(|| async {
// Task that runs every 5 minutes
// ...
Ok(())
})
.with_name("Background Process")
.with_config(TaskConfig {
timeout: Some(Duration::from_secs(60)),
max_retries: 3,
retry_delay: Duration::from_secs(5),
fail_scheduler_on_error: false,
})).await.unwrap();
// Start the scheduler in the background
scheduler.start().await.unwrap();
// Do other work...
// Stop the scheduler when done
scheduler.stop().await.unwrap();
}
// Create a task that runs every 5 minutes
let task = Task::new(|| async {
println!("Running periodic task!");
Ok(())
})
.with_interval(Duration::from_secs(300));
scheduler.add_task(task).await?;
// Create tasks with tags
let backup_task = Task::new(|| async { Ok(()) })
.with_tags(&["backup", "database", "critical"]);
let monitoring_task = Task::new(|| async { Ok(()) })
.with_tag("monitoring");
scheduler.add_task(backup_task)?;
scheduler.add_task(monitoring_task)?;
// Filter tasks by tags
let critical_tasks = scheduler.tasks_with_tag("critical").await;
let backup_or_monitoring = scheduler.tasks_with_any_tag(&["backup", "monitoring"]).await;
let critical_backups = scheduler.tasks_with_all_tags(&["backup", "critical"]).await;
// Subscribe to scheduler events
let mut events = scheduler.event_bus().subscribe();
tokio::spawn(async move {
while let Ok(event) = events.recv().await {
match event {
SchedulerEvent::TaskStarting { task_name, .. } => {
println!("Task starting: {}", task_name);
}
SchedulerEvent::TaskCompleted { task_name, duration_ms, .. } => {
println!("Task completed: {} ({}ms)", task_name, duration_ms);
}
SchedulerEvent::TaskTimeoutWarning { task_name, .. } => {
println!("Task approaching timeout: {}", task_name);
}
_ => {}
}
}
});
// Cancel a running task
if let Some(task) = scheduler.get_task(&task_id).await {
task.cancel().await;
}
// Task names are automatically generated from cron expressions
let task = Task::new(|| async { Ok(()) })
.with_schedule("0 * * * *")?; // Automatically named "Hourly"
let task = Task::new(|| async { Ok(()) })
.with_schedule("*/5 * * * *")?; // Automatically named "Every 5 Minutes"
let task = Task::new(|| async { Ok(()) })
.with_interval(Duration::from_secs(300)); // Automatically named "Every 5 Minutes"
// Get a list of all task IDs
let task_ids = scheduler.task_ids().await;
// Get a reference to a specific task
if let Some(task) = scheduler.get_task(&task_id).await {
// Get task statistics
let stats = task.stats().await;
println!("Task executions: {}", stats.executions);
// Manually execute a task
task.execute().await.unwrap();
// Pause a task
task.pause().await.unwrap();
// Resume a paused task
task.resume().await.unwrap();
// Cancel a running task
task.cancel().await;
}
// Remove a task from the scheduler
scheduler.remove(&task_id).unwrap();
Cronline uses standard cron syntax: * * * * * (minute, hour, day of month, month, day of week)
Examples:
* * * * * - Every minute0 * * * * - Every hour at minute 00 0 * * * - Every day at midnight0 12 * * MON-FRI - Weekdays at noon*/15 * * * * - Every 15 minutesCronline includes comprehensive real-world examples demonstrating production patterns:
See EXAMPLES.md for detailed documentation of all examples.
Run any example with:
cargo run --example web_integration_axum
cargo run --example monitoring_alerting
Task::with_interval(Duration)This project is licensed under the MIT License - see the LICENSE file for details.