@temporalio/core-bridge 0.19.2 → 0.20.2

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 (125) hide show
  1. package/Cargo.lock +90 -157
  2. package/Cargo.toml +1 -0
  3. package/index.d.ts +11 -27
  4. package/package.json +3 -3
  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/.buildkite/docker/Dockerfile +1 -1
  11. package/sdk-core/.buildkite/docker/docker-compose.yaml +1 -1
  12. package/sdk-core/.cargo/config.toml +1 -0
  13. package/sdk-core/CODEOWNERS +1 -1
  14. package/sdk-core/bridge-ffi/include/sdk-core-bridge.h +119 -86
  15. package/sdk-core/bridge-ffi/src/lib.rs +311 -315
  16. package/sdk-core/bridge-ffi/src/wrappers.rs +108 -113
  17. package/sdk-core/client/Cargo.toml +13 -9
  18. package/sdk-core/client/LICENSE.txt +23 -0
  19. package/sdk-core/client/src/lib.rs +286 -174
  20. package/sdk-core/client/src/metrics.rs +86 -12
  21. package/sdk-core/client/src/raw.rs +566 -0
  22. package/sdk-core/client/src/retry.rs +137 -99
  23. package/sdk-core/core/Cargo.toml +15 -10
  24. package/sdk-core/core/LICENSE.txt +23 -0
  25. package/sdk-core/core/benches/workflow_replay.rs +79 -0
  26. package/sdk-core/core/src/abstractions.rs +38 -0
  27. package/sdk-core/core/src/core_tests/activity_tasks.rs +108 -182
  28. package/sdk-core/core/src/core_tests/child_workflows.rs +16 -11
  29. package/sdk-core/core/src/core_tests/determinism.rs +24 -12
  30. package/sdk-core/core/src/core_tests/local_activities.rs +53 -27
  31. package/sdk-core/core/src/core_tests/mod.rs +30 -43
  32. package/sdk-core/core/src/core_tests/queries.rs +82 -81
  33. package/sdk-core/core/src/core_tests/workers.rs +111 -296
  34. package/sdk-core/core/src/core_tests/workflow_cancels.rs +4 -4
  35. package/sdk-core/core/src/core_tests/workflow_tasks.rs +257 -242
  36. package/sdk-core/core/src/lib.rs +73 -318
  37. package/sdk-core/core/src/pollers/mod.rs +4 -6
  38. package/sdk-core/core/src/pollers/poll_buffer.rs +20 -14
  39. package/sdk-core/core/src/protosext/mod.rs +7 -10
  40. package/sdk-core/core/src/replay/mod.rs +11 -150
  41. package/sdk-core/core/src/telemetry/metrics.rs +35 -2
  42. package/sdk-core/core/src/telemetry/mod.rs +49 -16
  43. package/sdk-core/core/src/telemetry/prometheus_server.rs +14 -35
  44. package/sdk-core/core/src/test_help/mod.rs +104 -170
  45. package/sdk-core/core/src/worker/activities/activity_heartbeat_manager.rs +57 -34
  46. package/sdk-core/core/src/worker/activities/local_activities.rs +95 -23
  47. package/sdk-core/core/src/worker/activities.rs +23 -16
  48. package/sdk-core/core/src/worker/client/mocks.rs +86 -0
  49. package/sdk-core/core/src/worker/client.rs +209 -0
  50. package/sdk-core/core/src/worker/mod.rs +207 -108
  51. package/sdk-core/core/src/workflow/driven_workflow.rs +21 -6
  52. package/sdk-core/core/src/workflow/history_update.rs +107 -24
  53. package/sdk-core/core/src/workflow/machines/activity_state_machine.rs +2 -3
  54. package/sdk-core/core/src/workflow/machines/child_workflow_state_machine.rs +2 -3
  55. package/sdk-core/core/src/workflow/machines/mod.rs +20 -17
  56. package/sdk-core/core/src/workflow/machines/signal_external_state_machine.rs +56 -19
  57. package/sdk-core/core/src/workflow/machines/transition_coverage.rs +5 -0
  58. package/sdk-core/core/src/workflow/machines/upsert_search_attributes_state_machine.rs +230 -22
  59. package/sdk-core/core/src/workflow/machines/workflow_machines.rs +81 -115
  60. package/sdk-core/core/src/workflow/machines/workflow_task_state_machine.rs +4 -4
  61. package/sdk-core/core/src/workflow/mod.rs +13 -1
  62. package/sdk-core/core/src/workflow/workflow_tasks/concurrency_manager.rs +70 -11
  63. package/sdk-core/core/src/workflow/workflow_tasks/mod.rs +65 -41
  64. package/sdk-core/core-api/Cargo.toml +9 -1
  65. package/sdk-core/core-api/LICENSE.txt +23 -0
  66. package/sdk-core/core-api/src/errors.rs +7 -38
  67. package/sdk-core/core-api/src/lib.rs +44 -52
  68. package/sdk-core/core-api/src/worker.rs +10 -2
  69. package/sdk-core/etc/deps.svg +127 -96
  70. package/sdk-core/protos/api_upstream/temporal/api/command/v1/message.proto +11 -7
  71. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/failed_cause.proto +10 -0
  72. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/namespace.proto +6 -1
  73. package/sdk-core/protos/api_upstream/temporal/api/enums/v1/workflow.proto +6 -0
  74. package/sdk-core/protos/api_upstream/temporal/api/errordetails/v1/message.proto +6 -0
  75. package/sdk-core/protos/api_upstream/temporal/api/history/v1/message.proto +2 -1
  76. package/sdk-core/protos/api_upstream/temporal/api/replication/v1/message.proto +3 -0
  77. package/sdk-core/protos/api_upstream/temporal/api/workflow/v1/message.proto +12 -0
  78. package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/request_response.proto +25 -0
  79. package/sdk-core/protos/api_upstream/temporal/api/workflowservice/v1/service.proto +4 -0
  80. package/sdk-core/protos/local/temporal/sdk/core/bridge/bridge.proto +19 -35
  81. package/sdk-core/protos/local/temporal/sdk/core/core_interface.proto +2 -6
  82. package/sdk-core/protos/local/temporal/sdk/core/workflow_activation/workflow_activation.proto +53 -11
  83. package/sdk-core/protos/local/temporal/sdk/core/workflow_commands/workflow_commands.proto +14 -7
  84. package/sdk-core/protos/local/temporal/sdk/core/workflow_completion/workflow_completion.proto +3 -5
  85. package/sdk-core/sdk/Cargo.toml +16 -2
  86. package/sdk-core/sdk/LICENSE.txt +23 -0
  87. package/sdk-core/sdk/src/interceptors.rs +11 -0
  88. package/sdk-core/sdk/src/lib.rs +139 -151
  89. package/sdk-core/sdk/src/workflow_context/options.rs +86 -1
  90. package/sdk-core/sdk/src/workflow_context.rs +36 -17
  91. package/sdk-core/sdk/src/workflow_future.rs +19 -25
  92. package/sdk-core/sdk-core-protos/Cargo.toml +1 -1
  93. package/sdk-core/sdk-core-protos/build.rs +1 -0
  94. package/sdk-core/sdk-core-protos/src/history_info.rs +17 -4
  95. package/sdk-core/sdk-core-protos/src/lib.rs +251 -47
  96. package/sdk-core/test-utils/Cargo.toml +3 -1
  97. package/sdk-core/test-utils/src/canned_histories.rs +27 -0
  98. package/sdk-core/test-utils/src/histfetch.rs +3 -3
  99. package/sdk-core/test-utils/src/lib.rs +223 -68
  100. package/sdk-core/tests/integ_tests/client_tests.rs +27 -4
  101. package/sdk-core/tests/integ_tests/heartbeat_tests.rs +93 -14
  102. package/sdk-core/tests/integ_tests/polling_tests.rs +18 -12
  103. package/sdk-core/tests/integ_tests/queries_tests.rs +50 -53
  104. package/sdk-core/tests/integ_tests/workflow_tests/activities.rs +117 -103
  105. package/sdk-core/tests/integ_tests/workflow_tests/cancel_external.rs +8 -1
  106. package/sdk-core/tests/integ_tests/workflow_tests/cancel_wf.rs +10 -5
  107. package/sdk-core/tests/integ_tests/workflow_tests/child_workflows.rs +7 -1
  108. package/sdk-core/tests/integ_tests/workflow_tests/continue_as_new.rs +32 -9
  109. package/sdk-core/tests/integ_tests/workflow_tests/determinism.rs +7 -1
  110. package/sdk-core/tests/integ_tests/workflow_tests/local_activities.rs +76 -15
  111. package/sdk-core/tests/integ_tests/workflow_tests/patches.rs +19 -3
  112. package/sdk-core/tests/integ_tests/workflow_tests/replay.rs +39 -42
  113. package/sdk-core/tests/integ_tests/workflow_tests/resets.rs +84 -0
  114. package/sdk-core/tests/integ_tests/workflow_tests/signals.rs +30 -8
  115. package/sdk-core/tests/integ_tests/workflow_tests/stickyness.rs +21 -6
  116. package/sdk-core/tests/integ_tests/workflow_tests/timers.rs +26 -16
  117. package/sdk-core/tests/integ_tests/workflow_tests/upsert_search_attrs.rs +66 -0
  118. package/sdk-core/tests/integ_tests/workflow_tests.rs +78 -74
  119. package/sdk-core/tests/load_tests.rs +9 -6
  120. package/sdk-core/tests/main.rs +43 -10
  121. package/src/conversions.rs +7 -12
  122. package/src/lib.rs +322 -357
  123. package/sdk-core/client/src/mocks.rs +0 -167
  124. package/sdk-core/core/src/worker/dispatcher.rs +0 -171
  125. package/sdk-core/protos/local/temporal/sdk/core/bridge/service.proto +0 -61
