gw_bin/triggers/mod.rs
1use crate::context::Context;
2use mockall::automock;
3use std::sync::mpsc::Sender;
4use thiserror::Error;
5
6/// A trigger that runs on an HTTP request.
7pub mod http;
8/// A trigger that runs the checks once and then exits.
9pub mod once;
10/// A trigger that runs the checks periodically.
11pub mod schedule;
12/// A trigger that runs the checks periodically.
13pub mod signal;
14
15/// A custom error for describing the error cases for triggers
16#[derive(Debug, Error)]
17pub enum TriggerError {
18 /// Cannot initialize trigger, because it has a misconfiguration.
19 #[error("not configured correctly: {0}")]
20 Misconfigured(String),
21 /// Cannot send trigger with Sender. This usually because the receiver is dropped.
22 #[error("cannot trigger changes, receiver hang up")]
23 ReceiverHangup(#[from] std::sync::mpsc::SendError<Option<Context>>),
24 /// Running the trigger failed.
25 #[error("{0}")]
26 FailedTrigger(String),
27}
28
29/// A trigger is a long running background process, which initiates the checks.
30///
31/// Triggers may include:
32/// - schedules ([schedule::ScheduleTrigger])
33/// - HTTP servers ([http::HttpTrigger])
34/// - etc.
35#[automock]
36pub trait Trigger: Sync + Send {
37 /// Start the trigger process.
38 fn listen(&self, tx: Sender<Option<Context>>) -> Result<(), TriggerError>;
39}