Skip to content

Commit 79e3e31

Browse files
committed
Activity definition
1 parent 03ea804 commit 79e3e31

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

sdk/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ extern crate tracing;
4747
mod activity_context;
4848
mod app_data;
4949
pub mod interceptors;
50+
pub mod new_activity_defs;
5051
mod payload_converter;
5152
mod workflow_context;
5253
mod workflow_future;

sdk/src/new_activity_defs.rs

Whitespace-only changes.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
use crate::WfContext;
2+
use futures::future::BoxFuture;
3+
4+
// #[activity_definition]
5+
type MyActFn = fn(String) -> String;
6+
7+
// Macro enforces types are serializable.
8+
9+
// The biggest problem with activity definitions is they need to be defined in a crate which doesn't
10+
// depend on the entire SDK, because then the workflow code which uses them wouldn't be able to
11+
// be compiled down to WASM. Of course, the issue is activities _aren't_ compiled to WASM, and need
12+
// access to full native functionality. Thus users need to structure their app a bit oddly. They
13+
// can either define all their workflow code & activity _definitions_ in one crate, and then
14+
// depend on that crate from another crate containing their activity implementations / worker, or
15+
// they could make a crate with *just* activity definitions, which is depended on by the workflow
16+
// implementation crate and the worker crate independently. It all makes perfect sense, but is
17+
// maybe a bit annoying in terms of setup - though not really any worse than TS.
18+
19+
// Macro generates this extension & implementation:
20+
//
21+
// The generated code taking `impl Into<Argtype>` is quite nice for ergonomics inside the workflow,
22+
// but might be impossible in some cases, so probably macro would need a flag to turn it off.
23+
pub trait MyActFnWfCtxExt {
24+
// In reality this returns the `CancellableFuture` type from SDK, would also need to move into
25+
// this crate.
26+
fn my_act_fn(
27+
&self,
28+
input: impl Into<String>,
29+
) -> BoxFuture<'static, Result<String, ActivityFail>>;
30+
}
31+
impl MyActFnWfCtxExt for WfContext {
32+
fn my_act_fn(&self, _: impl Into<String>) -> BoxFuture<'static, Result<String, ActivityFail>> {
33+
// Type name is injected in this implementation, taken from macro
34+
todo!()
35+
}
36+
}
37+
38+
// To implement the activity in their implementation crate, the user would do something like:
39+
// worker.register_activity(MyActFn, |input: String| async move { .... });
40+
41+
// Placeholder. Activity failures as can be seen by the WF code.
42+
#[derive(Debug)]
43+
pub struct ActivityFail {}

workflow-api/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
//! to WASM not work. I've already figured out how to do all that once before with my WASM workflows
33
//! hackathon
44
5+
mod activity_definitions;
6+
7+
use activity_definitions::MyActFnWfCtxExt;
58
use futures::future::BoxFuture;
69
use std::time::Duration;
710
use temporal_sdk_core_protos::{
@@ -136,6 +139,8 @@ mod tests {
136139
async move {
137140
ctx.timer(Duration::from_secs(1)).await;
138141
self.foo = 1;
142+
// See activity definitions file
143+
ctx.my_act_fn("Hi!").await.unwrap();
139144
// The into() is unfortunately unavoidable without making C-A-N and confirm cancel
140145
// be errors instead. Personally, I don't love that and I think it's not idiomatic
141146
// Rust, whereas needing to `into()` something is. Other way would be macros, but

0 commit comments

Comments
 (0)