stasis/application/runtime/
memory_aggregate_job_handler.rs1use std::sync::Arc;
2
3use async_trait::async_trait;
4use serde_json::json;
5
6use crate::application::orchestration::runtime_job_payloads::MemoryAggregateJobPayload;
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_models::{MemoryAggregateRequest, MemoryScope};
14use crate::ports::outbound::memory::memory_operations::MemoryOperations;
15
16pub struct MemoryAggregateJobHandler {
17 operations: Arc<dyn MemoryOperations>,
18}
19
20impl MemoryAggregateJobHandler {
21 pub fn new(operations: Arc<dyn MemoryOperations>) -> Self {
22 Self { operations }
23 }
24
25 fn parse_payload(raw: &str) -> std::result::Result<MemoryAggregateJobPayload, String> {
26 serde_json::from_str(raw).map_err(|err| {
27 format!("policy violation: invalid memory-aggregate payload json: {err}")
28 })
29 }
30}
31
32#[async_trait]
33impl JobHandler for MemoryAggregateJobHandler {
34 fn job_type(&self) -> &'static str {
35 "workflow.stasis.memory.aggregate"
36 }
37
38 async fn execute(&self, job: &Job) -> Result<JobExecutionOutcome> {
39 let payload = match Self::parse_payload(&job.payload_ref) {
40 Ok(payload) => payload,
41 Err(message) => return Ok(policy_violation_failure("stasis-memory-aggregate", message)),
42 };
43
44 let request = MemoryAggregateRequest {
45 scope: MemoryScope {
46 session_ids: payload.session_ids,
47 tiers: payload.tiers,
48 from_utc: payload.from_utc,
49 to_utc: payload.to_utc,
50 },
51 max_groups: payload.max_groups.unwrap_or(30),
52 max_nodes: payload.max_nodes.unwrap_or(5000),
53 };
54
55 match self.operations.aggregate(&request).await {
56 Ok(result) => Ok(operation_success(
57 "stasis-memory-aggregate",
58 "memory-aggregate",
59 &job.id,
60 json!({
61 "total_groups": result.total_groups,
62 "scanned_nodes": result.scanned_nodes,
63 }),
64 )),
65 Err(err) => Ok(operation_failure("stasis-memory-aggregate", err.to_string())),
66 }
67 }
68}