temporalio-sdk 0.6.0

Temporal Rust SDK
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
use std::{fs, io, path::PathBuf, rc::Rc, sync::Arc};

use anyhow::{Context, bail};
use prost::Message;
use temporalio_common::protos::{
    coresdk::workflow_commands::WorkflowCommand, temporal::api::failure::v1::Failure,
};
use temporalio_workflow::{
    PatchActivationCaller,
    runtime::{
        guest::WorkflowInstance,
        host::WorkflowHost,
        types::{
            ActivationJobResult, ActivationResult, MainRoutineCompletion, QueryResponse,
            RoutineCompletion, RoutinePendingState, RoutinePollResult, StartedRoutine, TaskFailure,
            TerminalOutcome, UpdateRoutineCompletion, UpdateRoutineKind, WorkflowActivation,
            WorkflowDefinitionDescriptor, WorkflowFailure,
        },
    },
};
use wasmtime::{
    Config, Engine, Store,
    component::{Component, HasSelf, Linker, ResourceAny},
};

use crate::workflow_registry::{
    WorkflowDefinitions, WorkflowExecutionFactory, WorkflowExecutionInput,
};

wasmtime::component::bindgen!({
    path: "../workflow/wit",
    world: "workflow-module",
});

use self::{
    exports::temporal::workflow_runtime::workflow_guest as wit_guest,
    temporal::workflow_runtime::{types as wit_types, workflow_host as wit_host},
};

/// A prebuilt WebAssembly component that exports one or more Temporal workflows.
#[derive(Clone, Debug)]
pub struct WasmWorkflowComponent {
    component_id: String,
    source: WasmWorkflowComponentSource,
}

#[derive(Clone, Debug)]
enum WasmWorkflowComponentSource {
    File(PathBuf),
    Bytes(Arc<[u8]>),
}

impl WasmWorkflowComponent {
    /// Register a workflow component from a component file on disk.
    pub fn from_file(
        component_id: impl Into<String>,
        path: impl Into<PathBuf>,
    ) -> io::Result<Self> {
        let path = path.into();
        if !path.try_exists()? {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                format!(
                    "WASM workflow component file does not exist: {}",
                    path.display()
                ),
            ));
        }
        Ok(Self {
            component_id: component_id.into(),
            source: WasmWorkflowComponentSource::File(path),
        })
    }

    /// Register a workflow component from in-memory bytes.
    pub fn from_bytes(
        component_id: impl Into<String>,
        bytes: impl Into<Arc<[u8]>>,
    ) -> io::Result<Self> {
        let bytes = bytes.into();
        if bytes.is_empty() {
            return Err(io::Error::new(
                io::ErrorKind::InvalidInput,
                "WASM workflow component bytes must not be empty",
            ));
        }
        Ok(Self {
            component_id: component_id.into(),
            source: WasmWorkflowComponentSource::Bytes(bytes),
        })
    }
}

impl WorkflowDefinitions {
    pub(crate) fn register_wasm_workflows(
        &mut self,
        components: Vec<WasmWorkflowComponent>,
        has_native_workflow_interceptors: bool,
    ) -> Result<(), anyhow::Error> {
        if !components.is_empty() && has_native_workflow_interceptors {
            tracing::warn!(
                "Native workflow interceptors will not be applied to WASM workflow components; \
                 define interceptors in the WASM bundle instead"
            );
        }
        for component in components {
            let module = Arc::new(CompiledWasmWorkflowModule::new(component)?);
            for definition in &module.definitions {
                let module = module.clone();
                let factory: WorkflowExecutionFactory =
                    Arc::new(move |input| module.instantiate(input));
                self.insert_workflow(definition.clone(), factory)?;
            }
        }
        Ok(())
    }
}

struct CompiledWasmWorkflowModule {
    engine: Engine,
    component: Component,
    definitions: Vec<WorkflowDefinitionDescriptor>,
}

impl CompiledWasmWorkflowModule {
    fn new(component: WasmWorkflowComponent) -> Result<Self, anyhow::Error> {
        let mut config = Config::new();
        config.wasm_component_model(true);
        let engine = Engine::new(&config)?;
        let bytes: Arc<[u8]> = match &component.source {
            WasmWorkflowComponentSource::File(path) => fs::read(path)
                .with_context(|| format!("Failed reading WASM component {}", path.display()))?
                .into(),
            WasmWorkflowComponentSource::Bytes(bytes) => bytes.clone(),
        };
        let component = Component::new(&engine, bytes.as_ref()).map_err(|err| {
            anyhow::Error::msg(format!(
                "Failed to compile WASM component {}: {err}",
                component.component_id
            ))
        })?;
        let definitions = Self::read_definitions(&engine, &component)?;
        if definitions.is_empty() {
            bail!("WASM component exports no workflows");
        }
        Ok(Self {
            engine,
            component,
            definitions,
        })
    }

