aprun/
lib.rs

1//! Async parallel runner
2//!
3//! ### Reason
4//!
5//! - Run multiple jobs in prallel
6//! - Controlled by server ( example with actix )
7
8use async_trait::async_trait;
9use std::time::SystemTime;
10
11pub mod controller;
12pub mod manager;
13pub mod runner;
14
15// Reexports
16pub use controller::ServiceController;
17pub use manager::ServiceManager;
18pub use runner::{RunnerController, RunnerEvent, ServiceRunner};
19
20#[async_trait]
21pub trait ServiceTask {
22    /// To identify service task
23    fn service_name(&self) -> String;
24
25    async fn run_service_check(&mut self) -> Result<(), String>;
26
27    /// ServiceTasks are triggered often ( 1s interval )
28    /// This method should tell whether run also this ServiceTask in current run.
29    fn should_run(&self) -> bool;
30
31    /// Should run when elapsed time from last run is higher then interval
32    fn should_run_last_interval(
33        &self,
34        last_run: Option<SystemTime>,
35        interval: std::time::Duration,
36    ) -> bool {
37        match last_run {
38            Some(last_run) => {
39                let elapsed = last_run.elapsed();
40                if let Ok(elapsed) = elapsed {
41                    elapsed > interval // true else false
42                } else {
43                    false
44                }
45            }
46            None => true,
47        }
48    }
49}
50
51#[derive(Debug)]
52pub enum Message {
53    RunCheck,
54    Terminate,
55    StartTimer,
56    StopTimer,
57}