clementine_core/task/
status_monitor.rs

1use std::sync::Arc;
2use tokio::sync::RwLock;
3use tokio::time::Duration;
4use tonic::async_trait;
5
6use crate::errors::BridgeError;
7
8use super::manager::TaskRegistry;
9use super::{manager::TaskStatus, Task, TaskVariant};
10
11pub const TASK_STATUS_MONITOR_POLL_DELAY: Duration = Duration::from_secs(300);
12
13/// A task that monitors the status of all tasks in the background task manager.
14/// If a task is not running, it will log an error periodically.
15#[derive(Debug)]
16pub struct TaskStatusMonitorTask {
17    task_registry: Arc<RwLock<TaskRegistry>>,
18}
19
20impl TaskStatusMonitorTask {
21    pub fn new(task_registry: Arc<RwLock<TaskRegistry>>) -> Self {
22        Self { task_registry }
23    }
24}
25
26#[async_trait]
27impl Task for TaskStatusMonitorTask {
28    type Output = bool;
29    const VARIANT: TaskVariant = TaskVariant::TaskStatusMonitor;
30
31    async fn run_once(&mut self) -> Result<Self::Output, BridgeError> {
32        let task_registry = self.task_registry.read().await;
33        for (task_variant, (task_status, _, _)) in task_registry.iter() {
34            if let TaskStatus::NotRunning(reason) = task_status {
35                tracing::error!("Task {:?} is not running: {}", task_variant, reason);
36            }
37        }
38        Ok(false)
39    }
40}