@temporalio/core-bridge 1.14.2-canary-release-testing.0 → 1.16.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.
Files changed (233) hide show
  1. package/Cargo.lock +794 -650
  2. package/bridge-macros/src/derive_tryintojs.rs +40 -0
  3. package/lib/native.d.ts +24 -3
  4. package/package.json +4 -4
  5. package/releases/aarch64-apple-darwin/index.node +0 -0
  6. package/releases/aarch64-unknown-linux-gnu/index.node +0 -0
  7. package/releases/x86_64-apple-darwin/index.node +0 -0
  8. package/releases/x86_64-pc-windows-msvc/index.node +0 -0
  9. package/releases/x86_64-unknown-linux-gnu/index.node +0 -0
  10. package/sdk-core/.github/workflows/per-pr.yml +6 -6
  11. package/sdk-core/AGENTS.md +42 -31
  12. package/sdk-core/Cargo.toml +4 -1
  13. package/sdk-core/README.md +19 -13
  14. package/sdk-core/crates/client/Cargo.toml +4 -0
  15. package/sdk-core/crates/client/README.md +139 -0
  16. package/sdk-core/crates/client/src/async_activity_handle.rs +297 -0
  17. package/sdk-core/crates/client/src/callback_based.rs +7 -0
  18. package/sdk-core/crates/client/src/errors.rs +294 -0
  19. package/sdk-core/crates/client/src/{raw.rs → grpc.rs} +370 -159
  20. package/sdk-core/crates/client/src/lib.rs +920 -1326
  21. package/sdk-core/crates/client/src/metrics.rs +24 -33
  22. package/sdk-core/crates/client/src/options_structs.rs +457 -0
  23. package/sdk-core/crates/client/src/replaceable.rs +5 -4
  24. package/sdk-core/crates/client/src/request_extensions.rs +8 -9
  25. package/sdk-core/crates/client/src/retry.rs +99 -54
  26. package/sdk-core/crates/client/src/{worker/mod.rs → worker.rs} +104 -29
  27. package/sdk-core/crates/client/src/workflow_handle.rs +826 -0
  28. package/sdk-core/crates/common/Cargo.toml +62 -3
  29. package/sdk-core/crates/common/build.rs +742 -12
  30. package/sdk-core/crates/common/protos/api_upstream/.github/workflows/ci.yml +2 -0
  31. package/sdk-core/crates/common/protos/api_upstream/.github/workflows/create-release.yml +0 -5
  32. package/sdk-core/crates/common/protos/api_upstream/Makefile +2 -1
  33. package/sdk-core/crates/common/protos/api_upstream/README.md +8 -0
  34. package/sdk-core/crates/common/protos/api_upstream/cmd/check-path-conflicts/main.go +137 -0
  35. package/sdk-core/crates/common/protos/api_upstream/openapi/openapiv2.json +3329 -2647
  36. package/sdk-core/crates/common/protos/api_upstream/openapi/openapiv3.yaml +2734 -708
  37. package/sdk-core/crates/common/protos/api_upstream/temporal/api/activity/v1/message.proto +155 -3
  38. package/sdk-core/crates/common/protos/api_upstream/temporal/api/command/v1/message.proto +26 -0
  39. package/sdk-core/crates/common/protos/api_upstream/temporal/api/common/v1/message.proto +8 -1
  40. package/sdk-core/crates/common/protos/api_upstream/temporal/api/deployment/v1/message.proto +27 -1
  41. package/sdk-core/crates/common/protos/api_upstream/temporal/api/enums/v1/activity.proto +81 -0
  42. package/sdk-core/crates/common/protos/api_upstream/temporal/api/enums/v1/event_type.proto +4 -0
  43. package/sdk-core/crates/common/protos/api_upstream/temporal/api/enums/v1/failed_cause.proto +4 -0
  44. package/sdk-core/crates/common/protos/api_upstream/temporal/api/enums/v1/task_queue.proto +15 -0
  45. package/sdk-core/crates/common/protos/api_upstream/temporal/api/enums/v1/workflow.proto +63 -15
  46. package/sdk-core/crates/common/protos/api_upstream/temporal/api/errordetails/v1/message.proto +8 -0
  47. package/sdk-core/crates/common/protos/api_upstream/temporal/api/failure/v1/message.proto +1 -0
  48. package/sdk-core/crates/common/protos/api_upstream/temporal/api/history/v1/message.proto +111 -17
  49. package/sdk-core/crates/common/protos/api_upstream/temporal/api/namespace/v1/message.proto +21 -0
  50. package/sdk-core/crates/common/protos/api_upstream/temporal/api/nexus/v1/message.proto +20 -1
  51. package/sdk-core/crates/common/protos/api_upstream/temporal/api/operatorservice/v1/request_response.proto +4 -0
  52. package/sdk-core/crates/common/protos/api_upstream/temporal/api/schedule/v1/message.proto +2 -2
  53. package/sdk-core/crates/common/protos/api_upstream/temporal/api/taskqueue/v1/message.proto +2 -0
  54. package/sdk-core/crates/common/protos/api_upstream/temporal/api/worker/v1/message.proto +4 -7
  55. package/sdk-core/crates/common/protos/api_upstream/temporal/api/workflow/v1/message.proto +80 -22
  56. package/sdk-core/crates/common/protos/api_upstream/temporal/api/workflowservice/v1/request_response.proto +347 -23
  57. package/sdk-core/crates/common/protos/api_upstream/temporal/api/workflowservice/v1/service.proto +242 -43
  58. package/sdk-core/crates/common/protos/local/temporal/sdk/core/core_interface.proto +15 -0
  59. package/sdk-core/crates/common/protos/local/temporal/sdk/core/nexus/nexus.proto +9 -2
  60. package/sdk-core/crates/common/protos/local/temporal/sdk/core/workflow_activation/workflow_activation.proto +8 -0
  61. package/sdk-core/crates/common/protos/local/temporal/sdk/core/workflow_commands/workflow_commands.proto +22 -5
  62. package/sdk-core/crates/common/src/activity_definition.rs +20 -0
  63. package/sdk-core/crates/common/src/data_converters.rs +770 -0
  64. package/sdk-core/crates/common/src/envconfig.rs +5 -0
  65. package/sdk-core/crates/common/src/lib.rs +15 -211
  66. package/sdk-core/crates/common/src/payload_visitor.rs +648 -0
  67. package/sdk-core/crates/common/src/priority.rs +110 -0
  68. package/sdk-core/crates/common/src/protos/canned_histories.rs +19 -0
  69. package/sdk-core/crates/common/src/protos/history_builder.rs +45 -0
  70. package/sdk-core/crates/common/src/protos/history_info.rs +2 -0
  71. package/sdk-core/crates/common/src/protos/mod.rs +134 -27
  72. package/sdk-core/crates/common/src/protos/task_token.rs +3 -3
  73. package/sdk-core/crates/common/src/protos/utilities.rs +11 -0
  74. package/sdk-core/crates/{sdk-core → common}/src/telemetry/log_export.rs +11 -16
  75. package/sdk-core/crates/common/src/telemetry/metrics/core.rs +125 -0
  76. package/sdk-core/crates/common/src/telemetry/metrics.rs +272 -225
  77. package/sdk-core/crates/{sdk-core → common}/src/telemetry/otel.rs +8 -13
  78. package/sdk-core/crates/{sdk-core → common}/src/telemetry/prometheus_meter.rs +49 -50
  79. package/sdk-core/crates/{sdk-core → common}/src/telemetry/prometheus_server.rs +2 -3
  80. package/sdk-core/crates/common/src/telemetry.rs +278 -19
  81. package/sdk-core/crates/common/src/worker.rs +68 -636
  82. package/sdk-core/crates/common/src/workflow_definition.rs +60 -0
  83. package/sdk-core/crates/macros/Cargo.toml +5 -1
  84. package/sdk-core/crates/macros/src/activities_definitions.rs +585 -0
  85. package/sdk-core/crates/macros/src/fsm_impl.rs +507 -0
  86. package/sdk-core/crates/macros/src/lib.rs +138 -512
  87. package/sdk-core/crates/macros/src/macro_utils.rs +106 -0
  88. package/sdk-core/crates/macros/src/workflow_definitions.rs +1224 -0
  89. package/sdk-core/crates/sdk/Cargo.toml +19 -6
  90. package/sdk-core/crates/sdk/README.md +415 -0
  91. package/sdk-core/crates/sdk/src/activities.rs +417 -0
  92. package/sdk-core/crates/sdk/src/interceptors.rs +1 -1
  93. package/sdk-core/crates/sdk/src/lib.rs +759 -442
  94. package/sdk-core/crates/sdk/src/workflow_context/options.rs +64 -35
  95. package/sdk-core/crates/sdk/src/workflow_context.rs +1033 -289
  96. package/sdk-core/crates/sdk/src/workflow_future.rs +277 -213
  97. package/sdk-core/crates/sdk/src/workflows.rs +711 -0
  98. package/sdk-core/crates/sdk-core/Cargo.toml +59 -65
  99. package/sdk-core/crates/sdk-core/benches/workflow_replay_bench.rs +45 -54
  100. package/sdk-core/crates/sdk-core/machine_coverage/ActivityMachine_Coverage.puml +1 -1
  101. package/sdk-core/crates/sdk-core/src/abstractions.rs +6 -10
  102. package/sdk-core/crates/sdk-core/src/core_tests/activity_tasks.rs +6 -5
  103. package/sdk-core/crates/sdk-core/src/core_tests/mod.rs +22 -21
  104. package/sdk-core/crates/sdk-core/src/core_tests/queries.rs +21 -25
  105. package/sdk-core/crates/sdk-core/src/core_tests/replay_flag.rs +7 -10
  106. package/sdk-core/crates/sdk-core/src/core_tests/updates.rs +14 -17
  107. package/sdk-core/crates/sdk-core/src/core_tests/workers.rs +647 -27
  108. package/sdk-core/crates/sdk-core/src/core_tests/workflow_tasks.rs +46 -41
  109. package/sdk-core/crates/sdk-core/src/ephemeral_server/mod.rs +13 -16
  110. package/sdk-core/crates/sdk-core/src/histfetch.rs +20 -10
  111. package/sdk-core/crates/sdk-core/src/lib.rs +60 -123
  112. package/sdk-core/crates/sdk-core/src/pollers/mod.rs +4 -9
  113. package/sdk-core/crates/sdk-core/src/pollers/poll_buffer.rs +411 -32
  114. package/sdk-core/crates/sdk-core/src/protosext/mod.rs +2 -2
  115. package/sdk-core/crates/sdk-core/src/replay/mod.rs +14 -5
  116. package/sdk-core/crates/sdk-core/src/telemetry/metrics.rs +183 -198
  117. package/sdk-core/crates/sdk-core/src/telemetry/mod.rs +3 -281
  118. package/sdk-core/crates/sdk-core/src/test_help/integ_helpers.rs +35 -16
  119. package/sdk-core/crates/sdk-core/src/test_help/unit_helpers.rs +3 -6
  120. package/sdk-core/crates/sdk-core/src/worker/activities/activity_heartbeat_manager.rs +1 -0
  121. package/sdk-core/crates/sdk-core/src/worker/activities/local_activities.rs +11 -14
  122. package/sdk-core/crates/sdk-core/src/worker/activities.rs +16 -19
  123. package/sdk-core/crates/sdk-core/src/worker/client/mocks.rs +11 -5
  124. package/sdk-core/crates/sdk-core/src/worker/client.rs +104 -86
  125. package/sdk-core/crates/sdk-core/src/worker/heartbeat.rs +10 -14
  126. package/sdk-core/crates/sdk-core/src/worker/mod.rs +1175 -241
  127. package/sdk-core/crates/sdk-core/src/worker/nexus.rs +150 -23
  128. package/sdk-core/crates/sdk-core/src/worker/slot_provider.rs +2 -2
  129. package/sdk-core/crates/sdk-core/src/worker/tuner/fixed_size.rs +2 -2
  130. package/sdk-core/crates/sdk-core/src/worker/tuner/resource_based.rs +25 -27
  131. package/sdk-core/crates/sdk-core/src/worker/tuner.rs +64 -44
  132. package/sdk-core/crates/sdk-core/src/worker/workflow/driven_workflow.rs +9 -3
  133. package/sdk-core/crates/sdk-core/src/worker/workflow/machines/patch_state_machine.rs +5 -8
  134. package/sdk-core/crates/sdk-core/src/worker/workflow/machines/upsert_search_attributes_state_machine.rs +21 -22
  135. package/sdk-core/crates/sdk-core/src/worker/workflow/machines/workflow_machines.rs +28 -4
  136. package/sdk-core/crates/sdk-core/src/worker/workflow/managed_run.rs +20 -41
  137. package/sdk-core/crates/sdk-core/src/worker/workflow/mod.rs +50 -9
  138. package/sdk-core/crates/sdk-core/src/worker/workflow/run_cache.rs +4 -7
  139. package/sdk-core/crates/sdk-core/src/worker/workflow/wft_extraction.rs +2 -4
  140. package/sdk-core/crates/sdk-core/src/worker/workflow/wft_poller.rs +8 -9
  141. package/sdk-core/crates/sdk-core/src/worker/workflow/workflow_stream.rs +1 -3
  142. package/sdk-core/crates/sdk-core/tests/activities_procmacro.rs +6 -0
  143. package/sdk-core/crates/sdk-core/tests/activities_trybuild/basic_pass.rs +54 -0
  144. package/sdk-core/crates/sdk-core/tests/activities_trybuild/invalid_self_type_fail.rs +18 -0
  145. package/sdk-core/crates/sdk-core/tests/activities_trybuild/invalid_self_type_fail.stderr +5 -0
  146. package/sdk-core/crates/sdk-core/tests/activities_trybuild/missing_context_fail.rs +14 -0
  147. package/sdk-core/crates/sdk-core/tests/activities_trybuild/missing_context_fail.stderr +5 -0
  148. package/sdk-core/crates/sdk-core/tests/activities_trybuild/multi_arg_pass.rs +48 -0
  149. package/sdk-core/crates/sdk-core/tests/activities_trybuild/no_input_pass.rs +14 -0
  150. package/sdk-core/crates/sdk-core/tests/activities_trybuild/no_return_type_pass.rs +19 -0
  151. package/sdk-core/crates/sdk-core/tests/cloud_tests.rs +14 -5
  152. package/sdk-core/crates/sdk-core/tests/common/activity_functions.rs +55 -0
  153. package/sdk-core/crates/sdk-core/tests/common/mod.rs +281 -236
  154. package/sdk-core/crates/sdk-core/tests/common/workflows.rs +41 -28
  155. package/sdk-core/crates/sdk-core/tests/global_metric_tests.rs +9 -14
  156. package/sdk-core/crates/sdk-core/tests/heavy_tests/fuzzy_workflow.rs +73 -66
  157. package/sdk-core/crates/sdk-core/tests/heavy_tests.rs +306 -268
  158. package/sdk-core/crates/sdk-core/tests/integ_tests/async_activity_client_tests.rs +230 -0
  159. package/sdk-core/crates/sdk-core/tests/integ_tests/client_tests.rs +94 -57
  160. package/sdk-core/crates/sdk-core/tests/integ_tests/data_converter_tests.rs +381 -0
  161. package/sdk-core/crates/sdk-core/tests/integ_tests/ephemeral_server_tests.rs +37 -38
  162. package/sdk-core/crates/sdk-core/tests/integ_tests/heartbeat_tests.rs +49 -40
  163. package/sdk-core/crates/sdk-core/tests/integ_tests/metrics_tests.rs +447 -300
  164. package/sdk-core/crates/sdk-core/tests/integ_tests/pagination_tests.rs +50 -45
  165. package/sdk-core/crates/sdk-core/tests/integ_tests/polling_tests.rs +157 -157
  166. package/sdk-core/crates/sdk-core/tests/integ_tests/queries_tests.rs +103 -89
  167. package/sdk-core/crates/sdk-core/tests/integ_tests/update_tests.rs +609 -463
  168. package/sdk-core/crates/sdk-core/tests/integ_tests/visibility_tests.rs +80 -62
  169. package/sdk-core/crates/sdk-core/tests/integ_tests/worker_heartbeat_tests.rs +389 -265
  170. package/sdk-core/crates/sdk-core/tests/integ_tests/worker_tests.rs +250 -185
  171. package/sdk-core/crates/sdk-core/tests/integ_tests/worker_versioning_tests.rs +52 -49
  172. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_client_tests.rs +180 -0
  173. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/activities.rs +437 -327
  174. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/cancel_external.rs +82 -58
  175. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/cancel_wf.rs +56 -30
  176. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/child_workflows.rs +364 -251
  177. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/client_interactions.rs +552 -0
  178. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/continue_as_new.rs +110 -46
  179. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/determinism.rs +243 -149
  180. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/eager.rs +98 -32
  181. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/local_activities.rs +1475 -1040
  182. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/modify_wf_properties.rs +73 -43
  183. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/nexus.rs +402 -245
  184. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/patches.rs +343 -207
  185. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/queries.rs +415 -0
  186. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/replay.rs +96 -36
  187. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/resets.rs +155 -140
  188. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/signals.rs +183 -113
  189. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/stickyness.rs +85 -44
  190. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/timers.rs +142 -48
  191. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/upsert_search_attrs.rs +73 -56
  192. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests.rs +365 -242
  193. package/sdk-core/crates/sdk-core/tests/main.rs +22 -16
  194. package/sdk-core/crates/sdk-core/tests/manual_tests.rs +233 -187
  195. package/sdk-core/crates/sdk-core/tests/runner.rs +4 -6
  196. package/sdk-core/crates/sdk-core/tests/shared_tests/mod.rs +73 -27
  197. package/sdk-core/crates/sdk-core/tests/shared_tests/priority.rs +107 -84
  198. package/sdk-core/crates/sdk-core/tests/workflows_procmacro.rs +6 -0
  199. package/sdk-core/crates/sdk-core/tests/workflows_trybuild/async_query_fail.rs +26 -0
  200. package/sdk-core/crates/sdk-core/tests/workflows_trybuild/async_query_fail.stderr +5 -0
  201. package/sdk-core/crates/sdk-core/tests/workflows_trybuild/basic_pass.rs +49 -0
  202. package/sdk-core/crates/sdk-core/tests/workflows_trybuild/minimal_pass.rs +21 -0
  203. package/sdk-core/crates/sdk-core/tests/workflows_trybuild/mut_query_fail.rs +26 -0
  204. package/sdk-core/crates/sdk-core/tests/workflows_trybuild/mut_query_fail.stderr +5 -0
  205. package/sdk-core/crates/sdk-core/tests/workflows_trybuild/sync_run_fail.rs +21 -0
  206. package/sdk-core/crates/sdk-core/tests/workflows_trybuild/sync_run_fail.stderr +5 -0
  207. package/sdk-core/crates/sdk-core-c-bridge/Cargo.toml +8 -1
  208. package/sdk-core/crates/sdk-core-c-bridge/include/temporal-sdk-core-c-bridge.h +37 -26
  209. package/sdk-core/crates/sdk-core-c-bridge/src/client.rs +180 -87
  210. package/sdk-core/crates/sdk-core-c-bridge/src/lib.rs +89 -5
  211. package/sdk-core/crates/sdk-core-c-bridge/src/metric.rs +10 -16
  212. package/sdk-core/crates/sdk-core-c-bridge/src/runtime.rs +59 -67
  213. package/sdk-core/crates/sdk-core-c-bridge/src/testing.rs +10 -10
  214. package/sdk-core/crates/sdk-core-c-bridge/src/tests/context.rs +57 -22
  215. package/sdk-core/crates/sdk-core-c-bridge/src/tests/mod.rs +108 -12
  216. package/sdk-core/crates/sdk-core-c-bridge/src/tests/utils.rs +9 -52
  217. package/sdk-core/crates/sdk-core-c-bridge/src/worker.rs +74 -91
  218. package/sdk-core/rustfmt.toml +2 -1
  219. package/src/client.rs +206 -289
  220. package/src/helpers/try_into_js.rs +88 -2
  221. package/src/metrics.rs +277 -35
  222. package/src/runtime.rs +94 -45
  223. package/src/testing.rs +9 -16
  224. package/src/worker.rs +86 -68
  225. package/ts/native.ts +39 -3
  226. package/sdk-core/crates/client/src/workflow_handle/mod.rs +0 -212
  227. package/sdk-core/crates/common/src/errors.rs +0 -85
  228. package/sdk-core/crates/common/tests/worker_task_types_test.rs +0 -129
  229. package/sdk-core/crates/macros/LICENSE.txt +0 -21
  230. package/sdk-core/crates/sdk/src/activity_context.rs +0 -238
  231. package/sdk-core/crates/sdk/src/app_data.rs +0 -37
  232. package/sdk-core/crates/sdk-core/tests/integ_tests/activity_functions.rs +0 -5
  233. package/sdk-core/crates/sdk-core/tests/integ_tests/workflow_tests/appdata_propagation.rs +0 -61
