Skip to main content

melodium_engine/
engine.rs

1use crate::{
2    debug::{DebugLevel, Event},
3    error::{LogicErrors, LogicResult},
4};
5use async_channel::Sender;
6use async_trait::async_trait;
7use melodium_common::{
8    descriptor::{Collection, Identifier},
9    executive::{
10        DirectCreationCallback, Level as LogLevel, Log, Secret, SecretError, SecretSource,
11        SecretsAccess, TrackId, Value,
12    },
13};
14use std::{collections::HashMap, sync::Arc};
15
16#[async_trait]
17pub trait Engine: Send + Sync {
18    fn collection(&self) -> Arc<Collection>;
19    /// Gives a secret source for the `scheme` of locators, such as `api` for `api:<name>`.
20    ///
21    /// Only possible before genesis, that registers it before the sources of models: as for
22    /// `env` and `file`, a model registering the same scheme makes the launch fail.
23    fn add_secret_source(
24        &self,
25        scheme: &str,
26        source: Arc<dyn SecretSource>,
27    ) -> Result<(), SecretError>;
28    fn genesis(&self, entry: &Identifier, params: HashMap<String, Value>) -> LogicResult<()>;
29    fn errors(&self) -> LogicErrors;
30    fn set_auto_end(&self, auto_end: bool);
31    fn auto_end(&self) -> bool;
32    fn log_level(&self) -> LogLevel;
33    fn add_logs_listener(&self, sender: Sender<Log>);
34    fn debug_level(&self) -> DebugLevel;
35    fn add_debug_listener(&self, sender: Sender<Event>);
36    async fn live(&self);
37    async fn instanciate(&self, callback: Option<DirectCreationCallback>) -> LogicResult<()>;
38    async fn end(&self);
39    /// Stops every running track and continuous task, after ending as `end` does: their
40    /// futures are dropped at their next await point, and `live` then ends.
41    async fn stop(&self);
42    /// Resolves the secrets given for each parameter and drops their values right away,
43    /// to check that they can be resolved.
44    async fn check_secrets(&self, secrets: Vec<(String, Secret)>) -> LogicResult<()>;
45    /// Logs a message from the engine itself.
46    async fn log(&self, level: LogLevel, label: String, message: String);
47    /// Gives an access to secrets on behalf of `element`, for code driving the engine
48    /// from outside, such as distribution.
49    fn secrets_access(
50        &self,
51        element: Identifier,
52        label: Option<String>,
53        track_id: Option<TrackId>,
54    ) -> SecretsAccess;
55}