temporalio-sdk-core 0.7.0

Library for building new Temporal SDKs
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
use crate::common::{get_integ_server_options, get_integ_telem_options, integ_namespace};
use futures_util::future::BoxFuture;
use std::{
    sync::{
        Arc,
        atomic::{
            AtomicU8, AtomicUsize,
            Ordering::{self, Relaxed},
        },
    },
    time::Duration,
};
use temporalio_client::{
    Client, ClientInterceptor, ClientOptions, ClientPlugin, ConnectionOptions, NamespacedClient,
    Next, PluginError, StartWorkflowInput, StartWorkflowOutput, WorkflowStartOptions,
    errors::WorkflowStartError,
};
use temporalio_common::{
    data_converters::{
        DataConverter, DefaultFailureConverter, PayloadCodec, PayloadConversionError,
        PayloadConverter, SerializationContextData,
    },
    protos::{
        coresdk::workflow_activation::WorkflowActivation, temporal::api::common::v1::Payload,
    },
};
use temporalio_macros::{activities, workflow, workflow_methods};
use temporalio_sdk::{
    ActivityOptions, ClientAndWorkerPlugin, Runtime, SimplePlugin, Worker, WorkerOptions,
    WorkerPlugin, WorkflowContext, WorkflowDefinitions, WorkflowResult,
    activities::{ActivityContext, ActivityDefinitions, ActivityError},
    interceptors::WorkerInterceptor,
    runtime::RuntimeOptions,
};
use url::Url;
use uuid::Uuid;

fn new_sdk_runtime() -> Runtime {
    Runtime::new_assume_tokio(
        RuntimeOptions::builder()
            .telemetry_options(get_integ_telem_options())
            .build()
            .unwrap(),
    )
    .unwrap()
}

#[derive(Clone)]
struct IntegrationPlugin {
    connection_calls: Arc<AtomicU8>,
    client_calls: Arc<AtomicU8>,
    worker_calls: Arc<AtomicU8>,
    target: Url,
}

impl ClientPlugin for IntegrationPlugin {
    fn name(&self) -> &str {
        "integration-plugin"
    }

    fn configure_connection_options(
        &self,
        options: &mut ConnectionOptions,
    ) -> Result<(), PluginError> {
        self.connection_calls.fetch_add(1, Relaxed);
        options.target = self.target.clone();
        options.identity = "integration-plugin-client".to_owned();
        Ok(())
    }

    fn configure_client_options(&self, options: &mut ClientOptions) -> Result<(), PluginError> {
        self.client_calls.fetch_add(1, Relaxed);
        options.namespace = integ_namespace();
        Ok(())
    }
}

impl WorkerPlugin for IntegrationPlugin {
    fn name(&self) -> &str {
        "integration-plugin"
    }

    fn configure_worker_options(&self, options: &mut WorkerOptions) -> Result<(), PluginError> {
        self.worker_calls.fetch_add(1, Relaxed);
        options.max_cached_workflows = 0;
        Ok(())
    }
}

#[tokio::test]
async fn plugins_configure_client_and_worker() {
    let runtime = new_sdk_runtime();
    let connection_calls = Arc::new(AtomicU8::new(0));
    let client_calls = Arc::new(AtomicU8::new(0));
    let worker_calls = Arc::new(AtomicU8::new(0));
    let server_options = get_integ_server_options();
    let plugin = ClientAndWorkerPlugin::new(IntegrationPlugin {
        connection_calls: connection_calls.clone(),
        client_calls: client_calls.clone(),
        worker_calls: worker_calls.clone(),
        target: server_options.target,
    });
    let client_options = ClientOptions::new("plugin-replaces-this-namespace")
        .plugin(plugin)
        .build();
    let connection_options =
        ConnectionOptions::new(Url::parse("http://127.0.0.1:1").unwrap()).build();
    let client = Client::connect(connection_options, client_options)
        .await
        .unwrap();
    assert_eq!(client.connection().identity(), "integration-plugin-client");
    assert_eq!(client.namespace(), integ_namespace());
    let worker_options = WorkerOptions::new(format!("plugins-{}", Uuid::new_v4()))
        .register_workflow::<SimplePluginWorkflow>()
        .unwrap()
        .build();
    let worker = Worker::new(&runtime, client, worker_options).unwrap();

    assert_eq!(connection_calls.load(Relaxed), 1);
    assert_eq!(client_calls.load(Relaxed), 1);
    assert_eq!(worker_calls.load(Relaxed), 1);
    assert_eq!(worker.core_worker().get_config().max_cached_workflows, 0);
    assert_eq!(worker.core_worker().get_config().plugins.len(), 1);
}

