Skip to main content

std_mel/engine/
mod.rs

1use melodium_core::{
2    common::executive::{Output, ResultStatus},
3    *,
4};
5use melodium_macro::{mel_function, mel_model};
6use std::collections::HashMap;
7
8pub mod log;
9
10/// Provides interactions with Mélodium engine.
11///
12/// `ready` source is triggered at startup when engine is ready to process.
13#[mel_model(
14    source ready () () (trigger Block<void>)
15    continuous (continuous)
16)]
17#[derive(Debug)]
18pub struct Engine {
19    model: std::sync::Weak<EngineModel>,
20}
21
22impl Engine {
23    fn new(model: std::sync::Weak<EngineModel>) -> Self {
24        Self { model }
25    }
26
27    async fn continuous(&self) {
28        let model = self.model.upgrade().unwrap();
29
30        model
31            .new_ready(
32                None,
33                &HashMap::new(),
34                Some(Box::new(|mut outputs| {
35                    let trigger = outputs.get("trigger");
36
37                    vec![Box::new(Box::pin(Self::ready(trigger)))]
38                })),
39            )
40            .await;
41    }
42
43    async fn ready(trigger: Box<dyn Output>) -> ResultStatus {
44        let _ = trigger.send_one_as(()).await;
45        trigger.close().await;
46        ResultStatus::Ok
47    }
48}
49
50/// Return the current Mélodium engine version string.
51#[mel_function]
52pub fn version() -> string {
53    melodium_engine::VERSION.to_string()
54}
55
56/// Return the unique identifier of the current execution run.
57#[mel_function]
58pub fn run_id() -> string {
59    melodium_engine::execution_run_id().to_string()
60}
61
62/// Return the group identifier of the current execution run, shared across all workers of the same job.
63#[mel_function]
64pub fn group_id() -> string {
65    melodium_engine::execution_group_id().to_string()
66}