cadmus_core/task/
hello_world.rs

1//! Example background task for testing the task infrastructure.
2//!
3//! This task prints "Hello world!" every minute. It is only compiled
4//! when the `test` feature is enabled.
5
6use std::sync::mpsc::Sender;
7use std::time::Duration;
8
9use crate::task::{BackgroundTask, ShutdownSignal, TaskId};
10use crate::view::Event;
11
12const PRINT_INTERVAL: Duration = Duration::from_secs(60);
13
14/// Example task that prints a message periodically.
15///
16/// This serves as a reference implementation for the [`BackgroundTask`] trait
17/// and validates that the task infrastructure works correctly.
18pub struct HelloWorldTask;
19
20impl BackgroundTask for HelloWorldTask {
21    fn id(&self) -> TaskId {
22        TaskId::HelloWorld
23    }
24
25    fn run(&mut self, _hub: &Sender<Event>, shutdown: &ShutdownSignal) {
26        tracing::info!("hello_world task started");
27
28        loop {
29            {
30                #[cfg(feature = "otel")]
31                let _span = tracing::info_span!("hello_world_tick").entered();
32                tracing::info!("Hello world!");
33            }
34
35            if shutdown.wait(PRINT_INTERVAL) {
36                break;
37            }
38        }
39
40        tracing::info!("hello_world task stopped");
41    }
42}