Skip to main content

Module job

Module job 

Source
Expand description

Background jobs: periodic tasks, daily schedules, or both.

Implement ServiceJob for each task and run it through JobRegistry. JobType::Periodic runs on an interval via a Tokio task, JobType::Exact runs once per day at a TimeOfDay via the cron scheduler, and JobType::PeriodicAndExact runs both legs.

Key types: ServiceJob for the task, JobRegistry for ownership and lifecycle, JobType for scheduling behavior, TimeOfDay for daily times.

Use this module for maintenance work such as cleanup or aggregation.

struct Cleanup;

#[async_trait::async_trait]
impl ServiceJob for Cleanup {
    fn name(&self) -> &str { "cleanup" }
    fn job_type(&self) -> JobType { JobType::Periodic }
    fn period(&self) -> Option<Duration> { Some(Duration::from_secs(60)) }
    fn schedule(&self) -> Option<TimeOfDay> { None }
    async fn run(&self) -> anyhow::Result<ServiceResult<serde_json::Value>> {
        Ok(ServiceResult::ok("cleaned", serde_json::json!({})))
    }
}

let mut registry = JobRegistry::new();
registry.add_job(Cleanup);
registry.start().await?;

Structs§

JobRegistry
Owns jobs and drives their schedules.
TimeOfDay
A time of day for JobType::Exact schedules.

Enums§

JobType
Scheduling behavior of a ServiceJob.

Traits§

ServiceJob
A unit of background work managed by JobRegistry.