struct CountingPayloadCodec {
    encode_calls: Arc<AtomicUsize>,
    decode_calls: Arc<AtomicUsize>,
}

impl PayloadCodec for CountingPayloadCodec {
    fn encode(
        &self,
        _context: &SerializationContextData,
        payloads: Vec<Payload>,
    ) -> BoxFuture<'static, Result<Vec<Payload>, PayloadConversionError>> {
        self.encode_calls.fetch_add(1, Ordering::Relaxed);
        Box::pin(async move { Ok(payloads) })
    }

    fn decode(
        &self,
        _context: &SerializationContextData,
        payloads: Vec<Payload>,
    ) -> BoxFuture<'static, Result<Vec<Payload>, PayloadConversionError>> {
        self.decode_calls.fetch_add(1, Ordering::Relaxed);
        Box::pin(async move { Ok(payloads) })
    }
}

struct CountingClientInterceptor {
    calls: Arc<AtomicUsize>,
}

impl ClientInterceptor for CountingClientInterceptor {
    fn start_workflow<'a>(
        &'a self,
        input: StartWorkflowInput,
        next: Next<
            'a,
            StartWorkflowInput,
            BoxFuture<'a, Result<StartWorkflowOutput, WorkflowStartError>>,
        >,
    ) -> BoxFuture<'a, Result<StartWorkflowOutput, WorkflowStartError>> {
        self.calls.fetch_add(1, Ordering::Relaxed);
        next.run(input)
    }
}

struct CountingWorkerInterceptor {
    calls: Arc<AtomicUsize>,
}

#[async_trait::async_trait(?Send)]
impl WorkerInterceptor for CountingWorkerInterceptor {
    async fn on_workflow_activation(
        &self,
        _activation: &WorkflowActivation,
    ) -> Result<(), anyhow::Error> {
        self.calls.fetch_add(1, Ordering::Relaxed);
        Ok(())
    }
}

#[workflow]
#[derive(Default)]
struct SimplePluginWorkflow;

struct SimplePluginActivities;

#[activities]
impl SimplePluginActivities {
    #[activity]
    async fn greet(_ctx: ActivityContext, name: String) -> Result<String, ActivityError> {
        Ok(format!("Hello, {name}!"))
    }
}

#[workflow_methods]
impl SimplePluginWorkflow {
    #[run]
    async fn run(ctx: &mut WorkflowContext<Self>, name: String) -> WorkflowResult<String> {
        Ok(ctx
            .execute_activity(
                SimplePluginActivities::greet,
                name,
                ActivityOptions::start_to_close_timeout(Duration::from_secs(5)),
            )
            .await?)
    }
}

#[tokio::test]
async fn simple_plugin_configures_working_client_and_worker() {
    let encode_calls = Arc::new(AtomicUsize::new(0));
    let decode_calls = Arc::new(AtomicUsize::new(0));
    let client_interceptor_calls = Arc::new(AtomicUsize::new(0));
    let worker_interceptor_calls = Arc::new(AtomicUsize::new(0));
    let data_converter = DataConverter::new(
        PayloadConverter::default(),
        DefaultFailureConverter,
        CountingPayloadCodec {
            encode_calls: encode_calls.clone(),
            decode_calls: decode_calls.clone(),
        },
    );
    let mut activities = ActivityDefinitions::default();
    activities.register_activities(SimplePluginActivities);
    let mut workflows = WorkflowDefinitions::new();
    workflows
        .register_workflow::<SimplePluginWorkflow>()
        .unwrap();
    let plugin = SimplePlugin::builder("simple-integration-plugin")
        .data_converter(data_converter)
        .client_interceptors(vec![Arc::new(CountingClientInterceptor {
            calls: client_interceptor_calls.clone(),
        }) as Arc<dyn ClientInterceptor>])
        .worker_interceptors(vec![Arc::new(CountingWorkerInterceptor {
            calls: worker_interceptor_calls.clone(),
        }) as Arc<dyn WorkerInterceptor>])
        .activities(activities)
        .workflows(workflows)
        .build();
    let client_options = ClientOptions::new(integ_namespace()).plugin(plugin).build();
    let client = Client::connect(get_integ_server_options(), client_options)
        .await
        .unwrap();
    let runtime = new_sdk_runtime();
    let task_queue = format!("simple-plugin-{}", Uuid::new_v4());
    let mut worker = Worker::new(
        &runtime,
        client.clone(),
        WorkerOptions::new(task_queue.clone()).build(),
    )
    .unwrap();
    let workflow_id = format!("simple-plugin-{}", Uuid::new_v4());
    let handle = client
        .start_workflow(
            SimplePluginWorkflow::run,
            "Temporal".to_owned(),
            WorkflowStartOptions::new(task_queue, workflow_id).build(),
        )
        .await
        .unwrap();

    let shutdown = worker.shutdown_handle();
    let (workflow_result, worker_result) = tokio::join!(
        async {
            let result = handle.get_result(Default::default()).await;
            shutdown();
            result
        },
        worker.run(),
    );
    worker_result.unwrap();
    let workflow_result = workflow_result.unwrap();
    assert_eq!(workflow_result, "Hello, Temporal!");
    assert_eq!(client_interceptor_calls.load(Ordering::Relaxed), 1);
    assert!(worker_interceptor_calls.load(Ordering::Relaxed) > 0);
    assert!(encode_calls.load(Ordering::Relaxed) > 0);
    assert!(decode_calls.load(Ordering::Relaxed) > 0);
}

