multicast_codemode/
engine.rs1use std::sync::Arc;
2
3use async_trait::async_trait;
4use serde::{Deserialize, Serialize};
5use serde_json::Value;
6
7use crate::{
8 ConnectorDescription, DispatchResponse, DispatchSession, ExecutionCheckpoint, StepResponse,
9};
10
11#[derive(Debug, Clone, Serialize, Deserialize)]
13pub struct ExecuteResult {
14 pub result: Option<Value>,
16 pub error: Option<String>,
18 pub logs: Vec<String>,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
24#[serde(tag = "kind", rename_all = "snake_case")]
25pub enum DispatchRequest {
26 LoadCheckpoint {
28 execution_id: String,
30 },
31 SaveCheckpoint {
33 execution_id: String,
35 checkpoint: ExecutionCheckpoint,
37 },
38 Call {
40 execution_id: String,
42 seq: u64,
44 connector: String,
46 method: String,
48 arguments: Value,
50 },
51 BeginStep {
53 execution_id: String,
55 seq: u64,
57 name: String,
59 },
60 RecordStep {
62 execution_id: String,
64 seq: u64,
66 result: Value,
68 },
69}
70
71#[async_trait(?Send)]
76#[allow(
77 clippy::double_must_use,
78 reason = "async_trait annotates generated futures"
79)]
80pub trait CodeExecutor {
81 async fn execute(
83 &self,
84 code: &str,
85 connectors: &[ConnectorDescription],
86 execution_id: &str,
87 host: Arc<ExecutionHost>,
88 ) -> Result<ExecuteResult, String>;
89}
90
91pub trait Clock: Send + Sync {
93 fn now_ms(&self) -> u64;
95 fn sleep(
97 &self,
98 milliseconds: u64,
99 ) -> std::pin::Pin<Box<dyn std::future::Future<Output = ()> + Send + '_>> {
100 #[cfg(not(target_arch = "wasm32"))]
101 {
102 Box::pin(tokio::time::sleep(std::time::Duration::from_millis(
103 milliseconds,
104 )))
105 }
106 #[cfg(target_arch = "wasm32")]
107 {
108 let _ = milliseconds;
109 Box::pin(std::future::pending())
110 }
111 }
112}
113
114#[derive(Debug, Clone, Copy, Default)]
116pub struct SystemClock;
117
118impl Clock for SystemClock {
119 fn now_ms(&self) -> u64 {
120 std::time::SystemTime::now()
121 .duration_since(std::time::UNIX_EPOCH)
122 .unwrap_or_default()
123 .as_millis() as u64
124 }
125}
126
127pub struct ExecutionHost {
129 session: Arc<DispatchSession>,
130 clock: Arc<dyn Clock>,
131}
132
133impl ExecutionHost {
134 pub async fn checkpoint(&self) -> Result<Option<ExecutionCheckpoint>, String> {
136 self.session.checkpoint().await
137 }
138
139 pub async fn save_checkpoint(&self, checkpoint: &ExecutionCheckpoint) -> Result<(), String> {
141 self.session.save_checkpoint(checkpoint).await
142 }
143 pub(crate) fn new(session: Arc<DispatchSession>, clock: Arc<dyn Clock>) -> Self {
144 Self { session, clock }
145 }
146
147 pub async fn call(
149 &self,
150 seq: u64,
151 connector: &str,
152 method: &str,
153 arguments: Value,
154 ) -> DispatchResponse {
155 self.session
156 .call_at(seq, connector, method, arguments, self.clock.now_ms())
157 .await
158 }
159
160 pub async fn begin_step(&self, seq: u64, name: &str) -> Result<StepResponse, String> {
162 self.session
163 .begin_step_at(seq, name, self.clock.now_ms())
164 .await
165 .map_err(|error| error.to_string())
166 }
167
168 pub async fn record_step(&self, seq: u64, result: Value) -> Result<(), String> {
170 self.session
171 .record_step(seq, result, self.clock.now_ms())
172 .await
173 .map_err(|error| error.to_string())
174 }
175}