Crate cron_tab

Source
Expand description

§CronTab - A Modern Rust Cron Job Scheduler

CronTab is a feature-rich cron job scheduling library for Rust applications that provides both synchronous and asynchronous job execution with timezone support and high performance.

§Features

  • Dual Execution Modes: Support for both synchronous and asynchronous job execution
  • Timezone Support: Full timezone handling with chrono integration
  • High Performance: Efficient job scheduling with minimal overhead
  • Thread Safety: Built with Rust’s safety guarantees in mind
  • Flexible Scheduling: Standard cron expressions with second precision
  • Runtime Control: Add, remove, start, and stop jobs at runtime

§Installation

Add CronTab to your Cargo.toml:

[dependencies]
# For both sync and async support
cron_tab = { version = "0.2", features = ["sync", "async"] }

# For sync only
cron_tab = { version = "0.2", features = ["sync"] }

# For async only
cron_tab = { version = "0.2", features = ["async"] }

§Cron Expression Format

CronTab supports 7-field cron expressions with second precision:

┌───────────── second (0 - 59)
│ ┌─────────── minute (0 - 59)
│ │ ┌───────── hour (0 - 23)
│ │ │ ┌─────── day of month (1 - 31)
│ │ │ │ ┌───── month (1 - 12)
│ │ │ │ │ ┌─── day of week (0 - 6) (Sunday to Saturday)
│ │ │ │ │ │ ┌─ year (1970 - 3000)
│ │ │ │ │ │ │
* * * * * * *

§Synchronous Usage Example

use chrono::{FixedOffset, Local, TimeZone, Utc};
use cron_tab::Cron;

// Create a new cron scheduler with UTC timezone
let mut cron = Cron::new(Utc);

// Add a job that runs every 10 seconds
let job_id = cron.add_fn("*/10 * * * * * *", || {
    println!("Job executed at: {}", Local::now());
})?;

// Start the scheduler in background
cron.start();

// Add another job that runs every minute
cron.add_fn("0 * * * * * *", || {
    println!("Every minute job executed!");
})?;

// Remove the first job after some time
std::thread::sleep(std::time::Duration::from_secs(1));
cron.remove(job_id);

// Stop the scheduler
cron.stop();

§Asynchronous Usage Example

use std::sync::Arc;
use chrono::{FixedOffset, Local, TimeZone, Utc};
use cron_tab::AsyncCron;
use tokio::sync::Mutex;

// Create a new async cron scheduler
let mut cron = AsyncCron::new(Utc);

// Shared state between jobs
let counter = Arc::new(Mutex::new(0));

// Add an async job that increments counter
let counter_clone = counter.clone();
cron.add_fn("* * * * * * *", move || {
    let counter = counter_clone.clone();
    async move {
        let mut count = counter.lock().await;
        *count += 1;
        println!("Counter: {}", *count);
        // Simulate async work
        tokio::time::sleep(tokio::time::Duration::from_millis(100)).await;
    }
}).await?;

// Start the scheduler
cron.start().await;

// Let it run for a few seconds
tokio::time::sleep(tokio::time::Duration::from_secs(3)).await;

// Stop the scheduler
cron.stop().await;

let final_count = *counter.lock().await;
println!("Final count: {}", final_count);

§Timezone Support Example

use chrono::{FixedOffset, TimeZone, Utc};
use cron_tab::Cron;

// Tokyo timezone (UTC+9)
let tokyo_tz = FixedOffset::east_opt(9 * 3600).unwrap();
let mut cron_tokyo = Cron::new(tokyo_tz);

// New York timezone (UTC-5)
let ny_tz = FixedOffset::west_opt(5 * 3600).unwrap();
let mut cron_ny = Cron::new(ny_tz);

// Jobs will run according to their respective timezones
cron_tokyo.add_fn("0 0 9 * * * *", || {
    println!("Good morning from Tokyo!");
})?;

cron_ny.add_fn("0 0 9 * * * *", || {
    println!("Good morning from New York!");
})?;

cron_tokyo.start();
cron_ny.start();

Structs§

AsyncCron
Re-export of the asynchronous cron scheduler.
Cron
Re-export of the synchronous cron scheduler.

Enums§

CronError
Re-export of the cron error type.

Type Aliases§

Result
Convenience type alias for Results returned by this crate.