stasis/application/runtime/
memory_schema_job_handler.rs1use std::sync::Arc;
2
3use async_trait::async_trait;
4use serde_json::json;
5
6use crate::application::orchestration::runtime_job_payloads::MemorySchemaJobPayload;
7use crate::application::runtime::in_memory_runtime::{JobExecutionOutcome, JobHandler};
8use crate::application::runtime::memory_operation_job_outcome_helpers::{
9 operation_failure, operation_success, policy_violation_failure,
10};
11use crate::domain::errors::Result;
12use crate::domain::runtime::job::Job;
13use crate::ports::outbound::memory::memory_operations::MemoryOperations;
14
15pub struct MemorySchemaJobHandler {
16 operations: Arc<dyn MemoryOperations>,
17}
18
19impl MemorySchemaJobHandler {
20 pub fn new(operations: Arc<dyn MemoryOperations>) -> Self {
21 Self { operations }
22 }
23
24 fn parse_payload(raw: &str) -> std::result::Result<MemorySchemaJobPayload, String> {
25 serde_json::from_str(raw)
26 .map_err(|err| format!("policy violation: invalid memory-schema payload json: {err}"))
27 }
28}
29
30#[async_trait]
31impl JobHandler for MemorySchemaJobHandler {
32 fn job_type(&self) -> &'static str {
33 "workflow.stasis.memory.schema"
34 }
35
36 async fn execute(&self, job: &Job) -> Result<JobExecutionOutcome> {
37 if let Err(message) = Self::parse_payload(&job.payload_ref) {
38 return Ok(policy_violation_failure("stasis-memory-schema", message));
39 }
40
41 match self.operations.schema().await {
42 Ok(result) => Ok(operation_success(
43 "stasis-memory-schema",
44 "memory-schema",
45 &job.id,
46 json!({
47 "schema_version": result.schema_version,
48 "transform_operations": result.transform_operations,
49 "evict_operations": result.evict_operations,
50 }),
51 )),
52 Err(err) => Ok(operation_failure("stasis-memory-schema", err.to_string())),
53 }
54 }
55}