@@ -14,8 +14,17 @@
14
14
 
15
15
  mod wrappers;
16
16
 
17
+ use bridge::{init_response, CreateWorkerRequest, InitResponse};
17
18
  use prost::Message;
18
- use temporal_sdk_core_protos::coresdk::bridge;
19
+ use std::sync::Arc;
20
+ use temporal_sdk_core::{
21
+ fetch_global_buffered_logs, telemetry_init, Client, ClientOptions, RetryClient,
22
+ };
23
+ use temporal_sdk_core_api::Worker;
24
+ use temporal_sdk_core_protos::coresdk::{
25
+ bridge,
26
+ bridge::{CreateClientRequest, InitTelemetryRequest},
27
+ };
19
28
 
20
29
  /// A set of bytes owned by Core. No fields within nor any bytes references must
21
30
  /// ever be mutated outside of Core. This must always be passed to
@@ -69,11 +78,10 @@ impl Drop for tmprl_bytes_t {
69
78
  }
70
79
  }
71
80
 
72
- /// Free a set of bytes. The first parameter can be null in cases where a
73
- /// tmprl_core_t instance isn't available. If the second parameter is null, this
74
- /// is a no-op.
81
+ /// Free a set of bytes. The first parameter can be null in cases where a [tmprl_worker_t] instance
82
+ /// isn't available. If the second parameter is null, this is a no-op.
75
83
  #[no_mangle]
