1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use mockall::automock;
use std::sync::mpsc::Sender;
use thiserror::Error;

/// A trigger that runs on an HTTP request.
pub mod http;
/// A trigger that runs the checks once and then exits.
pub mod once;
/// A trigger that runs the checks periodically.
pub mod schedule;

/// A custom error for describing the error cases for triggers
#[derive(Debug, Error)]
pub enum TriggerError {
    /// Cannot initialize trigger, because it has a misconfiguration.
    #[error("not configured correctly: {0}")]
    Misconfigured(String),
    /// Cannot send trigger with Sender. This usually because the receiver is dropped.
    #[error("cannot trigger changes, receiver hang up")]
    ReceiverHangup(#[from] std::sync::mpsc::SendError<Option<()>>),
    /// Running the trigger failed.
    #[error("{0}")]
    FailedTrigger(String),
}

/// A trigger is a long running background process, which initiates the checks.
///
/// Triggers may include:
///   - schedules ([schedule::ScheduleTrigger])
///   - HTTP servers ([http::HttpTrigger])
///   - etc.
#[automock]
pub trait Trigger: Sync + Send {
    /// Start the trigger process.
    fn listen(&self, tx: Sender<Option<()>>) -> Result<(), TriggerError>;
}