Beatwise
Beatwise is a small Rust library for watching long-running work.
A task calls beat() whenever it makes progress. Beatwise tracks that rhythm and
reports whether the task is Starting, Learning, Healthy, Late, or Stopped.
It works well for background workers, sync loops, schedulers, queue consumers, device pollers, backups, peer maintenance, and other jobs that should keep moving.
Beatwise can watch a known interval, learn a task's normal pace, or learn a repeating cycle. It uses only the standard library, starts no threads, and does not require an async runtime.
What it provides
- Fixed deadlines for predictable work.
- Learned timing for jobs whose pace changes.
- A robust model for occasional outliers.
- Repeating-pattern detection for multi-phase work.
- Independent transition cursors and aggregate health reports.
- Bounded memory with no event queue or unbounded history.
All timing stays inside the caller's process. Fixed heartbeats use atomics and a monotonic clock read. Learned tasks add one small mutex. Stopped tasks remain visible until explicitly purged.
Learn a task's normal pace
EWMA is the default learned model. It uses constant memory and adapts gradually as the task changes:
use Duration;
use ;
let monitor = new;
let timing = default
.with_minimum_samples
.with_minimum_grace
.with_sensitivity
.with_adaptation;
let heartbeat = monitor.register?;
heartbeat.beat?;
for status in monitor.snapshot
# Ok::
For jobs with occasional long pauses or extreme outliers, use the bounded robust window:
use Duration;
use ;
let monitor = new;
let timing = default
.with_minimum_samples
.with_minimum_grace
.with_adaptation
.with_model;
let heartbeat = monitor.register?;
let task_id = heartbeat.id;
monitor.retrain?;
# Ok::
Robust windows use odd capacities from 5 through 31. Monitor::retrain clears the
learned baseline without replacing the task ID or heartbeat handle.
Learn repeating work
Some healthy jobs alternate between short and long phases. The repeating-pattern model learns that cycle instead of forcing it into one average:
use Duration;
use ;
let monitor = new;
let pattern = new
.with_tolerance_percent
.with_minimum_contrast_percent;
let timing = default
.with_minimum_samples
.with_startup_grace
.with_minimum_grace
.with_sensitivity
.with_adaptation
.with_model;
let heartbeat = monitor.register?;
heartbeat.beat?;
# Ok::
Patterns can have two through eight phases and require three through eight cycles. Beatwise learns from at most 64 intervals. An outlier still counts as a heartbeat and advances the phase, but it does not replace the trusted baseline.
Watch state changes
A transition cursor reports meaningful state changes without callbacks or a shared event queue:
use Duration;
use ;
let monitor = new;
let heartbeat = monitor.register?;
let mut transitions = monitor.transition_cursor;
for transition in transitions.poll
heartbeat.beat?;
for transition in transitions.poll
# Ok::
Each cursor is independent. poll() returns only state changes, ordered by task
ID. Beatwise does not run callbacks or own an executor.
Build health endpoints
Use a summary for a lightweight health check or a full report when you need task details:
use Duration;
use ;
let monitor = new;
let heartbeat = monitor.register?;
let readiness = monitor.summary;
assert_eq!;
assert_eq!;
heartbeat.beat?;
let liveness = monitor.report;
println!;
for task in liveness.tasks.iter
# Ok::
readiness() treats starting and learning tasks as degraded. liveness() allows
those states, fails late tasks, and ignores stopped records. strict() accepts
only healthy tasks. Custom policies can change how any state affects the verdict.
Version note
Beatwise 0.2.0 replaces the old Thumper 0.1.x DJ, deck, and output API. The old implementation remains available in Git history.
Validation
From the repository root:
RUSTDOCFLAGS="-D warnings"