76
- pub extern "C" fn tmprl_bytes_free(core: *mut tmprl_core_t, bytes: *const tmprl_bytes_t) {
84
+ pub extern "C" fn tmprl_bytes_free(worker: *mut tmprl_worker_t, bytes: *const tmprl_bytes_t) {
77
85
  // Bail if freeing is disabled
78
86
  unsafe {
79
87
  if bytes.is_null() || (*bytes).disable_free {
@@ -85,10 +93,10 @@ pub extern "C" fn tmprl_bytes_free(core: *mut tmprl_core_t, bytes: *const tmprl_
85
93
  let vec = unsafe { Vec::from_raw_parts((*bytes).bytes as *mut u8, (*bytes).len, (*bytes).cap) };
86
94
  // Set to null so the byte dropper doesn't try to free it
87
95
  unsafe { (*bytes).bytes = std::ptr::null_mut() };
88
- // Return only if core is non-null
89
- if !core.is_null() {
90
- let core = unsafe { &mut *core };
91
- core.return_buf(vec);
96
+ // Return only if worker is non-null
97
+ if !worker.is_null() {
98
+ let worker = unsafe { &mut *worker };
99
+ worker.return_buf(vec);
92
100
  }
93
101
  unsafe {
94
102
  Box::from_raw(bytes);
@@ -112,10 +120,6 @@ lazy_static::lazy_static! {
112
120
  tmprl_bytes_t::from_vec_disable_free(bridge::InitResponse::default().encode_to_vec())
113
121
  };
114
122
 
115
- static ref DEFAULT_SHUTDOWN_RESPONSE_BYTES: tmprl_bytes_t = {
116
- tmprl_bytes_t::from_vec_disable_free(bridge::ShutdownResponse::default().encode_to_vec())
117
- };
118
-
119
123
  static ref DEFAULT_REGISTER_WORKER_RESPONSE_BYTES: tmprl_bytes_t = {
120
124
  tmprl_bytes_t::from_vec_disable_free(bridge::RegisterWorkerResponse::default().encode_to_vec())
121
125
  };
@@ -141,12 +145,13 @@ lazy_static::lazy_static! {
141
145
  };
142
146
  }
143
147
 
144
- /// A runtime owned by Core. This must be passed to tmprl_runtime_free when no
145
- /// longer in use. This must not be freed until every call to every tmprl_core_t
146
- /// instance created with this runtime has been shutdown.
148
+ /// A runtime owned by Core. This must be passed to [tmprl_runtime_free] when no longer in use. This
149
+ /// should not be freed until every call to every [tmprl_worker_t] instance created with this
150
+ /// runtime has been shutdown. In practice, since the actual runtime is behind an [Arc], it's
151
+ /// currently OK, but that's an implementation detail.
147
152
  pub struct tmprl_runtime_t {
148
- // This is the same runtime shared with the core instance
149
- tokio_runtime: std::sync::Arc<tokio::runtime::Runtime>,
153
+ // This is the same runtime shared with worker instances
154
+ tokio_runtime: Arc<tokio::runtime::Runtime>,
150
155
  }
151
156
 
152
157
  /// Create a new runtime. The result is never null and must be freed via
@@ -155,7 +160,7 @@ pub struct tmprl_runtime_t {
155
160
  pub extern "C" fn tmprl_runtime_new() -> *mut tmprl_runtime_t {
156
161
  Box::into_raw(Box::new(tmprl_runtime_t {
157
162
  // TODO(cretz): Options to configure thread pool?
158
- tokio_runtime: std::sync::Arc::new(
163
+ tokio_runtime: Arc::new(
159
164
  tokio::runtime::Builder::new_multi_thread()
160
165
  .enable_all()
161
166
  .build()
@@ -174,52 +179,54 @@ pub extern "C" fn tmprl_runtime_free(runtime: *mut tmprl_runtime_t) {
174
179
  }
175
180
  }
176
181
 
177
- /// A core instance owned by Core. This must be passed to tmprl_core_shutdown
182
+ /// A worker instance owned by Core. This must be passed to [tmprl_worker_shutdown]
178
183
  /// when no longer in use which will free the resources.
179
- pub struct tmprl_core_t {
180
- tokio_runtime: std::sync::Arc<tokio::runtime::Runtime>,
184
+ pub struct tmprl_worker_t {
185
+ tokio_runtime: Arc<tokio::runtime::Runtime>,
181
186
  // We are not concerned with the overhead of dynamic dispatch at this time
182
- core: std::sync::Arc<dyn temporal_sdk_core_api::Core>,
187
+ worker: Arc<dyn Worker>,
183
188
  }
184
189
 
185
- /// Callback called by tmprl_core_init on completion. The first parameter of the
190
+ /// Callback called by [tmprl_worker_init] on completion. The first parameter of the
186
191
  /// callback is user data passed into the original function. The second
187
- /// parameter is a core instance if the call is successful or null if not. If
188
- /// present, the core instance must be freed via tmprl_core_shutdown when no
192
+ /// parameter is a worker instance if the call is successful or null if not. If
193
+ /// present, the worker instance must be freed via [tmprl_worker_shutdown] when no
189
194
  /// longer in use. The third parameter of the callback is a byte array for a
190
- /// InitResponse protobuf message which must be freed via tmprl_bytes_free.
191
- type tmprl_core_init_callback = unsafe extern "C" fn(
195
+ /// [InitResponse] protobuf message which must be freed via [tmprl_bytes_free].
196
+ type tmprl_worker_init_callback = unsafe extern "C" fn(
192
197
  user_data: *mut libc::c_void,
193
- core: *mut tmprl_core_t,
198
+ worker: *mut tmprl_worker_t,
194
199
  resp: *const tmprl_bytes_t,
195
200
  );
196
201
 
197
202
  /// Callback called on function completion. The first parameter of the callback
198
203
  /// is user data passed into the original function. The second parameter of the
199
204
  /// callback is a never-null byte array for a response protobuf message which
200
- /// must be freed via tmprl_bytes_free.
205
+ /// must be freed via [tmprl_bytes_free].
201
206
  type tmprl_callback =
202
207
  unsafe extern "C" fn(user_data: *mut libc::c_void, core: *const tmprl_bytes_t);
203
208
 
204
- /// Create a new core instance.
209
+ /// Create a new worker instance.
205
210
  ///
206
- /// The runtime is required and must outlive this instance. The req_proto and
207
- /// req_proto_len represent a byte array for a InitRequest protobuf message. The
208
- /// callback is invoked on completion.
211
+ /// `runtime` and `client` are both required and must outlive this instance.
212
+ /// `req_proto` and `req_proto_len` represent a byte array for a [CreateWorkerRequest] protobuf
213
+ /// message.
214
+ /// The callback is invoked on completion.
209
215
  #[no_mangle]
210
- pub extern "C" fn tmprl_core_init(
216
+ pub extern "C" fn tmprl_worker_init(
211
217
  runtime: *mut tmprl_runtime_t,
218
+ client: *mut tmprl_client_t,
212
219
  req_proto: *const u8,
213
220
  req_proto_len: libc::size_t,
214
221
  user_data: *mut libc::c_void,
215
- callback: tmprl_core_init_callback,
222
+ callback: tmprl_worker_init_callback,
216
223
  ) {
217
- let runtime = unsafe { &*runtime };
218
- let req = match tmprl_core_t::decode_proto::<bridge::InitRequest>(req_proto, req_proto_len) {
224
+ let (runtime, client) = unsafe { (&*runtime, &*client) };
225
+ let req = match tmprl_worker_t::decode_proto::<CreateWorkerRequest>(req_proto, req_proto_len) {
219
226
  Ok(req) => req,
220
227
  Err(message) => {
221
- let resp = bridge::InitResponse {
222
- error: Some(bridge::init_response::Error { message }),
228
+ let resp = InitResponse {
229
+ error: Some(init_response::Error { message }),
223
230
  };
224
231
  unsafe {
225
232
  callback(
@@ -232,249 +239,273 @@ pub extern "C" fn tmprl_core_init(
232
239
  }
233
240
  };
234
241
  let user_data = UserDataHandle(user_data);
235
- runtime.tokio_runtime.spawn(async move {
236
- match tmprl_core_t::new(
237
- runtime.tokio_runtime.clone(),
238
- wrappers::CoreInitOptions(req),
239
- )
240
- .await
241
- {
242
- Ok(core) => unsafe {
242
+ match tmprl_worker_t::new(
243
+ runtime.tokio_runtime.clone(),
244
+ client.client.clone(),
245
+ wrappers::WorkerConfig(req),
246
+ ) {
247
+ Ok(worker) => unsafe {
248
+ callback(
249
+ user_data.into(),
250
+ Box::into_raw(Box::new(worker)),
251
+ &*DEFAULT_INIT_RESPONSE_BYTES,
252
+ );
253
+ },
254
+ Err(message) => {
255
+ let resp = InitResponse {
256
+ error: Some(init_response::Error { message }),
257
+ };
258
+ unsafe {
243
259
  callback(
244
260
  user_data.into(),
245
- Box::into_raw(Box::new(core)),
246
- &*DEFAULT_INIT_RESPONSE_BYTES,
261
+ std::ptr::null_mut(),
262
+ tmprl_bytes_t::from_vec(resp.encode_to_vec()).into_raw(),
247
263
  );
248
- },
249
- Err(message) => {
250
- let resp = bridge::InitResponse {
251
- error: Some(bridge::init_response::Error { message }),
252
- };
253
- unsafe {
254
- callback(
255
- user_data.into(),
256
- std::ptr::null_mut(),
257
- tmprl_bytes_t::from_vec(resp.encode_to_vec()).into_raw(),
258
- );
259
- }
260
264
  }
261
265
  }
262
- });
266
+ };
263
267
  }
264
268
 
265
- /// Shutdown and free a core instance.
269
+ /// Shutdown and free a previously created worker.
270
+ ///
271
+ /// The req_proto and req_proto_len represent a byte array for a [bridge::ShutdownWorkerRequest]
272
+ /// protobuf message, which currently contains nothing and are unused, but the parameters are kept
273
+ /// for now.
274
+ ///
275
+ /// The callback is invoked on completion with a ShutdownWorkerResponse protobuf message.
266
276
  ///
267
- /// The req_proto and req_proto_len represent a byte array for a ShutdownRequest
268
- /// protobuf message. The callback is invoked on completion with a
269
- /// ShutdownResponse protobuf message.
277
+ /// After the callback has been called, the worker struct will be freed and the pointer will no
278
+ /// longer be valid.
270
279
  #[no_mangle]
271
- pub extern "C" fn tmprl_core_shutdown(
272
- core: *mut tmprl_core_t,
280
+ pub extern "C" fn tmprl_worker_shutdown(
281
+ worker: *mut tmprl_worker_t,
273
282
  #[allow(unused_variables)] // We intentionally ignore the request
274
283
  req_proto: *const u8,
275
284
  #[allow(unused_variables)] req_proto_len: libc::size_t,
276
285
  user_data: *mut libc::c_void,
277
286
  callback: tmprl_callback,
278
287
  ) {
279
- // Re-own the object so it can be dropped
280
- let core = unsafe { Box::from_raw(core) };
288
+ let worker = unsafe { Box::from_raw(worker) };
281
289
  let user_data = UserDataHandle(user_data);
282
- core.tokio_runtime.clone().spawn(async move {
283
- core.shutdown().await;
290
+ worker.tokio_runtime.clone().spawn(async move {
291
+ worker.shutdown().await;
284
292
  unsafe {
285
- callback(user_data.into(), &*DEFAULT_SHUTDOWN_RESPONSE_BYTES);
293
+ callback(user_data.into(), &*DEFAULT_SHUTDOWN_WORKER_RESPONSE_BYTES);
286
294
  }
295
+ drop(worker);
287
296
  });
288
297
  }
289
298
 
290
- /// Register a worker.
299
+ /// Initialize process-wide telemetry. Should only be called once, subsequent calls will be ignored
300
+ /// by core.
301
+ ///
302
+ /// Unlike the other functions in this bridge, this blocks until initting is complete, as telemetry
303
+ /// should typically be initialized before doing other work.
291
304
  ///
292
- /// The req_proto and req_proto_len represent a byte array for a RegisterWorker
293
- /// protobuf message. The callback is invoked on completion with a
294
- /// RegisterWorkerResponse protobuf message.
305
+ /// Returns a byte array for a [InitResponse] protobuf message which must be freed via
306
+ /// tmprl_bytes_free.
295
307
  #[no_mangle]
296
- pub extern "C" fn tmprl_register_worker(
297
- core: *mut tmprl_core_t,
308
+ pub extern "C" fn tmprl_telemetry_init(
298
309
  req_proto: *const u8,
299
310
  req_proto_len: libc::size_t,
300
- user_data: *mut libc::c_void,
301
- callback: tmprl_callback,
302
- ) {
303
- let core = unsafe { &mut *core };
304
- let req =
305
- match tmprl_core_t::decode_proto::<bridge::RegisterWorkerRequest>(req_proto, req_proto_len)
306
- {
307
- Ok(req) => req,
308
- Err(message) => {
309
- let resp = bridge::RegisterWorkerResponse {
310
- error: Some(bridge::register_worker_response::Error {
311
- message,
312
- worker_already_registered: false,
313
- }),
314
- };
315
- unsafe {
316
- callback(user_data, core.encode_proto(&resp).into_raw());
317
- }
318
- return;
319
- }
320
- };
321
- let user_data = UserDataHandle(user_data);
322
- match core.register_worker(wrappers::WorkerConfig(req)) {
323
- Ok(()) => unsafe {
324
- callback(user_data.into(), &*DEFAULT_REGISTER_WORKER_RESPONSE_BYTES);
325
- },
326
- Err(err) => {
327
- let resp = bridge::RegisterWorkerResponse { error: Some(err) };
328
- unsafe { callback(user_data.into(), core.encode_proto(&resp).into_raw()) };
311
+ ) -> *const tmprl_bytes_t {
312
+ let req = match tmprl_worker_t::decode_proto::<InitTelemetryRequest>(req_proto, req_proto_len) {
313
+ Ok(req) => req,
314
+ Err(message) => {
315
+ let resp = InitResponse {
316
+ error: Some(init_response::Error { message }),
317
+ };
318
+ return tmprl_bytes_t::from_vec(resp.encode_to_vec()).into_raw();
319
+ }
320
+ };
321
+
322
+ match wrappers::InitTelemetryRequest(req)
323
+ .try_into()
324
+ .map(|opts| telemetry_init(&opts))
325
+ {
326
+ Ok(_) => &*DEFAULT_INIT_RESPONSE_BYTES,
327
+ Err(message) => {
328
+ let resp = InitResponse {
329
+ error: Some(init_response::Error { message }),
330
+ };
331
+ tmprl_bytes_t::from_vec(resp.encode_to_vec()).into_raw()
329
332
  }
330
333
  }
331
334
  }
332
335
 
333
- /// Shutdown registered worker.
336
+ /// A client instance owned by Core. This must be passed to [tmprl_client_free]
337
+ /// when no longer in use which will free the resources.
338
+ pub struct tmprl_client_t {
339
+ client: Arc<RetryClient<Client>>,
340
+ }
341
+
342
+ impl tmprl_client_t {
343
+ pub fn new(client: Arc<RetryClient<Client>>) -> Self {
344
+ Self { client }
345
+ }
346
+ }
347
+
348
+ /// Callback called by [tmprl_client_init] on completion. The first parameter of the
349
+ /// callback is user data passed into the original function. The second
350
+ /// parameter is a client instance if the call is successful or null if not. If
351
+ /// present, the client instance must be freed via [tmprl_client_free] when no
352
+ /// longer in use. The third parameter of the callback is a byte array for a
353
+ /// [InitResponse] protobuf message which must be freed via [tmprl_bytes_free].
354
+ type tmprl_client_init_callback = unsafe extern "C" fn(
355
+ user_data: *mut libc::c_void,
356
+ client: *mut tmprl_client_t,
357
+ resp: *const tmprl_bytes_t,
358
+ );
359
+
360
+ /// Initialize a client connection to the Temporal service.
334
361
  ///
335
- /// The req_proto and req_proto_len represent a byte array for a
336
- /// ShutdownWorkerRequest protobuf message. The callback is invoked on
337
- /// completion with a ShutdownWorkerResponse protobuf message.
362
+ /// The runtime is required and must outlive this instance. The `req_proto` and `req_proto_len`
363
+ /// represent a byte array for a [CreateClientRequest] protobuf message. The callback is invoked on
364
+ /// completion.
338
365
  #[no_mangle]
339
- pub extern "C" fn tmprl_shutdown_worker(
340
- core: *mut tmprl_core_t,
341
- #[allow(unused_variables)] // We intentionally ignore the request
366
+ pub extern "C" fn tmprl_client_init(
367
+ runtime: *mut tmprl_runtime_t,
342
368
  req_proto: *const u8,
343
- #[allow(unused_variables)] req_proto_len: libc::size_t,
369
+ req_proto_len: libc::size_t,
344
370
  user_data: *mut libc::c_void,
345
- callback: tmprl_callback,
371
+ callback: tmprl_client_init_callback,
346
372
  ) {
347
- let core = unsafe { &mut *core };
348
- let req =
349
- match tmprl_core_t::decode_proto::<bridge::ShutdownWorkerRequest>(req_proto, req_proto_len)
350
- {
373
+ let runtime = unsafe { &*runtime };
374
+ let (namespace, req) =
375
+ match tmprl_worker_t::decode_proto::<CreateClientRequest>(req_proto, req_proto_len)
376
+ .and_then(|cgr| {
377
+ let ns = cgr.namespace.clone();
378
+ wrappers::ClientOptions(cgr)
379
+ .try_into()
380
+ .map(|sgo: ClientOptions| (ns, sgo))
381
+ }) {
351
382
  Ok(req) => req,
352
383
  Err(message) => {
353
- let resp = bridge::ShutdownWorkerResponse {
354
- error: Some(bridge::shutdown_worker_response::Error { message }),
384
+ let resp = InitResponse {
385
+ error: Some(init_response::Error { message }),
355
386
  };
356
387
  unsafe {
357
- callback(user_data, core.encode_proto(&resp).into_raw());
388
+ callback(
389
+ user_data,
390
+ std::ptr::null_mut(),
391
+ tmprl_bytes_t::from_vec(resp.encode_to_vec()).into_raw(),
392
+ );
358
393
  }
359
394
  return;
360
395
  }
361
396
  };
397
+
362
398
  let user_data = UserDataHandle(user_data);
363
- core.tokio_runtime.clone().spawn(async move {
364
- core.shutdown_worker(req).await;
365
- unsafe {
366
- callback(user_data.into(), &*DEFAULT_SHUTDOWN_WORKER_RESPONSE_BYTES);
399
+ runtime.tokio_runtime.spawn(async move {
400
+ match req.connect(namespace, None).await {
401
+ Ok(client) => unsafe {
402
+ callback(
403
+ user_data.into(),
404
+ Box::into_raw(Box::new(tmprl_client_t::new(Arc::new(client)))),
405
+ &*DEFAULT_INIT_RESPONSE_BYTES,
406
+ );
407
+ },
408
+ Err(e) => {
409
+ let resp = InitResponse {
410
+ error: Some(init_response::Error {
411
+ message: e.to_string(),
412
+ }),
413
+ };
414
+ unsafe {
415
+ callback(
416
+ user_data.into(),
417
+ std::ptr::null_mut(),
418
+ tmprl_bytes_t::from_vec(resp.encode_to_vec()).into_raw(),
419
+ );
420
+ }
421
+ }
367
422
  }
368
423
  });
369
424
  }
370
425
 
371
- /// Poll workflow activation.
426
+ /// Free a previously created client
427
+ #[no_mangle]
428
+ pub extern "C" fn tmprl_client_free(client: *mut tmprl_client_t) {
429
+ unsafe { drop(Box::from_raw(client)) };
430
+ }
431
+
432
+ /// Poll for a workflow activation.
433
+ ///
434
+ /// The `req_proto` and `req_proto_len` represent a byte array for a
435
+ /// [bridge::PollWorkflowActivationRequest] protobuf message, which currently contains nothing and
436
+ /// is unused, but the parameters are kept for now.
372
437
  ///
373
- /// The req_proto and req_proto_len represent a byte array for a
374
- /// PollWorkflowActivationRequest protobuf message. The callback is invoked on
375
- /// completion with a PollWorkflowActivationResponse protobuf message.
438
+ /// The callback is invoked on completion with a [bridge::PollWorkflowActivationResponse] protobuf
439
+ /// message.
376
440
  #[no_mangle]
377
441
  pub extern "C" fn tmprl_poll_workflow_activation(
378
- core: *mut tmprl_core_t,
442
+ worker: *mut tmprl_worker_t,
443
+ #[allow(unused_variables)] // We intentionally ignore the request
379
444
  req_proto: *const u8,
445
+ #[allow(unused_variables)] // We intentionally ignore the request
380
446
  req_proto_len: libc::size_t,
381
447
  user_data: *mut libc::c_void,
382
448
  callback: tmprl_callback,
383
449
  ) {
384
- let core = unsafe { &mut *core };
385
- let req = match tmprl_core_t::decode_proto::<bridge::PollWorkflowActivationRequest>(
386
- req_proto,
387
- req_proto_len,
388
- ) {
389
- Ok(req) => req,
390
- Err(message) => {
391
- let resp = bridge::PollWorkflowActivationResponse {
392
- response: Some(bridge::poll_workflow_activation_response::Response::Error(
393
- bridge::poll_workflow_activation_response::Error {
394
- message,
395
- shutdown: false,
396
- },
397
- )),
398
- };
399
- unsafe {
400
- callback(user_data, core.encode_proto(&resp).into_raw());
401
- }
402
- return;
403
- }
404
- };
450
+ let worker = unsafe { &mut *worker };
405
451
  let user_data = UserDataHandle(user_data);
406
- core.tokio_runtime.clone().spawn(async move {
452
+ worker.tokio_runtime.clone().spawn(async move {
407
453
  let resp = bridge::PollWorkflowActivationResponse {
408
- response: Some(match core.poll_workflow_activation(req).await {
454
+ response: Some(match worker.poll_workflow_activation().await {
409
455
  Ok(act) => bridge::poll_workflow_activation_response::Response::Activation(act),
410
456
  Err(err) => bridge::poll_workflow_activation_response::Response::Error(err),
411
457
  }),
412
458
  };
413
- unsafe { callback(user_data.into(), core.encode_proto(&resp).into_raw()) };
459
+ unsafe { callback(user_data.into(), worker.encode_proto(&resp).into_raw()) };
414
460
  });
415
461
  }
416
462
 
417
- /// Poll activity task.
463
+ /// Poll for an activity task.
418
464
  ///
419
- /// The req_proto and req_proto_len represent a byte array for a
420
- /// PollActivityTaskRequest protobuf message. The callback is invoked on
421
- /// completion with a PollActivityTaskResponse protobuf message.
465
+ /// The `req_proto` and `req_proto_len` represent a byte array for a
466
+ /// [bridge::PollActivityTaskRequest] protobuf message, which currently contains nothing and is
467
+ /// unused, but the parameters are kept for now.
468
+ ///
469
+ /// The callback is invoked on completion with a [bridge::PollActivityTaskResponse] protobuf
470
+ /// message.
422
471
  #[no_mangle]
423
472
  pub extern "C" fn tmprl_poll_activity_task(
424
- core: *mut tmprl_core_t,
473
+ worker: *mut tmprl_worker_t,
474
+ #[allow(unused_variables)] // We intentionally ignore the request
425
475
  req_proto: *const u8,
476
+ #[allow(unused_variables)] // We intentionally ignore the request
426
477
  req_proto_len: libc::size_t,
427
478
  user_data: *mut libc::c_void,
428
479
  callback: tmprl_callback,
429
480
  ) {
430
- let core = unsafe { &mut *core };
431
- let req = match tmprl_core_t::decode_proto::<bridge::PollActivityTaskRequest>(
432
- req_proto,
433
- req_proto_len,
434
- ) {
435
- Ok(req) => req,
436
- Err(message) => {
437
- let resp = bridge::PollActivityTaskResponse {
438
- response: Some(bridge::poll_activity_task_response::Response::Error(
439
- bridge::poll_activity_task_response::Error {
440
- message,
441
- shutdown: false,
442
- },
443
- )),
444
- };
445
- unsafe {
446
- callback(user_data, core.encode_proto(&resp).into_raw());
447
- }
448
- return;
449
- }
450
- };
481
+ let worker = unsafe { &mut *worker };
451
482
  let user_data = UserDataHandle(user_data);
452
- core.tokio_runtime.clone().spawn(async move {
483
+ worker.tokio_runtime.clone().spawn(async move {
453
484
  let resp = bridge::PollActivityTaskResponse {
454
- response: Some(match core.poll_activity_task(req).await {
485
+ response: Some(match worker.poll_activity_task().await {
455
486
  Ok(task) => bridge::poll_activity_task_response::Response::Task(task),
456
487
  Err(err) => bridge::poll_activity_task_response::Response::Error(err),
457
488
  }),
458
489
  };
459
- unsafe { callback(user_data.into(), core.encode_proto(&resp).into_raw()) };
490
+ unsafe { callback(user_data.into(), worker.encode_proto(&resp).into_raw()) };
460
491
  });
461
492
  }
462
493
 
463
- /// Complete workflow activation.
494
+ /// Complete a workflow activation.
464
495
  ///
465
- /// The req_proto and req_proto_len represent a byte array for a
466
- /// CompleteWorkflowActivationRequest protobuf message. The callback is invoked
467
- /// on completion with a CompleteWorkflowActivationResponse protobuf message.
496
+ /// The `req_proto` and `req_proto_len` represent a byte array for a
497
+ /// [bridge::CompleteWorkflowActivationRequest] protobuf message. The callback is invoked on
498
+ /// completion with a [bridge::CompleteWorkflowActivationResponse] protobuf message.
468
499
  #[no_mangle]
469
500
  pub extern "C" fn tmprl_complete_workflow_activation(
470
- core: *mut tmprl_core_t,
501
+ worker: *mut tmprl_worker_t,
471
502
  req_proto: *const u8,
472
503
  req_proto_len: libc::size_t,
473
504
  user_data: *mut libc::c_void,
474
505
  callback: tmprl_callback,
475
506
  ) {
476
- let core = unsafe { &mut *core };
477
- let req = match tmprl_core_t::decode_proto::<bridge::CompleteWorkflowActivationRequest>(
507
+ let worker = unsafe { &mut *worker };
508
+ let req = match tmprl_worker_t::decode_proto::<bridge::CompleteWorkflowActivationRequest>(
478
509
  req_proto,
479
510
  req_proto_len,
480
511
  ) {
@@ -484,14 +515,14 @@ pub extern "C" fn tmprl_complete_workflow_activation(
484
515
  error: Some(bridge::complete_workflow_activation_response::Error { message }),
485
516
  };
486
517
  unsafe {
487
- callback(user_data, core.encode_proto(&resp).into_raw());
518
+ callback(user_data, worker.encode_proto(&resp).into_raw());
488
519
  }
489
520
  return;
490
521
  }
491
522
  };
492
523
  let user_data = UserDataHandle(user_data);
493
- core.tokio_runtime.clone().spawn(async move {
494
- match core.complete_workflow_activation(req).await {
524
+ worker.tokio_runtime.clone().spawn(async move {
525
+ match worker.complete_workflow_activation(req).await {
495
526
  Ok(()) => unsafe {
496
527
  callback(
497
528
  user_data.into(),
@@ -500,27 +531,27 @@ pub extern "C" fn tmprl_complete_workflow_activation(
500
531
  },
501
532
  Err(err) => {
502
533
  let resp = bridge::CompleteWorkflowActivationResponse { error: Some(err) };
503
- unsafe { callback(user_data.into(), core.encode_proto(&resp).into_raw()) };
534
+ unsafe { callback(user_data.into(), worker.encode_proto(&resp).into_raw()) };
504
535
  }
505
536
  }
506
537
  });
507
538
  }
508
539
 
509
- /// Complete activity task.
540
+ /// Complete an activity task.
510
541
  ///
511
- /// The req_proto and req_proto_len represent a byte array for a
512
- /// CompleteActivityTaskRequest protobuf message. The callback is invoked
513
- /// on completion with a CompleteActivityTaskResponse protobuf message.
542
+ /// The `req_proto` and `req_proto_len` represent a byte array for a
543
+ /// [bridge::CompleteActivityTaskRequest] protobuf message. The callback is invoked on completion
544
+ /// with a [bridge::CompleteActivityTaskResponse] protobuf message.
514
545
  #[no_mangle]
515
546
  pub extern "C" fn tmprl_complete_activity_task(
516
- core: *mut tmprl_core_t,
547
+ worker: *mut tmprl_worker_t,
517
548
  req_proto: *const u8,
518
549
  req_proto_len: libc::size_t,
519
550
  user_data: *mut libc::c_void,
520
551
  callback: tmprl_callback,
521
552
  ) {
522
- let core = unsafe { &mut *core };
523
- let req = match tmprl_core_t::decode_proto::<bridge::CompleteActivityTaskRequest>(
553
+ let worker = unsafe { &mut *worker };
554
+ let req = match tmprl_worker_t::decode_proto::<bridge::CompleteActivityTaskRequest>(
524
555
  req_proto,
525
556
  req_proto_len,
526
557
  ) {
@@ -530,14 +561,14 @@ pub extern "C" fn tmprl_complete_activity_task(
530
561
  error: Some(bridge::complete_activity_task_response::Error { message }),
531
562
  };
532
563
  unsafe {
533
- callback(user_data, core.encode_proto(&resp).into_raw());
564
+ callback(user_data, worker.encode_proto(&resp).into_raw());
534
565
  }
535
566
  return;
536
567
  }
537
568
  };
538
569
  let user_data = UserDataHandle(user_data);
539
- core.tokio_runtime.clone().spawn(async move {
540
- match core.complete_activity_task(req).await {
570
+ worker.tokio_runtime.clone().spawn(async move {
571
+ match worker.complete_activity_task(req).await {
541
572
  Ok(()) => unsafe {
542
573
  callback(
543
574
  user_data.into(),
@@ -546,27 +577,27 @@ pub extern "C" fn tmprl_complete_activity_task(
546
577
  },
547
578
  Err(err) => {
548
579
  let resp = bridge::CompleteActivityTaskResponse { error: Some(err) };
549
- unsafe { callback(user_data.into(), core.encode_proto(&resp).into_raw()) };
580
+ unsafe { callback(user_data.into(), worker.encode_proto(&resp).into_raw()) };
550
581
  }
551
582
  }
552
583
  });
553
584
  }
554
585
 
555
- /// Record activity heartbeat.
586
+ /// Record an activity heartbeat.
556
587
  ///
557
- /// The req_proto and req_proto_len represent a byte array for a
558
- /// RecordActivityHeartbeatRequest protobuf message. The callback is invoked
559
- /// on completion with a RecordActivityHeartbeatResponse protobuf message.
588
+ /// `req_proto` and `req_proto_len` represent a byte array for a
589
+ /// [bridge::RecordActivityHeartbeatRequest] protobuf message. The callback is invoked on completion
590
+ /// with a RecordActivityHeartbeatResponse protobuf message.
560
591
  #[no_mangle]
561
592
  pub extern "C" fn tmprl_record_activity_heartbeat(
562
- core: *mut tmprl_core_t,
593
+ worker: *mut tmprl_worker_t,
563
594
  req_proto: *const u8,
564
595
  req_proto_len: libc::size_t,
565
596
  user_data: *mut libc::c_void,
566
597
  callback: tmprl_callback,
567
598
  ) {
568
- let core = unsafe { &mut *core };
569
- let req = match tmprl_core_t::decode_proto::<bridge::RecordActivityHeartbeatRequest>(
599
+ let worker = unsafe { &mut *worker };
600
+ let req = match tmprl_worker_t::decode_proto::<bridge::RecordActivityHeartbeatRequest>(
570
601
  req_proto,
571
602
  req_proto_len,
572
603
  ) {
@@ -576,7 +607,7 @@ pub extern "C" fn tmprl_record_activity_heartbeat(
576
607
  error: Some(bridge::record_activity_heartbeat_response::Error { message }),
577
608
  };
578
609
  unsafe {
579
- callback(user_data, core.encode_proto(&resp).into_raw());
610
+ callback(user_data, worker.encode_proto(&resp).into_raw());
580
611
  }
581
612
  return;
582
613
  }
@@ -584,8 +615,8 @@ pub extern "C" fn tmprl_record_activity_heartbeat(
584
615
  let user_data = UserDataHandle(user_data);
585
616
  // We intentionally spawn even though the core call is not async so the
586
617
  // callback can be made in the tokio runtime
587
- core.tokio_runtime.clone().spawn(async move {
588
- core.record_activity_heartbeat(req);
618
+ worker.tokio_runtime.clone().spawn(async move {
619
+ worker.record_activity_heartbeat(req);
589
620
  unsafe {
590
621
  callback(
591
622
  user_data.into(),
@@ -595,21 +626,21 @@ pub extern "C" fn tmprl_record_activity_heartbeat(
595
626
  });
596
627
  }
597
628
 
598
- /// Request workflow eviction.
629
+ /// Request a workflow eviction.
599
630
  ///
600
- /// The req_proto and req_proto_len represent a byte array for a
601
- /// RequestWorkflowEvictionRequest protobuf message. The callback is invoked
602
- /// on completion with a RequestWorkflowEvictionResponse protobuf message.
631
+ /// The `req_proto` and `req_proto_len` represent a byte array for a
632
+ /// [bridge::RequestWorkflowEvictionRequest] protobuf message. The callback is invoked on completion
633
+ /// with a [bridge::RequestWorkflowEvictionResponse] protobuf message.
603
634
  #[no_mangle]
604
635
  pub extern "C" fn tmprl_request_workflow_eviction(
605
- core: *mut tmprl_core_t,
636
+ worker: *mut tmprl_worker_t,
606
637
  req_proto: *const u8,
607
638
  req_proto_len: libc::size_t,
608
639
  user_data: *mut libc::c_void,
609
640
  callback: tmprl_callback,
610
641
  ) {
611
- let core = unsafe { &mut *core };
612
- let req = match tmprl_core_t::decode_proto::<bridge::RequestWorkflowEvictionRequest>(
642
+ let worker = unsafe { &mut *worker };
643
+ let req = match tmprl_worker_t::decode_proto::<bridge::RequestWorkflowEvictionRequest>(
613
644
  req_proto,
614
645
  req_proto_len,
615
646
  ) {
@@ -619,7 +650,7 @@ pub extern "C" fn tmprl_request_workflow_eviction(
619
650
  error: Some(bridge::request_workflow_eviction_response::Error { message }),
620
651
  };
621
652
  unsafe {
622
- callback(user_data, core.encode_proto(&resp).into_raw());
653
+ callback(user_data, worker.encode_proto(&resp).into_raw());
623
654
  }
624
655
  return;
625
656
  }
@@ -627,8 +658,8 @@ pub extern "C" fn tmprl_request_workflow_eviction(
627
658
  let user_data = UserDataHandle(user_data);
628
659
  // We intentionally spawn even though the core call is not async so the
629
660
  // callback can be made in the tokio runtime
630
- core.tokio_runtime.clone().spawn(async move {
631
- core.request_workflow_eviction(req);
661
+ worker.tokio_runtime.clone().spawn(async move {
662
+ worker.request_workflow_eviction(req);
632
663
  unsafe {
633
664
  callback(
634
665
  user_data.into(),
@@ -638,115 +669,102 @@ pub extern "C" fn tmprl_request_workflow_eviction(
638
669
  });
639
670
  }
640
671
 
641
- /// Fetch buffered logs.
672
+ /// Fetch buffered logs. Blocks until complete. This is still using the callback since we might
673
+ /// reasonably change log fetching to be async in the future.
642
674
  ///
643
- /// The req_proto and req_proto_len represent a byte array for a
644
- /// FetchBufferedLogsRequest protobuf message. The callback is invoked
645
- /// on completion with a FetchBufferedLogsResponse protobuf message.
675
+ /// The `req_proto` and `req_proto_len` represent a byte array for a
676
+ /// [bridge::FetchBufferedLogsRequest] protobuf message. The callback is invoked on completion with
677
+ /// a [bridge::FetchBufferedLogsResponse] protobuf message.
646
678
  #[no_mangle]
647
679
  pub extern "C" fn tmprl_fetch_buffered_logs(
648
- core: *mut tmprl_core_t,
649
680
  #[allow(unused_variables)] // We intentionally ignore the request
650
681
  req_proto: *const u8,
651
682
  #[allow(unused_variables)] req_proto_len: libc::size_t,
652
683
  user_data: *mut libc::c_void,
653
684
  callback: tmprl_callback,
654
685
  ) {
655
- let core = unsafe { &mut *core };
656
686
  let user_data = UserDataHandle(user_data);
657
687
  // We intentionally spawn even though the core call is not async so the
658
688
  // callback can be made in the tokio runtime
659
- core.tokio_runtime.clone().spawn(async move {
660
- let resp = core.fetch_buffered_logs();
661
- unsafe { callback(user_data.into(), core.encode_proto(&resp).into_raw()) };
662
- });
689
+ let resp = bridge::FetchBufferedLogsResponse {
690
+ entries: fetch_global_buffered_logs()
691
+ .into_iter()
692
+ .map(|log| bridge::fetch_buffered_logs_response::LogEntry {
693
+ message: log.message,
694
+ timestamp: Some(log.timestamp.into()),
695
+ level: match log.level {
696
+ log::Level::Error => bridge::LogLevel::Error.into(),
697
+ log::Level::Warn => bridge::LogLevel::Warn.into(),
698
+ log::Level::Info => bridge::LogLevel::Info.into(),
699
+ log::Level::Debug => bridge::LogLevel::Debug.into(),
700
+ log::Level::Trace => bridge::LogLevel::Trace.into(),
701
+ },
702
+ })
703
+ .collect(),
704
+ };
705
+
706
+ unsafe {
707
+ callback(
708
+ user_data.into(),
709
+ // TODO: Creates vec every time since no worker/core instance. Can be fixed with a
710
+ // pool if optimizations needed.
711
+ tmprl_bytes_t::from_vec(resp.encode_to_vec()).into_raw(),
712
+ )
713
+ };
663
714
  }
664
715
 
665
- impl tmprl_core_t {
666
- async fn new(
667
- tokio_runtime: std::sync::Arc<tokio::runtime::Runtime>,
668
- opts: wrappers::CoreInitOptions,
669
- ) -> Result<tmprl_core_t, String> {
670
- Ok(tmprl_core_t {
716
+ impl tmprl_worker_t {
717
+ fn new(
718
+ tokio_runtime: Arc<tokio::runtime::Runtime>,
719
+ client: Arc<RetryClient<Client>>,
720
+ opts: wrappers::WorkerConfig,
721
+ ) -> Result<tmprl_worker_t, String> {
722
+ Ok(tmprl_worker_t {
671
723
  tokio_runtime,
672
- core: std::sync::Arc::new(
673
- temporal_sdk_core::init(opts.try_into()?)
674
- .await
675
- .map_err(|err| format!("failed initializing: {}", err))?,
676
- ),
724
+ worker: Arc::new(temporal_sdk_core::init_worker(opts.try_into()?, client)),
677
725
  })
678
726
  }
679
727
 
680
728
  async fn shutdown(&self) {
681
- self.core.shutdown().await;
682
- }
683
-
684
- fn register_worker(
685
- &self,
686
- config: wrappers::WorkerConfig,
687
- ) -> Result<(), bridge::register_worker_response::Error> {
688
- let config =
689
- config
690
- .try_into()
691
- .map_err(|message| bridge::register_worker_response::Error {
692
- message,
693
- worker_already_registered: false,
694
- })?;
695
- self.core.register_worker(config).map_err(|err| {
696
- bridge::register_worker_response::Error {
697
- message: format!("{}", err),
698
- worker_already_registered: matches!(
699
- err,
700
- temporal_sdk_core_api::errors::WorkerRegistrationError::WorkerAlreadyRegisteredForQueue(_),
701
- ),
702
- }
703
- })
704
- }
705
-
706
- async fn shutdown_worker(&self, req: bridge::ShutdownWorkerRequest) {
707
- self.core.shutdown_worker(&req.task_queue).await;
729
+ self.worker.shutdown().await;
708
730
  }
709
731
 
710
732
  async fn poll_workflow_activation(
711
733
  &self,
712
- req: bridge::PollWorkflowActivationRequest,
713
734
  ) -> Result<
714
735
  temporal_sdk_core_protos::coresdk::workflow_activation::WorkflowActivation,
715
736
  bridge::poll_workflow_activation_response::Error,
716
737
  > {
717
- self.core
718
- .poll_workflow_activation(&req.task_queue)
719
- .await
720
- .map_err(|err| bridge::poll_workflow_activation_response::Error {
738
+ self.worker.poll_workflow_activation().await.map_err(|err| {
739
+ bridge::poll_workflow_activation_response::Error {
721
740
  message: format!("{}", err),
722
741
  shutdown: matches!(err, temporal_sdk_core_api::errors::PollWfError::ShutDown),
723
- })
742
+ }
743
+ })
724
744
  }
725
745
 
726
746
  async fn poll_activity_task(
727
747
  &self,
728
- req: bridge::PollActivityTaskRequest,
729
748
  ) -> Result<
730
749
  temporal_sdk_core_protos::coresdk::activity_task::ActivityTask,
731
750
  bridge::poll_activity_task_response::Error,
732
751
  > {
733
- self.core
734
- .poll_activity_task(&req.task_queue)
735
- .await
736
- .map_err(|err| bridge::poll_activity_task_response::Error {
752
+ self.worker.poll_activity_task().await.map_err(|err| {
753
+ bridge::poll_activity_task_response::Error {
737
754
  message: format!("{}", err),
738
755
  shutdown: matches!(
739
756
  err,
740
757
  temporal_sdk_core_api::errors::PollActivityError::ShutDown
741
758
  ),
742
- })
759
+ }
760
+ })
743
761
  }
744
762
 
745
763
  async fn complete_workflow_activation(
746
764
  &self,
747
765
  req: bridge::CompleteWorkflowActivationRequest,
748
766
  ) -> Result<(), bridge::complete_workflow_activation_response::Error> {
749
- self.core
767
+ self.worker
750
768
  .complete_workflow_activation(req.completion.unwrap_or_default())
751
769
  .await
752
770
  .map_err(|err| bridge::complete_workflow_activation_response::Error {
@@ -758,7 +776,7 @@ impl tmprl_core_t {
758
776
  &self,
759
777
  req: bridge::CompleteActivityTaskRequest,
760
778
  ) -> Result<(), bridge::complete_activity_task_response::Error> {
761
- self.core
779
+ self.worker
762
780
  .complete_activity_task(req.completion.unwrap_or_default())
763
781
  .await
764
782
  .map_err(|err| bridge::complete_activity_task_response::Error {
@@ -767,34 +785,12 @@ impl tmprl_core_t {
767
785
  }
768
786
 
769
787
  fn record_activity_heartbeat(&self, req: bridge::RecordActivityHeartbeatRequest) {
770
- self.core
788
+ self.worker
771
789
  .record_activity_heartbeat(req.heartbeat.unwrap_or_default());
772
790
  }
773
791
 
774
792
  fn request_workflow_eviction(&self, req: bridge::RequestWorkflowEvictionRequest) {
775
- self.core
776
- .request_workflow_eviction(&req.task_queue, &req.run_id);
777
- }
778
-
779
- fn fetch_buffered_logs(&self) -> bridge::FetchBufferedLogsResponse {
780
- bridge::FetchBufferedLogsResponse {
781
- entries: self
782
- .core
783
- .fetch_buffered_logs()
784
- .into_iter()
785
- .map(|log| bridge::fetch_buffered_logs_response::LogEntry {
786
- message: log.message,
787
- timestamp: Some(log.timestamp.into()),
788
- level: match log.level {
789
- log::Level::Error => bridge::LogLevel::Error.into(),
790
- log::Level::Warn => bridge::LogLevel::Warn.into(),
791
- log::Level::Info => bridge::LogLevel::Info.into(),
792
- log::Level::Debug => bridge::LogLevel::Debug.into(),
793
- log::Level::Trace => bridge::LogLevel::Trace.into(),
794
- },
795
- })
796
- .collect(),
797
- }
793
+ self.worker.request_workflow_eviction(&req.run_id);
798
794
  }
799
795
 
800
796
  fn borrow_buf(&mut self) -> Vec<u8> {