@@ -0,0 +1,417 @@
1
+ //! Functionality related to defining and interacting with activities
2
+ //!
3
+ //!
4
+ //! An example of defining an activity:
5
+ //! ```
6
+ //! use std::sync::{
7
+ //! Arc,
8
+ //! atomic::{AtomicUsize, Ordering},
9
+ //! };
10
+ //! use temporalio_macros::activities;
11
+ //! use temporalio_sdk::activities::{ActivityContext, ActivityError};
12
+ //!
13
+ //! struct MyActivities {
14
+ //! counter: AtomicUsize,
15
+ //! }
16
+ //!
17
+ //! #[activities]
18
+ //! impl MyActivities {
19
+ //! #[activity]
20
+ //! async fn echo(_ctx: ActivityContext, e: String) -> Result<String, ActivityError> {
21
+ //! Ok(e)
22
+ //! }
23
+ //!
24
+ //! #[activity]
25
+ //! async fn uses_self(self: Arc<Self>, _ctx: ActivityContext) -> Result<(), ActivityError> {
26
+ //! self.counter.fetch_add(1, Ordering::Relaxed);
27
+ //! Ok(())
28
+ //! }
29
+ //! }
30
+ //!
31
+ //! // If you need to refer to an activity that is defined externally, in a different codebase or
32
+ //! // possibly a differenet language, you can simply leave the function body unimplemented like so:
33
+ //!
34
+ //! struct ExternalActivities;
35
+ //! #[activities]
36
+ //! impl ExternalActivities {
37
+ //! #[activity(name = "foo")]
38
+ //! async fn foo(_ctx: ActivityContext, _: String) -> Result<String, ActivityError> {
39
+ //! unimplemented!()
40
+ //! }
41
+ //! }
42
+ //! ```
43
+ //!
44
+ //! This will allows you to call the activity from workflow code still, but the actual function
45
+ //! will never be invoked, since you won't have registered it with the worker.
46
+
47
+ #[doc(inline)]
48
+ pub use temporalio_macros::activities;
49
+
50
+ use futures_util::{FutureExt, future::BoxFuture};
51
+ use prost_types::{Duration, Timestamp};
52
+ use std::{
53
+ collections::HashMap,
54
+ fmt::Debug,
55
+ sync::Arc,
56
+ time::{Duration as StdDuration, SystemTime},
57
+ };
58
+ use temporalio_client::Priority;
59
+ use temporalio_common::{
60
+ ActivityDefinition,
61
+ data_converters::{
62
+ DataConverter, GenericPayloadConverter, SerializationContext, SerializationContextData,
63
+ },
64
+ protos::{
65
+ coresdk::{ActivityHeartbeat, activity_task},
66
+ temporal::api::common::v1::{Payload, RetryPolicy, WorkflowExecution},
67
+ utilities::TryIntoOrNone,
68
+ },
69
+ };
70
+ use temporalio_sdk_core::Worker as CoreWorker;
71
+ use tokio_util::sync::CancellationToken;
72
+
73
+ /// Used within activities to get info, heartbeat management etc.
74
+ #[derive(Clone)]
75
+ pub struct ActivityContext {
76
+ worker: Arc<CoreWorker>,
77
+ cancellation_token: CancellationToken,
78
+ heartbeat_details: Vec<Payload>,
79
+ header_fields: HashMap<String, Payload>,
80
+ info: ActivityInfo,
81
+ }
82
+
83
+ impl ActivityContext {
84
+ /// Construct new Activity Context, returning the context and all arguments to the activity.
85
+ pub fn new(
86
+ worker: Arc<CoreWorker>,
87
+ cancellation_token: CancellationToken,
88
+ task_queue: String,
89
+ task_token: Vec<u8>,
90
+ task: activity_task::Start,
91
+ ) -> (Self, Vec<Payload>) {
92
+ let activity_task::Start {
93
+ workflow_namespace,
94
+ workflow_type,
95
+ workflow_execution,
96
+ activity_id,
97
+ activity_type,
98
+ header_fields,
99
+ input,
100
+ heartbeat_details,
101
+ scheduled_time,
102
+ current_attempt_scheduled_time,
103
+ started_time,
104
+ attempt,
105
+ schedule_to_close_timeout,
106
+ start_to_close_timeout,
107
+ heartbeat_timeout,
108
+ retry_policy,
109
+ is_local,
110
+ priority,
111
+ } = task;
112
+ let deadline = calculate_deadline(
113
+ scheduled_time.as_ref(),
114
+ started_time.as_ref(),
115
+ start_to_close_timeout.as_ref(),
116
+ schedule_to_close_timeout.as_ref(),
117
+ );
118
+
119
+ (
120
+ ActivityContext {
121
+ worker,
122
+ cancellation_token,
123
+ heartbeat_details,
124
+ header_fields,
125
+ info: ActivityInfo {
126
+ task_token,
127
+ task_queue,
128
+ workflow_type,
129
+ workflow_namespace,
130
+ workflow_execution,
131
+ activity_id,
132
+ activity_type,
133
+ heartbeat_timeout: heartbeat_timeout.try_into_or_none(),
134
+ scheduled_time: scheduled_time.try_into_or_none(),
135
+ started_time: started_time.try_into_or_none(),
136
+ deadline,
137
+ attempt,
138
+ current_attempt_scheduled_time: current_attempt_scheduled_time
139
+ .try_into_or_none(),
140
+ retry_policy,
141
+ is_local,
142
+ priority: priority.map(Into::into).unwrap_or_default(),
143
+ },
144
+ },
145
+ input,
146
+ )
147
+ }
148
+
149
+ /// Returns a future the completes if and when the activity this was called inside has been
150
+ /// cancelled
151
+ pub async fn cancelled(&self) {
152
+ self.cancellation_token.clone().cancelled().await
153
+ }
154
+
155
+ /// Returns true if this activity has already been cancelled
156
+ pub fn is_cancelled(&self) -> bool {
157
+ self.cancellation_token.is_cancelled()
158
+ }
159
+
160
+ /// Extract heartbeat details from last failed attempt. This is used in combination with retry
161
+ /// policy.
162
+ pub fn heartbeat_details(&self) -> &[Payload] {
163
+ &self.heartbeat_details
164
+ }
165
+
166
+ /// RecordHeartbeat sends heartbeat for the currently executing activity
167
+ pub fn record_heartbeat(&self, details: Vec<Payload>) {
168
+ if !self.info.is_local {
169
+ self.worker.record_activity_heartbeat(ActivityHeartbeat {
170
+ task_token: self.info.task_token.clone(),
171
+ details,
172
+ })
173
+ }
174
+ }
175
+
176
+ /// Returns activity info of the executing activity
177
+ pub fn info(&self) -> &ActivityInfo {
178
+ &self.info
179
+ }
180
+
181
+ /// Get headers attached to this activity
182
+ pub fn headers(&self) -> &HashMap<String, Payload> {
183
+ &self.header_fields
184
+ }
185
+ }
186
+
187
+ /// Various information about a specific activity attempt.
188
+ #[derive(Clone, Debug)]
189
+ #[non_exhaustive]
190
+ pub struct ActivityInfo {
191
+ /// An opaque token representing a specific Activity task.
192
+ pub task_token: Vec<u8>,
193
+ /// The type of the workflow that invoked this activity.
194
+ pub workflow_type: String,
195
+ /// The namespace of the workflow that invoked this activity.
196
+ pub workflow_namespace: String,
197
+ /// The execution of the workflow that invoked this activity.
198
+ pub workflow_execution: Option<WorkflowExecution>,
199
+ /// The ID of this activity.
200
+ pub activity_id: String,
201
+ /// The type of this activity.
202
+ pub activity_type: String,
203
+ /// The task queue of this activity.
204
+ pub task_queue: String,
205
+ /// The interval within which this activity must heartbeat or be timed out.
206
+ pub heartbeat_timeout: Option<StdDuration>,
207
+ /// Time activity was scheduled by a workflow.
208
+ pub scheduled_time: Option<SystemTime>,
209
+ /// Time of activity start.
210
+ pub started_time: Option<SystemTime>,
211
+ /// Time of activity timeout.
212
+ pub deadline: Option<SystemTime>,
213
+ /// Attempt starts from 1, and increase by 1 for every retry, if retry policy is specified.
214
+ pub attempt: u32,
215
+ /// Time this attempt at the activity was scheduled.
216
+ pub current_attempt_scheduled_time: Option<SystemTime>,
217
+ /// The retry policy for this activity.
218
+ pub retry_policy: Option<RetryPolicy>,
219
+ /// Whether or not this is a local activity.
220
+ pub is_local: bool,
221
+ /// Priority of this activity. If unset uses [Priority::default].
222
+ pub priority: Priority,
223
+ }
224
+
225
+ /// Returned as errors from activity functions.
226
+ #[derive(Debug)]
227
+ pub enum ActivityError {
228
+ /// This error can be returned from activities to allow the explicit configuration of certain
229
+ /// error properties. It's also the default error type that arbitrary errors will be converted
230
+ /// into.
231
+ Retryable {
232
+ /// The underlying error
233
+ source: Box<dyn std::error::Error + Send + Sync + 'static>,
234
+ /// If specified, the next retry (if there is one) will occur after this delay
235
+ explicit_delay: Option<StdDuration>,
236
+ },
237
+ /// Return this error to indicate your activity is cancelling
238
+ Cancelled {
239
+ /// Some data to save as the cancellation reason
240
+ details: Option<Payload>,
241
+ },
242
+ /// Return this error to indicate that the activity should not be retried.
243
+ NonRetryable(Box<dyn std::error::Error + Send + Sync + 'static>),
244
+ /// Return this error to indicate that the activity will be completed outside of this activity
245
+ /// definition, by an external client.
246
+ WillCompleteAsync,
247
+ }
248
+
249
+ impl ActivityError {
250
+ /// Construct a cancelled error without details
251
+ pub fn cancelled() -> Self {
252
+ Self::Cancelled { details: None }
253
+ }
254
+ }
255
+
256
+ impl<E> From<E> for ActivityError
257
+ where
258
+ E: Into<anyhow::Error>,
259
+ {
260
+ fn from(source: E) -> Self {
261
+ Self::Retryable {
262
+ source: source.into().into_boxed_dyn_error(),
263
+ explicit_delay: None,
264
+ }
265
+ }
266
+ }
267
+
268
+ /// Deadline calculation. This is a port of
269
+ /// https://github.com/temporalio/sdk-go/blob/8651550973088f27f678118f997839fb1bb9e62f/internal/activity.go#L225
270
+ fn calculate_deadline(
271
+ scheduled_time: Option<&Timestamp>,
272
+ started_time: Option<&Timestamp>,
273
+ start_to_close_timeout: Option<&Duration>,
274
+ schedule_to_close_timeout: Option<&Duration>,
275
+ ) -> Option<SystemTime> {
276
+ match (
277
+ scheduled_time,
278
+ started_time,
279
+ start_to_close_timeout,
280
+ schedule_to_close_timeout,
281
+ ) {
282
+ (
283
+ Some(scheduled),
284
+ Some(started),
285
+ Some(start_to_close_timeout),
286
+ Some(schedule_to_close_timeout),
287
+ ) => {
288
+ let scheduled: SystemTime = maybe_convert_timestamp(scheduled)?;
289
+ let started: SystemTime = maybe_convert_timestamp(started)?;
290
+ let start_to_close_timeout: StdDuration = (*start_to_close_timeout).try_into().ok()?;
291
+ let schedule_to_close_timeout: StdDuration =
292
+ (*schedule_to_close_timeout).try_into().ok()?;
293
+
294
+ let start_to_close_deadline: SystemTime =
295
+ started.checked_add(start_to_close_timeout)?;
296
+ if schedule_to_close_timeout > StdDuration::ZERO {
297
+ let schedule_to_close_deadline =
298
+ scheduled.checked_add(schedule_to_close_timeout)?;
299
+ // Minimum of the two deadlines.
300
+ if schedule_to_close_deadline < start_to_close_deadline {
301
+ Some(schedule_to_close_deadline)
302
+ } else {
303
+ Some(start_to_close_deadline)
304
+ }
305
+ } else {
306
+ Some(start_to_close_deadline)
307
+ }
308
+ }
309
+ _ => None,
310
+ }
311
+ }
312
+
313
+ /// Helper function lifted from prost_types::Timestamp implementation to prevent double cloning in
314
+ /// error construction
315
+ fn maybe_convert_timestamp(timestamp: &Timestamp) -> Option<SystemTime> {
316
+ let mut timestamp = *timestamp;
317
+ timestamp.normalize();
318
+
319
+ let system_time = if timestamp.seconds >= 0 {
320
+ std::time::UNIX_EPOCH.checked_add(StdDuration::from_secs(timestamp.seconds as u64))
321
+ } else {
322
+ std::time::UNIX_EPOCH.checked_sub(StdDuration::from_secs((-timestamp.seconds) as u64))
323
+ };
324
+
325
+ system_time.and_then(|system_time| {
326
+ system_time.checked_add(StdDuration::from_nanos(timestamp.nanos as u64))
327
+ })
328
+ }
329
+
330
+ pub(crate) type ActivityInvocation = Arc<
331
+ dyn Fn(
332
+ Vec<Payload>,
333
+ DataConverter,
334
+ ActivityContext,
335
+ ) -> BoxFuture<'static, Result<Payload, ActivityError>>
336
+ + Send
337
+ + Sync,
338
+ >;
339
+
340
+ #[doc(hidden)]
341
+ pub trait ActivityImplementer {
342
+ fn register_all(self: Arc<Self>, defs: &mut ActivityDefinitions);
343
+ }
344
+
345
+ #[doc(hidden)]
346
+ pub trait ExecutableActivity: ActivityDefinition {
347
+ type Implementer: ActivityImplementer + Send + Sync + 'static;
348
+ fn execute(
349
+ receiver: Option<Arc<Self::Implementer>>,
350
+ ctx: ActivityContext,
351
+ input: Self::Input,
352
+ ) -> BoxFuture<'static, Result<Self::Output, ActivityError>>;
353
+ }
354
+
355
+ #[doc(hidden)]
356
+ pub trait HasOnlyStaticMethods {}
357
+
358
+ /// Contains activity registrations in a form ready for execution by workers.
359
+ #[derive(Default, Clone)]
360
+ pub struct ActivityDefinitions {
361
+ activities: HashMap<&'static str, ActivityInvocation>,
362
+ }
363
+
364
+ impl ActivityDefinitions {
365
+ /// Registers all activities on an activity implementer.
366
+ pub fn register_activities<AI: ActivityImplementer>(&mut self, instance: AI) -> &mut Self {
367
+ let arcd = Arc::new(instance);
368
+ AI::register_all(arcd, self);
369
+ self
370
+ }
371
+ /// Registers a specific activitiy.
372
+ pub fn register_activity<AD>(&mut self, instance: Arc<AD::Implementer>) -> &mut Self
373
+ where
374
+ AD: ActivityDefinition + ExecutableActivity,
375
+ AD::Output: Send + Sync,
376
+ {
377
+ self.activities.insert(
378
+ AD::name(),
379
+ Arc::new(move |payloads, dc, c| {
380
+ let instance = instance.clone();
381
+ let dc = dc.clone();
382
+ async move {
383
+ // Use PayloadConverter (not DataConverter) since the codec is applied
384
+ // at the SDK/Core boundary by the visitor, not here.
385
+ let pc = dc.payload_converter();
386
+ let ctx = SerializationContext {
387
+ data: &SerializationContextData::Activity,
388
+ converter: pc,
389
+ };
390
+ let deserialized: AD::Input = pc
391
+ .from_payloads(&ctx, payloads)
392
+ .map_err(ActivityError::from)?;
393
+ let result = AD::execute(Some(instance), c, deserialized).await?;
394
+ pc.to_payload(&ctx, &result).map_err(ActivityError::from)
395
+ }
396
+ .boxed()
397
+ }),
398
+ );
399
+ self
400
+ }
401
+
402
+ pub(crate) fn is_empty(&self) -> bool {
403
+ self.activities.is_empty()
404
+ }
405
+
406
+ pub(crate) fn get(&self, act_type: &str) -> Option<ActivityInvocation> {
407
+ self.activities.get(act_type).cloned()
408
+ }
409
+ }
410
+
411
+ impl Debug for ActivityDefinitions {
412
+ fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
413
+ f.debug_struct("ActivityDefinitions")
414
+ .field("activities", &self.activities.keys())
415
+ .finish()
416
+ }
417
+ }
@@ -102,7 +102,7 @@ pub struct ReturnWorkflowExitValueInterceptor {
102
102
 
103
103
  impl ReturnWorkflowExitValueInterceptor {
104
104
  /// Can be used to fetch the workflow result if/when it is determined
105
- pub fn get_result_handle(&self) -> Arc<OnceLock<Payload>> {
105
+ pub fn result_handle(&self) -> Arc<OnceLock<Payload>> {
106
106
  self.result_value.clone()
107
107
  }
108
108
  }