#[tokio::test]
async fn plugin_errors_surface() {
    let connection_result = Client::connect(
        get_integ_server_options(),
        ClientOptions::new(integ_namespace())
            .client_plugin(FailingConnectionPlugin)
            .build(),
    )
    .await;
    assert_eq!(
        connection_result.unwrap_err().to_string(),
        "plugin 'failing-connection' failed to configure connection options: connection failure"
    );

    let client_result = Client::connect(
        get_integ_server_options(),
        ClientOptions::new(integ_namespace())
            .client_plugin(FailingClientPlugin)
            .build(),
    )
    .await;
    assert_eq!(
        client_result.unwrap_err().to_string(),
        "plugin 'failing-client' failed to configure client options: client failure"
    );

    let client = Client::connect(
        get_integ_server_options(),
        ClientOptions::new(integ_namespace()).build(),
    )
    .await
    .unwrap();
    let worker_result = Worker::new(
        &new_sdk_runtime(),
        client,
        WorkerOptions::new(format!("failing-plugin-{}", Uuid::new_v4()))
            .worker_plugin(FailingWorkerPlugin)
            .build(),
    );
    assert_eq!(
        worker_result.unwrap_err().to_string(),
        "plugin 'failing-worker' failed to configure worker options: worker failure"
    );
}

struct FailingConnectionPlugin;

impl ClientPlugin for FailingConnectionPlugin {
    fn name(&self) -> &str {
        "failing-connection"
    }

    fn configure_connection_options(
        &self,
        _options: &mut ConnectionOptions,
    ) -> Result<(), PluginError> {
        Err(PluginError::new("connection failure"))
    }
}

struct FailingClientPlugin;

impl ClientPlugin for FailingClientPlugin {
    fn name(&self) -> &str {
        "failing-client"
    }

    fn configure_client_options(&self, _options: &mut ClientOptions) -> Result<(), PluginError> {
        Err(PluginError::new("client failure"))
    }
}

struct FailingWorkerPlugin;

impl WorkerPlugin for FailingWorkerPlugin {
    fn name(&self) -> &str {
        "failing-worker"
    }

    fn configure_worker_options(&self, _options: &mut WorkerOptions) -> Result<(), PluginError> {
        Err(PluginError::new("worker failure"))
    }
}

struct ClientOnlyMetadataPlugin;

impl ClientPlugin for ClientOnlyMetadataPlugin {
    fn name(&self) -> &str {
        "client-only-plugin"
    }
}

struct WorkerOnlyMetadataPlugin;

impl WorkerPlugin for WorkerOnlyMetadataPlugin {
    fn name(&self) -> &str {
        "worker-only-plugin"
    }
}

#[tokio::test]
async fn worker_metadata_includes_client_and_worker_plugin_names() {
    let runtime = new_sdk_runtime();
    let client = Client::connect(
        get_integ_server_options(),
        ClientOptions::new(integ_namespace())
            .client_plugin(ClientOnlyMetadataPlugin)
            .build(),
    )
    .await
    .unwrap();
    let worker = Worker::new(
        &runtime,
        client,
        WorkerOptions::new(format!("plugin-metadata-{}", Uuid::new_v4()))
            .register_workflow::<SimplePluginWorkflow>()
            .unwrap()
            .worker_plugin(WorkerOnlyMetadataPlugin)
            .build(),
    )
    .unwrap();
    let core_worker = worker.core_worker();
    let mut names = core_worker
        .get_config()
        .plugins
        .iter()
        .map(|plugin| plugin.name.clone())
        .collect::<Vec<_>>();
    names.sort_unstable();

    assert_eq!(names, ["client-only-plugin", "worker-only-plugin"]);
}