1
1
#![ warn( missing_docs) ] // error if there are missing docs
2
2
3
- //! This crate is a rough prototype Rust SDK. It can be used to create closures that look sort of
4
- //! like normal workflow code. It should only depend on things in the core crate that are already
5
- //! publicly exposed.
3
+ //! This crate defines an alpha-stage Temporal Rust SDK.
6
4
//!
7
- //! Needs lots of love to be production ready but the basis is there
5
+ //! Currently defining activities and running an activity-only worker is the most stable code.
6
+ //! Workflow definitions exist and running a workflow worker works, but the API is still very
7
+ //! unstable.
8
+ //!
9
+ //! An example of running an activity worker:
10
+ //! ```no_run
11
+ //! use std::sync::Arc;
12
+ //! use std::str::FromStr;
13
+ //! use temporal_sdk::{sdk_client_options, Worker};
14
+ //! use temporal_sdk_core::{init_worker, Url};
15
+ //! use temporal_sdk_core_api::worker::{WorkerConfig, WorkerConfigBuilder};
16
+ //!
17
+ //! #[tokio::main]
18
+ //! async fn main() -> Result<(), Box<dyn std::error::Error>> {
19
+ //! let server_options = sdk_client_options(Url::from_str("http://localhost:7233")?).build()?;
20
+ //! let client = server_options.connect("my_namespace", None).await?;
21
+ //! let worker_config = WorkerConfigBuilder::default().build()?;
22
+ //! let core_worker = init_worker(worker_config, client);
23
+ //!
24
+ //! let mut worker = Worker::new(Arc::new(core_worker), "task_queue", None);
25
+ //! worker.register_activity(
26
+ //! "echo_activity",
27
+ //! |echo_me: String| async move { Ok(echo_me) },
28
+ //! );
29
+ //! // TODO: This should be different
30
+ //! worker.run_until_done().await?;
31
+ //! Ok(())
32
+ //! }
33
+ //! ```
8
34
9
35
#[ macro_use]
10
36
extern crate tracing;
@@ -22,6 +48,7 @@ pub use workflow_context::{
22
48
use crate :: workflow_context:: { ChildWfCommon , PendingChildWorkflow } ;
23
49
use anyhow:: { anyhow, bail} ;
24
50
use futures:: { future:: BoxFuture , stream:: FuturesUnordered , FutureExt , StreamExt } ;
51
+ use once_cell:: sync:: OnceCell ;
25
52
use std:: {
26
53
collections:: HashMap ,
27
54
fmt:: { Debug , Display , Formatter } ,
@@ -32,7 +59,8 @@ use std::{
32
59
} ,
33
60
time:: Duration ,
34
61
} ;
35
- use temporal_client:: { ServerGatewayApis , WorkflowOptions } ;
62
+ use temporal_client:: { ServerGatewayApis , ServerGatewayOptionsBuilder , WorkflowOptions } ;
63
+ use temporal_sdk_core:: Url ;
36
64
use temporal_sdk_core_api:: {
37
65
errors:: { PollActivityError , PollWfError } ,
38
66
Worker as CoreWorker ,
@@ -64,6 +92,21 @@ use tokio::{
64
92
} ;
65
93
use tokio_util:: sync:: CancellationToken ;
66
94
95
+ const VERSION : & str = env ! ( "CARGO_PKG_VERSION" ) ;
96
+
97
+ /// Returns a [ServerGatewayOptionsBuilder] with required fields set to appropriate values
98
+ /// for the Rust SDK.
99
+ pub fn sdk_client_options ( url : impl Into < Url > ) -> ServerGatewayOptionsBuilder {
100
+ let mut builder = ServerGatewayOptionsBuilder :: default ( ) ;
101
+ builder
102
+ . target_url ( url)
103
+ . client_name ( "rust-sdk" . to_string ( ) )
104
+ . client_version ( VERSION . to_string ( ) )
105
+ . worker_binary_id ( binary_id ( ) . to_string ( ) ) ;
106
+
107
+ builder
108
+ }
109
+
67
110
/// A worker that can poll for and respond to workflow tasks by using [WorkflowFunction]s,
68
111
/// and activity tasks by using [ActivityFunction]s
69
112
pub struct Worker {
@@ -96,12 +139,12 @@ impl Worker {
96
139
/// Create a new rust worker
97
140
pub fn new (
98
141
worker : Arc < dyn CoreWorker > ,
99
- task_queue : String ,
142
+ task_queue : impl Into < String > ,
100
143
task_timeout : Option < Duration > ,
101
144
) -> Self {
102
145
Self {
103
146
worker,
104
- task_queue,
147
+ task_queue : task_queue . into ( ) ,
105
148
task_timeout,
106
149
workflow_half : WorkflowHalf {
107
150
workflows : Default :: default ( ) ,
@@ -683,3 +726,20 @@ where
683
726
Arc :: new ( wrapper)
684
727
}
685
728
}
729
+
730
+ /// Reads own binary, hashes it, and returns b64 str version of that hash
731
+ fn binary_id ( ) -> & ' static str {
732
+ use sha2:: { Digest , Sha256 } ;
733
+ use std:: { env, fs, io} ;
734
+
735
+ static INSTANCE : OnceCell < String > = OnceCell :: new ( ) ;
736
+ INSTANCE . get_or_init ( || {
737
+ let exe_path = env:: current_exe ( ) . expect ( "Cannot read own binary to determine binary id" ) ;
738
+ let mut exe_file =
739
+ fs:: File :: open ( exe_path) . expect ( "Cannot read own binary to determine binary id" ) ;
740
+ let mut hasher = Sha256 :: new ( ) ;
741
+ io:: copy ( & mut exe_file, & mut hasher) . expect ( "Copying data into binary hasher works" ) ;
742
+ let hash = hasher. finalize ( ) ;
743
+ base64:: encode ( hash)
744
+ } )
745
+ }
0 commit comments