    fn read_definitions(
        engine: &Engine,
        component: &Component,
    ) -> Result<Vec<WorkflowDefinitionDescriptor>, anyhow::Error> {
        let mut linker = Linker::new(engine);
        WorkflowModule::add_to_linker::<_, HasSelf<_>>(&mut linker, |data| data)?;
        let mut store = Store::new(
            engine,
            WasmWorkflowHostState::new(Rc::new(NoopWorkflowHost), None),
        );
        let module = WorkflowModule::instantiate(&mut store, component, &linker)?;
        module
            .temporal_workflow_runtime_workflow_guest()
            .call_list_workflows(&mut store)
            .map(|defs| {
                defs.into_iter()
                    .map(|def| WorkflowDefinitionDescriptor {
                        workflow_type: def.workflow_type,
                        has_init: def.has_init,
                        init_takes_input: def.init_takes_input,
                        signals: def.signals,
                        queries: def.queries,
                        updates: def
                            .updates
                            .into_iter()
                            .map(|u| {
                                temporalio_workflow::runtime::types::UpdateDefinitionDescriptor {
                                    name: u.name,
                                    has_validator: u.has_validator,
                                }
                            })
                            .collect(),
                    })
                    .collect()
            })
            .map_err(|err| {
                anyhow::Error::msg(format!(
                    "Failed to list workflows exported by WASM component: {err}"
                ))
            })
    }

    fn instantiate(
        &self,
        input: WorkflowExecutionInput,
    ) -> Result<Box<dyn WorkflowInstance>, anyhow::Error> {
        let mut linker = Linker::new(&self.engine);
        WorkflowModule::add_to_linker::<_, HasSelf<_>>(&mut linker, |data| data)?;
        let WorkflowExecutionInput {
            namespace,
            task_queue,
            run_id,
            init_workflow_job,
            data_converter,
            host,
            patch_activation_callback,
            ..
        } = input;
        let workflow_init = wit_types::WorkflowInit {
            namespace: namespace.clone(),
            task_queue: task_queue.clone(),
            run_id: run_id.clone(),
            initialize_workflow: init_workflow_job.encode_to_vec(),
        };
        let patch_activation = patch_activation_callback.map(|callback| {
            PatchActivationCaller::new(
                callback,
                namespace,
                task_queue,
                run_id,
                init_workflow_job,
                data_converter.payload_converter().clone(),
            )
        });
        let mut store = Store::new(
            &self.engine,
            WasmWorkflowHostState::new(host, patch_activation),
        );
        let module = WorkflowModule::instantiate(&mut store, &self.component, &linker)?;
        let guest = module.temporal_workflow_runtime_workflow_guest();
        let workflow_instance = guest
            .call_instantiate_workflow(&mut store, &workflow_init)
            .map_err(|err| {
                anyhow::Error::msg(format!("Failed to instantiate WASM workflow: {err}"))
            })?
            .map_err(convert_failure)
            .map_err(|failure| {
                anyhow::Error::msg(format!(
                    "WASM workflow initialization failed: {}",
                    failure.message
                ))
            })?;

        Ok(Box::new(WasmWorkflowInstance {
            store,
            guest: guest.clone(),
            workflow_instance,
        }))
    }
}

struct WasmWorkflowInstance {
    store: Store<WasmWorkflowHostState>,
    guest: wit_guest::Guest,
    workflow_instance: ResourceAny,
}

impl WorkflowInstance for WasmWorkflowInstance {
    fn activate(
        &mut self,
        activation: WorkflowActivation,
        _waker: &std::task::Waker,
    ) -> Result<ActivationResult, WorkflowFailure> {
        let result = self.guest.workflow_instance().call_activate(
            &mut self.store,
            self.workflow_instance,
            &activation.encode_to_vec(),
        );
        trap_to_failure(result, |result| ActivationResult {
            job_results: result
                .job_results
                .into_iter()
                .map(|job_result| match job_result {
                    wit_types::ActivationJobResult::None => ActivationJobResult::None,
                    wit_types::ActivationJobResult::StartedRoutine(routine) => {
                        ActivationJobResult::StartedRoutine(StartedRoutine {
                            routine_id: routine.routine_id,
                            kind: match routine.kind {
                                wit_types::RoutineKind::Main => {
                                    temporalio_workflow::runtime::types::RoutineKind::Main
                                }
                                wit_types::RoutineKind::Signal(name) => {
                                    temporalio_workflow::runtime::types::RoutineKind::Signal(name)
                                }
                                wit_types::RoutineKind::Update(update) => {
                                    temporalio_workflow::runtime::types::RoutineKind::Update(
                                        UpdateRoutineKind {
                                            name: update.name,
                                            update_id: update.update_id,
                                            protocol_instance_id: update.protocol_instance_id,
                                        },
                                    )
                                }
                            },
                        })
                    }
                    wit_types::ActivationJobResult::QueryResponse(response) => {
                        ActivationJobResult::QueryResponse(Box::new(QueryResponse {
                            result: response
                                .response
                                .map(decode_proto)
                                .map_err(|failure| *convert_failure(failure)),
                        }))
                    }
                    wit_types::ActivationJobResult::UpdateRejected(failure) => {
                        ActivationJobResult::UpdateRejected(convert_failure(failure))
                    }
                })
                .collect(),
        })
    }

