@temporalio/core-bridge 0.20.2 → 0.22.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.lock +137 -127
- package/index.d.ts +7 -2
- package/package.json +3 -3
- package/releases/aarch64-apple-darwin/index.node +0 -0
- package/releases/x86_64-apple-darwin/index.node +0 -0
- package/releases/x86_64-pc-windows-msvc/index.node +0 -0
- package/releases/x86_64-unknown-linux-gnu/index.node +0 -0
- package/sdk-core/.buildkite/docker/docker-compose.yaml +5 -4
- package/sdk-core/client/Cargo.toml +1 -0
- package/sdk-core/client/src/lib.rs +52 -9
- package/sdk-core/client/src/raw.rs +9 -1
- package/sdk-core/client/src/retry.rs +12 -1
- package/sdk-core/client/src/workflow_handle/mod.rs +183 -0
- package/sdk-core/core/src/abstractions.rs +10 -3
- package/sdk-core/core/src/core_tests/child_workflows.rs +7 -9
- package/sdk-core/core/src/core_tests/determinism.rs +8 -19
- package/sdk-core/core/src/core_tests/local_activities.rs +22 -32
- package/sdk-core/core/src/core_tests/queries.rs +272 -5
- package/sdk-core/core/src/core_tests/workers.rs +4 -34
- package/sdk-core/core/src/core_tests/workflow_tasks.rs +197 -41
- package/sdk-core/core/src/pending_activations.rs +11 -0
- package/sdk-core/core/src/telemetry/mod.rs +1 -1
- package/sdk-core/core/src/test_help/mod.rs +57 -7
- package/sdk-core/core/src/worker/mod.rs +64 -15
- package/sdk-core/core/src/workflow/machines/mod.rs +1 -1
- package/sdk-core/core/src/workflow/machines/timer_state_machine.rs +2 -2
- package/sdk-core/core/src/workflow/machines/workflow_machines.rs +14 -3
- package/sdk-core/core/src/workflow/mod.rs +5 -2
- package/sdk-core/core/src/workflow/workflow_tasks/cache_manager.rs +47 -2
- package/sdk-core/core/src/workflow/workflow_tasks/concurrency_manager.rs +16 -2
- package/sdk-core/core/src/workflow/workflow_tasks/mod.rs +252 -125
- package/sdk-core/core-api/src/worker.rs +9 -0
- package/sdk-core/sdk/Cargo.toml +1 -0
- package/sdk-core/sdk/src/activity_context.rs +223 -0
- package/sdk-core/sdk/src/interceptors.rs +8 -2
- package/sdk-core/sdk/src/lib.rs +167 -122
- package/sdk-core/sdk-core-protos/src/history_info.rs +3 -7
- package/sdk-core/test-utils/Cargo.toml +1 -0
- package/sdk-core/test-utils/src/lib.rs +78 -37
- package/sdk-core/tests/integ_tests/workflow_tests/activities.rs +11 -4
- package/sdk-core/tests/integ_tests/workflow_tests/child_workflows.rs +0 -1
- package/sdk-core/tests/integ_tests/workflow_tests/continue_as_new.rs +0 -3
- package/sdk-core/tests/integ_tests/workflow_tests/local_activities.rs +33 -17
- package/sdk-core/tests/integ_tests/workflow_tests/resets.rs +10 -1
- package/sdk-core/tests/integ_tests/workflow_tests/signals.rs +0 -1
- package/sdk-core/tests/integ_tests/workflow_tests.rs +71 -3
- package/sdk-core/tests/load_tests.rs +80 -6
- package/src/errors.rs +9 -2
- package/src/lib.rs +39 -16
- package/releases/aarch64-unknown-linux-gnu/index.node +0 -0
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
use prost_types::{Duration, Timestamp};
|
|
2
|
+
use std::{
|
|
3
|
+
collections::HashMap,
|
|
4
|
+
sync::Arc,
|
|
5
|
+
time::{Duration as StdDuration, SystemTime},
|
|
6
|
+
};
|
|
7
|
+
use temporal_sdk_core_api::Worker;
|
|
8
|
+
use temporal_sdk_core_protos::{
|
|
9
|
+
coresdk::{
|
|
10
|
+
activity_task,
|
|
11
|
+
common::{Payload, RetryPolicy, WorkflowExecution},
|
|
12
|
+
ActivityHeartbeat,
|
|
13
|
+
},
|
|
14
|
+
utilities::TryIntoOrNone,
|
|
15
|
+
};
|
|
16
|
+
use tokio_util::sync::CancellationToken;
|
|
17
|
+
|
|
18
|
+
/// Used within activities to get info, heartbeat management etc.
|
|
19
|
+
#[derive(Clone)]
|
|
20
|
+
pub struct ActContext {
|
|
21
|
+
worker: Arc<dyn Worker>,
|
|
22
|
+
cancellation_token: CancellationToken,
|
|
23
|
+
input: Vec<Payload>,
|
|
24
|
+
heartbeat_details: Vec<Payload>,
|
|
25
|
+
header_fields: HashMap<String, Payload>,
|
|
26
|
+
info: ActivityInfo,
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
#[derive(Clone)]
|
|
30
|
+
pub struct ActivityInfo {
|
|
31
|
+
pub task_token: Vec<u8>,
|
|
32
|
+
pub workflow_type: String,
|
|
33
|
+
pub workflow_namespace: String,
|
|
34
|
+
pub workflow_execution: Option<WorkflowExecution>,
|
|
35
|
+
pub activity_id: String,
|
|
36
|
+
pub activity_type: String,
|
|
37
|
+
pub task_queue: String,
|
|
38
|
+
pub heartbeat_timeout: Option<StdDuration>,
|
|
39
|
+
/// Time activity was scheduled by a workflow
|
|
40
|
+
pub scheduled_time: Option<SystemTime>,
|
|
41
|
+
/// Time of activity start
|
|
42
|
+
pub started_time: Option<SystemTime>,
|
|
43
|
+
/// Time of activity timeout
|
|
44
|
+
pub deadline: Option<SystemTime>,
|
|
45
|
+
/// Attempt starts from 1, and increase by 1 for every retry, if retry policy is specified.
|
|
46
|
+
pub attempt: u32,
|
|
47
|
+
/// Time this attempt at the activity was scheduled
|
|
48
|
+
pub current_attempt_scheduled_time: Option<SystemTime>,
|
|
49
|
+
pub retry_policy: Option<RetryPolicy>,
|
|
50
|
+
pub is_local: bool,
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
impl ActContext {
|
|
54
|
+
/// Construct new Activity Context, returning the context and the first argument to the activity
|
|
55
|
+
/// (which may be a default [Payload]).
|
|
56
|
+
pub(crate) fn new(
|
|
57
|
+
worker: Arc<dyn Worker>,
|
|
58
|
+
cancellation_token: CancellationToken,
|
|
59
|
+
task_queue: String,
|
|
60
|
+
task_token: Vec<u8>,
|
|
61
|
+
task: activity_task::Start,
|
|
62
|
+
) -> (Self, Payload) {
|
|
63
|
+
let activity_task::Start {
|
|
64
|
+
workflow_namespace,
|
|
65
|
+
workflow_type,
|
|
66
|
+
workflow_execution,
|
|
67
|
+
activity_id,
|
|
68
|
+
activity_type,
|
|
69
|
+
header_fields,
|
|
70
|
+
mut input,
|
|
71
|
+
heartbeat_details,
|
|
72
|
+
scheduled_time,
|
|
73
|
+
current_attempt_scheduled_time,
|
|
74
|
+
started_time,
|
|
75
|
+
attempt,
|
|
76
|
+
schedule_to_close_timeout,
|
|
77
|
+
start_to_close_timeout,
|
|
78
|
+
heartbeat_timeout,
|
|
79
|
+
retry_policy,
|
|
80
|
+
is_local,
|
|
81
|
+
} = task;
|
|
82
|
+
let deadline = calculate_deadline(
|
|
83
|
+
scheduled_time.as_ref(),
|
|
84
|
+
started_time.as_ref(),
|
|
85
|
+
start_to_close_timeout.as_ref(),
|
|
86
|
+
schedule_to_close_timeout.as_ref(),
|
|
87
|
+
);
|
|
88
|
+
let first_arg = input.pop().unwrap_or_default();
|
|
89
|
+
|
|
90
|
+
(
|
|
91
|
+
ActContext {
|
|
92
|
+
worker,
|
|
93
|
+
cancellation_token,
|
|
94
|
+
input,
|
|
95
|
+
heartbeat_details,
|
|
96
|
+
header_fields,
|
|
97
|
+
info: ActivityInfo {
|
|
98
|
+
task_token,
|
|
99
|
+
task_queue,
|
|
100
|
+
workflow_type,
|
|
101
|
+
workflow_namespace,
|
|
102
|
+
workflow_execution,
|
|
103
|
+
activity_id,
|
|
104
|
+
activity_type,
|
|
105
|
+
heartbeat_timeout: heartbeat_timeout.try_into_or_none(),
|
|
106
|
+
scheduled_time: scheduled_time.try_into_or_none(),
|
|
107
|
+
started_time: started_time.try_into_or_none(),
|
|
108
|
+
deadline,
|
|
109
|
+
attempt,
|
|
110
|
+
current_attempt_scheduled_time: current_attempt_scheduled_time
|
|
111
|
+
.try_into_or_none(),
|
|
112
|
+
retry_policy,
|
|
113
|
+
is_local,
|
|
114
|
+
},
|
|
115
|
+
},
|
|
116
|
+
first_arg,
|
|
117
|
+
)
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/// Returns a future the completes if and when the activity this was called inside has been
|
|
121
|
+
/// cancelled
|
|
122
|
+
pub async fn cancelled(&self) {
|
|
123
|
+
self.cancellation_token.clone().cancelled().await
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/// Returns true if this activity has already been cancelled
|
|
127
|
+
pub fn is_cancelled(&self) -> bool {
|
|
128
|
+
self.cancellation_token.is_cancelled()
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/// Retrieve extra parameters to the Activity. The first input is always popped and passed to
|
|
132
|
+
/// the Activity function for the currently executing activity. However, if more parameters are
|
|
133
|
+
/// passed, perhaps from another language's SDK, explicit access is available from extra_inputs
|
|
134
|
+
pub fn extra_inputs(&mut self) -> &mut [Payload] {
|
|
135
|
+
&mut self.input
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/// Extract heartbeat details from last failed attempt. This is used in combination with retry policy.
|
|
139
|
+
pub fn get_heartbeat_details(&self) -> &[Payload] {
|
|
140
|
+
&self.heartbeat_details
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/// RecordHeartbeat sends heartbeat for the currently executing activity
|
|
144
|
+
pub fn record_heartbeat(&self, details: Vec<Payload>) {
|
|
145
|
+
self.worker.record_activity_heartbeat(ActivityHeartbeat {
|
|
146
|
+
task_token: self.info.task_token.clone(),
|
|
147
|
+
details,
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/// Get activity info of the executing activity
|
|
152
|
+
pub fn get_info(&self) -> &ActivityInfo {
|
|
153
|
+
&self.info
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/// Get headers attached to this activity
|
|
157
|
+
pub fn headers(&self) -> &HashMap<String, Payload> {
|
|
158
|
+
&self.header_fields
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/// Deadline calculation. This is a port of
|
|
163
|
+
/// https://github.com/temporalio/sdk-go/blob/8651550973088f27f678118f997839fb1bb9e62f/internal/activity.go#L225
|
|
164
|
+
fn calculate_deadline(
|
|
165
|
+
scheduled_time: Option<&Timestamp>,
|
|
166
|
+
started_time: Option<&Timestamp>,
|
|
167
|
+
start_to_close_timeout: Option<&Duration>,
|
|
168
|
+
schedule_to_close_timeout: Option<&Duration>,
|
|
169
|
+
) -> Option<SystemTime> {
|
|
170
|
+
match (
|
|
171
|
+
scheduled_time,
|
|
172
|
+
started_time,
|
|
173
|
+
start_to_close_timeout,
|
|
174
|
+
schedule_to_close_timeout,
|
|
175
|
+
) {
|
|
176
|
+
(
|
|
177
|
+
Some(scheduled),
|
|
178
|
+
Some(started),
|
|
179
|
+
Some(start_to_close_timeout),
|
|
180
|
+
Some(schedule_to_close_timeout),
|
|
181
|
+
) => {
|
|
182
|
+
let scheduled: SystemTime = maybe_convert_timestamp(scheduled)?;
|
|
183
|
+
let started: SystemTime = maybe_convert_timestamp(started)?;
|
|
184
|
+
let start_to_close_timeout: StdDuration =
|
|
185
|
+
start_to_close_timeout.clone().try_into().ok()?;
|
|
186
|
+
let schedule_to_close_timeout: StdDuration =
|
|
187
|
+
schedule_to_close_timeout.clone().try_into().ok()?;
|
|
188
|
+
|
|
189
|
+
let start_to_close_deadline: SystemTime =
|
|
190
|
+
started.checked_add(start_to_close_timeout)?;
|
|
191
|
+
if schedule_to_close_timeout > StdDuration::ZERO {
|
|
192
|
+
let schedule_to_close_deadline =
|
|
193
|
+
scheduled.checked_add(schedule_to_close_timeout)?;
|
|
194
|
+
// Minimum of the two deadlines.
|
|
195
|
+
if schedule_to_close_deadline < start_to_close_deadline {
|
|
196
|
+
Some(schedule_to_close_deadline)
|
|
197
|
+
} else {
|
|
198
|
+
Some(start_to_close_deadline)
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
Some(start_to_close_deadline)
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
_ => None,
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/// Helper function lifted from prost_types::Timestamp implementation to prevent double cloning in
|
|
209
|
+
/// error construction
|
|
210
|
+
fn maybe_convert_timestamp(timestamp: &Timestamp) -> Option<SystemTime> {
|
|
211
|
+
let mut timestamp = timestamp.clone();
|
|
212
|
+
timestamp.normalize();
|
|
213
|
+
|
|
214
|
+
let system_time = if timestamp.seconds >= 0 {
|
|
215
|
+
std::time::UNIX_EPOCH.checked_add(StdDuration::from_secs(timestamp.seconds as u64))
|
|
216
|
+
} else {
|
|
217
|
+
std::time::UNIX_EPOCH.checked_sub(StdDuration::from_secs((-timestamp.seconds) as u64))
|
|
218
|
+
};
|
|
219
|
+
|
|
220
|
+
system_time.and_then(|system_time| {
|
|
221
|
+
system_time.checked_add(StdDuration::from_nanos(timestamp.nanos as u64))
|
|
222
|
+
})
|
|
223
|
+
}
|
|
@@ -1,11 +1,17 @@
|
|
|
1
1
|
//! User-definable interceptors are defined in this module
|
|
2
2
|
|
|
3
|
+
use crate::Worker;
|
|
3
4
|
use temporal_sdk_core_protos::coresdk::workflow_completion::WorkflowActivationCompletion;
|
|
4
5
|
|
|
5
6
|
/// Implementors can intercept certain actions that happen within the Worker.
|
|
6
7
|
///
|
|
7
8
|
/// Advanced usage only.
|
|
9
|
+
#[async_trait::async_trait(?Send)]
|
|
8
10
|
pub trait WorkerInterceptor {
|
|
9
|
-
/// Called every time a workflow activation completes
|
|
10
|
-
|
|
11
|
+
/// Called every time a workflow activation completes (just before sending the completion to
|
|
12
|
+
/// core).
|
|
13
|
+
async fn on_workflow_activation_completion(&self, completion: &WorkflowActivationCompletion);
|
|
14
|
+
/// Called after the worker has initiated shutdown and the workflow/activity polling loops
|
|
15
|
+
/// have exited, but just before waiting for the inner core worker shutdown
|
|
16
|
+
fn on_shutdown(&self, sdk_worker: &Worker);
|
|
11
17
|
}
|