Skip to main content

stasis/application/runtime/
grapheme_healthcheck_job_handler.rs

1use std::sync::Arc;
2
3use async_trait::async_trait;
4
5use crate::application::runtime::grapheme_job_handler::GraphemeJobHandler;
6use crate::application::runtime::in_memory_runtime::{JobExecutionOutcome, JobHandler};
7use crate::domain::errors::Result;
8use crate::domain::runtime::job::Job;
9use crate::ports::outbound::runtime::workflow_engine::WorkflowEngine;
10
11pub struct GraphemeHealthcheckJobHandler {
12    delegate: GraphemeJobHandler,
13}
14
15impl GraphemeHealthcheckJobHandler {
16    pub fn new(engine: Arc<dyn WorkflowEngine>) -> Self {
17        Self {
18            delegate: GraphemeJobHandler::new(engine),
19        }
20    }
21
22    fn build_inline_source(message: &str) -> String {
23        let cleaned = message
24            .replace('"', "'")
25            .replace(['\n', '\r'], " ");
26
27        format!(
28            "import core from \"grapheme/core\"\n\nquery Healthcheck {{\n  core.echo(message: \"{}\") {{\n    state {{ current }}\n  }}\n}}\n",
29            cleaned
30        )
31    }
32}
33
34#[async_trait]
35impl JobHandler for GraphemeHealthcheckJobHandler {
36    fn job_type(&self) -> &'static str {
37        "workflow.grapheme.healthcheck"
38    }
39
40    async fn execute(&self, job: &Job) -> Result<JobExecutionOutcome> {
41        let message = if job.payload_ref.trim().is_empty() {
42            "stasis grapheme healthcheck"
43        } else {
44            job.payload_ref.as_str()
45        };
46
47        let source = Self::build_inline_source(message);
48        let synthetic_job = Job {
49            payload_ref: format!("grapheme:inline:{}", source),
50            ..job.clone()
51        };
52
53        self.delegate.execute(&synthetic_job).await
54    }
55}