    fn poll_routine(
        &mut self,
        routine_id: u64,
        _waker: &std::task::Waker,
    ) -> Result<RoutinePollResult, WorkflowFailure> {
        let result = self.guest.workflow_instance().call_poll_routine(
            &mut self.store,
            self.workflow_instance,
            routine_id,
        );
        trap_to_failure(result, |result| RoutinePollResult {
            completion: result.completion.map(|completion| match completion {
                wit_types::RoutineCompletion::Main(completion) => {
                    RoutineCompletion::Main(match completion {
                        wit_types::MainRoutineCompletion::Blocked => MainRoutineCompletion::Blocked,
                        wit_types::MainRoutineCompletion::TaskFailed(task_failure) => {
                            MainRoutineCompletion::TaskFailed(TaskFailure {
                                failure: convert_failure(task_failure.failure),
                                force_cause: task_failure.force_cause,
                            })
                        }
                        wit_types::MainRoutineCompletion::Terminal(outcome) => {
                            MainRoutineCompletion::Terminal(Box::new(match outcome {
                                wit_types::TerminalOutcome::Completed(payload) => {
                                    TerminalOutcome::Completed(decode_proto(payload))
                                }
                                wit_types::TerminalOutcome::Failed(failure) => {
                                    TerminalOutcome::Failed(convert_failure(failure))
                                }
                                wit_types::TerminalOutcome::Cancelled => TerminalOutcome::Cancelled,
                                wit_types::TerminalOutcome::ContinueAsNew(req) => {
                                    TerminalOutcome::ContinueAsNew(Box::new(decode_proto(req)))
                                }
                            }))
                        }
                    })
                }
                wit_types::RoutineCompletion::Signal(result) => {
                    RoutineCompletion::Signal(match result {
                        wit_types::SignalRoutineCompletion::Succeeded => Ok(()),
                        wit_types::SignalRoutineCompletion::Failed(failure) => {
                            Err(convert_failure(failure))
                        }
                    })
                }
                wit_types::RoutineCompletion::Update(completion) => {
                    RoutineCompletion::Update(match completion {
                        wit_types::UpdateRoutineCompletion::Completed(success) => {
                            UpdateRoutineCompletion::Completed {
                                protocol_instance_id: success.protocol_instance_id,
                                result: decode_proto(success.value),
                            }
                        }
                        wit_types::UpdateRoutineCompletion::Rejected(rejection) => {
                            UpdateRoutineCompletion::Rejected {
                                protocol_instance_id: rejection.protocol_instance_id,
                                failure: convert_failure(rejection.failure),
                            }
                        }
                    })
                }
            }),
            made_progress: result.made_progress,
            pending_state: result.pending_state.map(|state| match state {
                wit_types::RoutinePendingState::Handler => RoutinePendingState::Handler,
                wit_types::RoutinePendingState::Interceptor => RoutinePendingState::Interceptor,
                wit_types::RoutinePendingState::InterceptorWithActivation => {
                    RoutinePendingState::InterceptorWithActivation
                }
            }),
        })
    }
}

struct WasmWorkflowHostState {
    host: Rc<dyn WorkflowHost>,
    patch_activation: Option<PatchActivationCaller>,
}

impl WasmWorkflowHostState {
    fn new(host: Rc<dyn WorkflowHost>, patch_activation: Option<PatchActivationCaller>) -> Self {
        Self {
            host,
            patch_activation,
        }
    }
}

impl wit_types::Host for WasmWorkflowHostState {}

impl wit_host::Host for WasmWorkflowHostState {
    fn set_current_details(&mut self, details: String) {
        self.host.set_current_details(details);
    }

    fn push_command(&mut self, command: wit_types::WorkflowCommand) {
        self.host.push_command(decode_proto(command));
    }

    fn patch_activation(&mut self, patch_id: String) -> bool {
        let Some(patch_activation) = &self.patch_activation else {
            return true;
        };
        patch_activation.call(patch_id)
    }
}

struct NoopWorkflowHost;

impl WorkflowHost for NoopWorkflowHost {
    fn set_current_details(&self, _details: String) {}
    fn push_command(&self, _command: WorkflowCommand) {}
}

fn trap_to_failure<T, U>(
    result: Result<Result<T, wit_types::Failure>, wasmtime::Error>,
    convert: impl FnOnce(T) -> U,
) -> Result<U, WorkflowFailure> {
    result
        .map_err(|trap| {
            Box::new(Failure {
                message: format!("WASM workflow trapped: {trap}"),
                ..Default::default()
            })
        })?
        .map(convert)
        .map_err(convert_failure)
}

fn convert_failure(failure: wit_types::Failure) -> WorkflowFailure {
    Box::new(decode_proto(failure))
}

fn decode_proto<M: Message + prost::Name + Default>(bytes: Vec<u8>) -> M {
    M::decode(bytes.as_slice()).unwrap_or_else(|err| {
        let n = M::NAME;
        panic!("failed to decode {n} from WASM boundary bytes: {err}")
    })
}