|
2 | 2 |
|
3 | 3 | require 'google/protobuf/well_known_types' |
4 | 4 | require 'temporalio/api' |
| 5 | +require 'temporalio/client/async_activity_handle' |
5 | 6 | require 'temporalio/client/connection' |
| 7 | +require 'temporalio/client/implementation' |
6 | 8 | require 'temporalio/client/interceptor' |
| 9 | +require 'temporalio/client/workflow_execution' |
| 10 | +require 'temporalio/client/workflow_execution_count' |
7 | 11 | require 'temporalio/client/workflow_handle' |
| 12 | +require 'temporalio/client/workflow_query_reject_condition' |
8 | 13 | require 'temporalio/common_enums' |
9 | 14 | require 'temporalio/converters' |
10 | 15 | require 'temporalio/error' |
11 | | -require 'temporalio/internal/proto_utils' |
| 16 | +require 'temporalio/retry_policy' |
12 | 17 | require 'temporalio/runtime' |
13 | 18 | require 'temporalio/search_attributes' |
14 | 19 |
|
@@ -45,10 +50,10 @@ class Client |
45 | 50 | # TLS options are present, those TLS options will be used. |
46 | 51 | # @param data_converter [Converters::DataConverter] Data converter to use for all data conversions to/from payloads. |
47 | 52 | # @param interceptors [Array<Interceptor>] Set of interceptors that are chained together to allow intercepting of |
48 | | - # client calls. The earlier interceptors wrap the later ones. Any interceptors that also implement |
49 | | - # {Worker::Interceptor} will be used as worker interceptors too so they should not be given separately when |
50 | | - # creating a worker. |
51 | | - # @param default_workflow_query_reject_condition [Api::Enums::V1::QueryRejectCondition, nil] Default rejection |
| 53 | + # client calls. The earlier interceptors wrap the later ones. Any interceptors that also implement worker |
| 54 | + # interceptor will be used as worker interceptors too so they should not be given separately when creating a |
| 55 | + # worker. |
| 56 | + # @param default_workflow_query_reject_condition [WorkflowQueryRejectCondition, nil] Default rejection |
52 | 57 | # condition for workflow queries if not set during query. See {WorkflowHandle.query} for details on the |
53 | 58 | # rejection condition. |
54 | 59 | # @param rpc_metadata [Hash<String, String>] Headers to use for all calls to the server. Keys here can be overriden |
@@ -116,11 +121,10 @@ def self.connect( |
116 | 121 | # @param interceptors [Array<Interceptor>] Set of interceptors that are chained together to allow intercepting of |
117 | 122 | # client calls. The earlier interceptors wrap the later ones. |
118 | 123 | # |
119 | | - # Any interceptors that also implement {Worker::Interceptor} will be used as worker interceptors too so they |
120 | | - # should not be given separately when creating a worker. |
121 | | - # @param default_workflow_query_reject_condition [Api::Enums::V1::QueryRejectCondition, nil] Default rejection |
122 | | - # condition for workflow queries if not set during query. See {WorkflowHandle.query} for details on the |
123 | | - # rejection condition. |
| 124 | + # Any interceptors that also implement worker interceptor will be used as worker interceptors too so they should |
| 125 | + # not be given separately when creating a worker. |
| 126 | + # @param default_workflow_query_reject_condition [WorkflowQueryRejectCondition, nil] Default rejection condition for |
| 127 | + # workflow queries if not set during query. See {WorkflowHandle.query} for details on the rejection condition. |
124 | 128 | # |
125 | 129 | # @see connect |
126 | 130 | def initialize( |
@@ -218,7 +222,7 @@ def start_workflow( |
218 | 222 | @impl.start_workflow(Interceptor::StartWorkflowInput.new( |
219 | 223 | workflow:, |
220 | 224 | args:, |
221 | | - id:, |
| 225 | + workflow_id: id, |
222 | 226 | task_queue:, |
223 | 227 | execution_timeout:, |
224 | 228 | run_timeout:, |
@@ -264,7 +268,7 @@ def start_workflow( |
264 | 268 | # |
265 | 269 | # @return [Object] Successful result of the workflow. |
266 | 270 | # @raise [Error::WorkflowAlreadyStartedError] Workflow already exists. |
267 | | - # @raise [Error::WorkflowFailureError] Workflow failed with {Error::WorkflowFailureError.cause} as cause. |
| 271 | + # @raise [Error::WorkflowFailureError] Workflow failed with +cause+ as the cause. |
268 | 272 | # @raise [Error::RPCError] RPC error from call. |
269 | 273 | def execute_workflow( |
270 | 274 | workflow, |
@@ -320,103 +324,72 @@ def workflow_handle( |
320 | 324 | run_id: nil, |
321 | 325 | first_execution_run_id: nil |
322 | 326 | ) |
323 | | - WorkflowHandle.new(self, workflow_id, run_id:, result_run_id: run_id, first_execution_run_id:) |
| 327 | + WorkflowHandle.new(client: self, id: workflow_id, run_id:, result_run_id: run_id, first_execution_run_id:) |
324 | 328 | end |
325 | 329 |
|
326 | | - # @!visibility private |
327 | | - def _impl |
328 | | - @impl |
| 330 | + # List workflows. |
| 331 | + # |
| 332 | + # @param query [String, nil] A Temporal visibility list filter. |
| 333 | + # @param rpc_metadata [Hash<String, String>, nil] Headers to include on the RPC call. |
| 334 | + # @param rpc_timeout [Float, nil] Number of seconds before timeout. |
| 335 | + # |
| 336 | + # @return [Enumerator<WorkflowExecution>] Enumerable workflow executions. |
| 337 | + # |
| 338 | + # @raise [Error::RPCError] RPC error from call. |
| 339 | + # |
| 340 | + # @see https://docs.temporal.io/visibility |
| 341 | + def list_workflows( |
| 342 | + query = nil, |
| 343 | + rpc_metadata: nil, |
| 344 | + rpc_timeout: nil |
| 345 | + ) |
| 346 | + @impl.list_workflows(Interceptor::ListWorkflowsInput.new( |
| 347 | + query:, |
| 348 | + rpc_metadata:, |
| 349 | + rpc_timeout: |
| 350 | + )) |
329 | 351 | end |
330 | 352 |
|
331 | | - # @!visibility private |
332 | | - class Implementation < Interceptor::Outbound |
333 | | - def initialize(client) |
334 | | - super(nil) |
335 | | - @client = client |
336 | | - end |
337 | | - |
338 | | - # @!visibility private |
339 | | - def start_workflow(input) |
340 | | - # TODO(cretz): Signal/update with start |
341 | | - req = Api::WorkflowService::V1::StartWorkflowExecutionRequest.new( |
342 | | - request_id: SecureRandom.uuid, |
343 | | - namespace: @client.namespace, |
344 | | - workflow_type: Api::Common::V1::WorkflowType.new(name: input.workflow.to_s), |
345 | | - workflow_id: input.id, |
346 | | - task_queue: Api::TaskQueue::V1::TaskQueue.new(name: input.task_queue.to_s), |
347 | | - input: @client.data_converter.to_payloads(input.args), |
348 | | - workflow_execution_timeout: Internal::ProtoUtils.seconds_to_duration(input.execution_timeout), |
349 | | - workflow_run_timeout: Internal::ProtoUtils.seconds_to_duration(input.run_timeout), |
350 | | - workflow_task_timeout: Internal::ProtoUtils.seconds_to_duration(input.task_timeout), |
351 | | - identity: @client.connection.identity, |
352 | | - workflow_id_reuse_policy: input.id_reuse_policy, |
353 | | - workflow_id_conflict_policy: input.id_conflict_policy, |
354 | | - retry_policy: input.retry_policy&.to_proto, |
355 | | - cron_schedule: input.cron_schedule, |
356 | | - memo: Internal::ProtoUtils.memo_to_proto(input.memo, @client.data_converter), |
357 | | - search_attributes: input.search_attributes&.to_proto, |
358 | | - workflow_start_delay: Internal::ProtoUtils.seconds_to_duration(input.start_delay), |
359 | | - request_eager_execution: input.request_eager_start, |
360 | | - header: input.headers |
361 | | - ) |
362 | | - |
363 | | - # Send request |
364 | | - begin |
365 | | - resp = @client.workflow_service.start_workflow_execution( |
366 | | - req, |
367 | | - rpc_retry: true, |
368 | | - rpc_metadata: input.rpc_metadata, |
369 | | - rpc_timeout: input.rpc_timeout |
370 | | - ) |
371 | | - rescue Error::RPCError => e |
372 | | - # Unpack and raise already started if that's the error, otherwise default raise |
373 | | - if e.code == Error::RPCError::Code::ALREADY_EXISTS && e.grpc_status.details.first |
374 | | - details = e.grpc_status.details.first.unpack(Api::ErrorDetails::V1::WorkflowExecutionAlreadyStartedFailure) |
375 | | - if details |
376 | | - raise Error::WorkflowAlreadyStartedError.new( |
377 | | - workflow_id: req.workflow_id, |
378 | | - workflow_type: req.workflow_type.name, |
379 | | - run_id: details.run_id |
380 | | - ) |
381 | | - end |
382 | | - end |
383 | | - raise |
384 | | - end |
| 353 | + # Count workflows. |
| 354 | + # |
| 355 | + # @param query [String, nil] A Temporal visibility list filter. |
| 356 | + # @param rpc_metadata [Hash<String, String>, nil] Headers to include on the RPC call. |
| 357 | + # @param rpc_timeout [Float, nil] Number of seconds before timeout. |
| 358 | + # |
| 359 | + # @return [WorkflowExecutionCount] Count of workflows. |
| 360 | + # |
| 361 | + # @raise [Error::RPCError] RPC error from call. |
| 362 | + # |
| 363 | + # @see https://docs.temporal.io/visibility |
| 364 | + def count_workflows( |
| 365 | + query = nil, |
| 366 | + rpc_metadata: nil, |
| 367 | + rpc_timeout: nil |
| 368 | + ) |
| 369 | + @impl.count_workflows(Interceptor::CountWorkflowsInput.new( |
| 370 | + query:, |
| 371 | + rpc_metadata:, |
| 372 | + rpc_timeout: |
| 373 | + )) |
| 374 | + end |
385 | 375 |
|
386 | | - # Return handle |
387 | | - WorkflowHandle.new( |
388 | | - @client, |
389 | | - input.id, |
390 | | - result_run_id: resp.run_id, |
391 | | - first_execution_run_id: resp.run_id |
392 | | - ) |
| 376 | + # Get an async activity handle. |
| 377 | + # |
| 378 | + # @param task_token_or_id_reference [String, ActivityIDReference] Task token string or activity ID reference. |
| 379 | + # @return [AsyncActivityHandle] |
| 380 | + def async_activity_handle(task_token_or_id_reference) |
| 381 | + if task_token_or_id_reference.is_a?(ActivityIDReference) |
| 382 | + AsyncActivityHandle.new(client: self, task_token: nil, id_reference: task_token_or_id_reference) |
| 383 | + elsif task_token_or_id_reference.is_a?(String) |
| 384 | + AsyncActivityHandle.new(client: self, task_token: task_token_or_id_reference, id_reference: nil) |
| 385 | + else |
| 386 | + raise ArgumentError, 'Must be a string task token or an ActivityIDReference' |
393 | 387 | end |
| 388 | + end |
394 | 389 |
|
395 | | - # @!visibility private |
396 | | - def fetch_workflow_history_event_page(input) |
397 | | - req = Api::WorkflowService::V1::GetWorkflowExecutionHistoryRequest.new( |
398 | | - namespace: @client.namespace, |
399 | | - execution: Api::Common::V1::WorkflowExecution.new( |
400 | | - workflow_id: input.id, |
401 | | - run_id: input.run_id || '' |
402 | | - ), |
403 | | - maximum_page_size: input.page_size || 0, |
404 | | - next_page_token: input.next_page_token, |
405 | | - wait_new_event: input.wait_new_event, |
406 | | - history_event_filter_type: input.event_filter_type, |
407 | | - skip_archival: input.skip_archival |
408 | | - ) |
409 | | - resp = @client.workflow_service.get_workflow_execution_history( |
410 | | - req, |
411 | | - rpc_retry: true, |
412 | | - rpc_metadata: input.rpc_metadata, |
413 | | - rpc_timeout: input.rpc_timeout |
414 | | - ) |
415 | | - Interceptor::FetchWorkflowHistoryEventPage.new( |
416 | | - events: resp.history&.events || [], |
417 | | - next_page_token: resp.next_page_token.empty? ? nil : resp.next_page_token |
418 | | - ) |
419 | | - end |
| 390 | + # @!visibility private |
| 391 | + def _impl |
| 392 | + @impl |
420 | 393 | end |
421 | 394 | end |
422 | 395 | end |
0 commit comments