Skip to main content

Module scheduler

Module scheduler 

Source
Expand description

In-process scheduled task runner — fire async jobs at fixed intervals. See scheduler::Scheduler. In-process scheduled task runner — fire async jobs at fixed intervals.

§Quick start

use rustango::scheduler::Scheduler;
use std::time::Duration;

let scheduler = Scheduler::new();

scheduler.every("cleanup_expired_sessions", Duration::from_secs(300), || async {
    cleanup().await.ok();
});

scheduler.every("rotate_logs", Duration::from_secs(86_400), || async {
    rotate().await.ok();
});

// Spawn the runner — runs until the returned handle is dropped
let handle = scheduler.start();
// ... app runs ...
handle.shutdown().await;

§Semantics

  • Per-task tick loops: each registered task runs in its own tokio task with a tokio::time::interval.
  • Drift handling: if a job takes longer than the interval, ticks are skipped (default MissedTickBehavior::Skip) — no ticks pile up.
  • First fire: occurs after one full interval, not immediately.
  • Panic isolation: a panicking job aborts only that task; other scheduled jobs keep running. The panic is logged via tracing::error!.
  • Shutdown: Handle::shutdown() aborts every spawned task.

§Production note

For multi-process deployments where the same job must run on exactly one node (not per-replica), pair this with the cache layer for distributed locks, or use an external scheduler (Kubernetes CronJob, GitHub Actions).

Structs§

Handle
Handle to a running scheduler — drop or call shutdown() to stop.
Scheduler
Scheduler configuration — register tasks, then start() to spawn the runner.