Skip to main content

bsv_wallet_toolbox/monitor/
task_trait.rs

1//! WalletMonitorTask trait -- the interface for all monitor background tasks.
2//!
3//! Translated from wallet-toolbox/src/monitor/tasks/WalletMonitorTask.ts.
4//! Each task implements trigger (sync, fast check) and run_task (async work).
5
6use async_trait::async_trait;
7
8use crate::error::WalletError;
9
10/// A monitor task performs some periodic or state-triggered maintenance function
11/// on the data managed by a wallet.
12///
13/// The monitor maintains a collection of tasks. It runs each task's non-async
14/// `trigger` to determine if `run_task` needs to run. Tasks that need to be run
15/// are executed consecutively by awaiting their async `run_task` method.
16///
17/// Tasks may use the monitor_events table to persist their execution history
18/// via the storage object.
19#[async_trait]
20pub trait WalletMonitorTask: Send + Sync {
21    /// Returns the name of this task (used for logging and lookup).
22    fn name(&self) -> &str;
23
24    /// Override to handle async task setup configuration.
25    /// Called before the first call to `trigger`.
26    async fn async_setup(&mut self) -> Result<(), WalletError> {
27        Ok(())
28    }
29
30    /// Return true if `run_task` needs to be called now.
31    /// This is NOT async -- it must be a fast, synchronous check.
32    fn trigger(&mut self, now_msecs: u64) -> bool;
33
34    /// Execute the task's work. Returns a log string describing what was done.
35    async fn run_task(&mut self) -> Result<String, WalletError>;
36}