Class: Temporalio::Internal::Client::Implementation

Inherits:
Client::Interceptor::Outbound show all
Defined in:
lib/temporalio/internal/client/implementation.rb

Instance Attribute Summary

Attributes inherited from Client::Interceptor::Outbound

#next_interceptor

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Implementation

Returns a new instance of Implementation.



25
26
27
28
# File 'lib/temporalio/internal/client/implementation.rb', line 25

def initialize(client)
  super(nil)
  @client = client
end

Instance Method Details

#cancel_workflow(input) ⇒ Object



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'lib/temporalio/internal/client/implementation.rb', line 348

def cancel_workflow(input)
  @client.workflow_service.request_cancel_workflow_execution(
    Api::WorkflowService::V1::RequestCancelWorkflowExecutionRequest.new(
      namespace: @client.namespace,
      workflow_execution: Api::Common::V1::WorkflowExecution.new(
        workflow_id: input.workflow_id,
        run_id: input.run_id || ''
      ),
      first_execution_run_id: input.first_execution_run_id,
      identity: @client.connection.identity,
      request_id: SecureRandom.uuid
    ),
    rpc_retry: true,
    rpc_metadata: input.,
    rpc_timeout: input.rpc_timeout
  )
  nil
end

#complete_async_activity(input) ⇒ Object



420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
# File 'lib/temporalio/internal/client/implementation.rb', line 420

def complete_async_activity(input)
  if input.task_token_or_id_reference.is_a?(Temporalio::Client::ActivityIDReference)
    @client.workflow_service.respond_activity_task_completed_by_id(
      Api::WorkflowService::V1::RespondActivityTaskCompletedByIdRequest.new(
        workflow_id: input.task_token_or_id_reference.workflow_id,
        run_id: input.task_token_or_id_reference.run_id,
        activity_id: input.task_token_or_id_reference.activity_id,
        namespace: @client.namespace,
        identity: @client.connection.identity,
        result: @client.data_converter.to_payloads([input.result])
      ),
      rpc_retry: true,
      rpc_metadata: input.,
      rpc_timeout: input.rpc_timeout
    )
  else
    @client.workflow_service.respond_activity_task_completed(
      Api::WorkflowService::V1::RespondActivityTaskCompletedRequest.new(
        task_token: input.task_token_or_id_reference,
        namespace: @client.namespace,
        identity: @client.connection.identity,
        result: @client.data_converter.to_payloads([input.result])
      ),
      rpc_retry: true,
      rpc_metadata: input.,
      rpc_timeout: input.rpc_timeout
    )
  end
  nil
end

#count_workflows(input) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/temporalio/internal/client/implementation.rb', line 112

def count_workflows(input)
  resp = @client.workflow_service.count_workflow_executions(
    Api::WorkflowService::V1::CountWorkflowExecutionsRequest.new(
      namespace: @client.namespace,
      query: input.query || ''
    ),
    rpc_retry: true,
    rpc_metadata: input.,
    rpc_timeout: input.rpc_timeout
  )
  Temporalio::Client::WorkflowExecutionCount.new(
    resp.count,
    resp.groups.map do |group|
      Temporalio::Client::WorkflowExecutionCount::AggregationGroup.new(
        group.count,
        group.group_values.map { |payload| SearchAttributes.value_from_payload(payload) }
      )
    end
  )
end

#describe_workflow(input) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'lib/temporalio/internal/client/implementation.rb', line 133

def describe_workflow(input)
  resp = @client.workflow_service.describe_workflow_execution(
    Api::WorkflowService::V1::DescribeWorkflowExecutionRequest.new(
      namespace: @client.namespace,
      execution: Api::Common::V1::WorkflowExecution.new(
        workflow_id: input.workflow_id,
        run_id: input.run_id || ''
      )
    ),
    rpc_retry: true,
    rpc_metadata: input.,
    rpc_timeout: input.rpc_timeout
  )
  Temporalio::Client::WorkflowExecution::Description.new(resp, @client.data_converter)
end

#fail_async_activity(input) ⇒ Object



451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
# File 'lib/temporalio/internal/client/implementation.rb', line 451

def fail_async_activity(input)
  if input.task_token_or_id_reference.is_a?(Temporalio::Client::ActivityIDReference)
    @client.workflow_service.respond_activity_task_failed_by_id(
      Api::WorkflowService::V1::RespondActivityTaskFailedByIdRequest.new(
        workflow_id: input.task_token_or_id_reference.workflow_id,
        run_id: input.task_token_or_id_reference.run_id,
        activity_id: input.task_token_or_id_reference.activity_id,
        namespace: @client.namespace,
        identity: @client.connection.identity,
        failure: @client.data_converter.to_failure(input.error),
        last_heartbeat_details: if input.last_heartbeat_details.empty?
                                  nil
                                else
                                  @client.data_converter.to_payloads(input.last_heartbeat_details)
                                end
      ),
      rpc_retry: true,
      rpc_metadata: input.,
      rpc_timeout: input.rpc_timeout
    )
  else
    @client.workflow_service.respond_activity_task_failed(
      Api::WorkflowService::V1::RespondActivityTaskFailedRequest.new(
        task_token: input.task_token_or_id_reference,
        namespace: @client.namespace,
        identity: @client.connection.identity,
        failure: @client.data_converter.to_failure(input.error),
        last_heartbeat_details: if input.last_heartbeat_details.empty?
                                  nil
                                else
                                  @client.data_converter.to_payloads(input.last_heartbeat_details)
                                end
      ),
      rpc_retry: true,
      rpc_metadata: input.,
      rpc_timeout: input.rpc_timeout
    )
  end
  nil
end

#fetch_workflow_history_events(input) ⇒ Object



149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/temporalio/internal/client/implementation.rb', line 149

def fetch_workflow_history_events(input)
  Enumerator.new do |yielder|
    req = Api::WorkflowService::V1::GetWorkflowExecutionHistoryRequest.new(
      namespace: @client.namespace,
      execution: Api::Common::V1::WorkflowExecution.new(
        workflow_id: input.workflow_id,
        run_id: input.run_id || ''
      ),
      wait_new_event: input.wait_new_event,
      history_event_filter_type: input.event_filter_type,
      skip_archival: input.skip_archival
    )
    loop do
      resp = @client.workflow_service.get_workflow_execution_history(
        req,
        rpc_retry: true,
        rpc_metadata: input.,
        rpc_timeout: input.rpc_timeout
      )
      resp.history&.events&.each { |event| yielder << event }
      break if resp.next_page_token.empty?

      req.next_page_token = resp.next_page_token
    end
  end
end

#heartbeat_async_activity(input) ⇒ Object



387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
# File 'lib/temporalio/internal/client/implementation.rb', line 387

def heartbeat_async_activity(input)
  resp = if input.task_token_or_id_reference.is_a?(Temporalio::Client::ActivityIDReference)
           @client.workflow_service.record_activity_task_heartbeat_by_id(
             Api::WorkflowService::V1::RecordActivityTaskHeartbeatByIdRequest.new(
               workflow_id: input.task_token_or_id_reference.workflow_id,
               run_id: input.task_token_or_id_reference.run_id,
               activity_id: input.task_token_or_id_reference.activity_id,
               namespace: @client.namespace,
               identity: @client.connection.identity,
               details: @client.data_converter.to_payloads(input.details)
             ),
             rpc_retry: true,
             rpc_metadata: input.,
             rpc_timeout: input.rpc_timeout
           )
         else
           @client.workflow_service.record_activity_task_heartbeat(
             Api::WorkflowService::V1::RecordActivityTaskHeartbeatRequest.new(
               task_token: input.task_token_or_id_reference,
               namespace: @client.namespace,
               identity: @client.connection.identity,
               details: @client.data_converter.to_payloads(input.details)
             ),
             rpc_retry: true,
             rpc_metadata: input.,
             rpc_timeout: input.rpc_timeout
           )
         end
  raise Error::AsyncActivityCanceledError if resp.cancel_requested

  nil
end

#list_workflows(input) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/temporalio/internal/client/implementation.rb', line 89

def list_workflows(input)
  Enumerator.new do |yielder|
    req = Api::WorkflowService::V1::ListWorkflowExecutionsRequest.new(
      namespace: @client.namespace,
      query: input.query || ''
    )
    loop do
      resp = @client.workflow_service.list_workflow_executions(
        req,
        rpc_retry: true,
        rpc_metadata: input.,
        rpc_timeout: input.rpc_timeout
      )
      resp.executions.each do |raw_info|
        yielder << Temporalio::Client::WorkflowExecution.new(raw_info, @client.data_converter)
      end
      break if resp.next_page_token.empty?

      req.next_page_token = resp.next_page_token
    end
  end
end

#poll_workflow_update(input) ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
# File 'lib/temporalio/internal/client/implementation.rb', line 313

def poll_workflow_update(input)
  req = Api::WorkflowService::V1::PollWorkflowExecutionUpdateRequest.new(
    namespace: @client.namespace,
    update_ref: Api::Update::V1::UpdateRef.new(
      workflow_execution: Api::Common::V1::WorkflowExecution.new(
        workflow_id: input.workflow_id,
        run_id: input.run_id || ''
      ),
      update_id: input.update_id
    ),
    identity: @client.connection.identity,
    wait_policy: Api::Update::V1::WaitPolicy.new(
      lifecycle_stage: Temporalio::Client::WorkflowUpdateWaitStage::COMPLETED
    )
  )

  # Continue polling as long as we have no outcome
  loop do
    resp = @client.workflow_service.poll_workflow_execution_update(
      req,
      rpc_retry: true,
      rpc_metadata: input.,
      rpc_timeout: input.rpc_timeout
    )
    return resp.outcome if resp.outcome
  rescue Error::RPCError => e
    # Deadline exceeded or cancel is a special error type
    if e.code == Error::RPCError::Code::DEADLINE_EXCEEDED || e.code == Error::RPCError::Code::CANCELLED
      raise Error::WorkflowUpdateRPCTimeoutOrCanceledError
    end

    raise
  end
end

#query_workflow(input) ⇒ Object



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/temporalio/internal/client/implementation.rb', line 197

def query_workflow(input)
  begin
    resp = @client.workflow_service.query_workflow(
      Api::WorkflowService::V1::QueryWorkflowRequest.new(
        namespace: @client.namespace,
        execution: Api::Common::V1::WorkflowExecution.new(
          workflow_id: input.workflow_id,
          run_id: input.run_id || ''
        ),
        query: Api::Query::V1::WorkflowQuery.new(
          query_type: input.query,
          query_args: @client.data_converter.to_payloads(input.args),
          header: input.headers
        ),
        query_reject_condition: input.reject_condition || 0
      ),
      rpc_retry: true,
      rpc_metadata: input.,
      rpc_timeout: input.rpc_timeout
    )
  rescue Error::RPCError => e
    # If the status is INVALID_ARGUMENT, we can assume it's a query failed
    # error
    raise Error::WorkflowQueryFailedError, e.message if e.code == Error::RPCError::Code::INVALID_ARGUMENT

    raise
  end
  unless resp.query_rejected.nil?
    raise Error::WorkflowQueryRejectedError.new(status: ProtoUtils.enum_to_int(
      Api::Enums::V1::WorkflowExecutionStatus, resp.query_rejected.status
    ))
  end

  results = @client.data_converter.from_payloads(resp.query_result)
  warn("Expected 0 or 1 query result, got #{results.size}") if results.size > 1
  results&.first
end

#report_cancellation_async_activity(input) ⇒ Object



492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
# File 'lib/temporalio/internal/client/implementation.rb', line 492

def report_cancellation_async_activity(input)
  if input.task_token_or_id_reference.is_a?(Temporalio::Client::ActivityIDReference)
    @client.workflow_service.respond_activity_task_canceled_by_id(
      Api::WorkflowService::V1::RespondActivityTaskCanceledByIdRequest.new(
        workflow_id: input.task_token_or_id_reference.workflow_id,
        run_id: input.task_token_or_id_reference.run_id,
        activity_id: input.task_token_or_id_reference.activity_id,
        namespace: @client.namespace,
        identity: @client.connection.identity,
        details: @client.data_converter.to_payloads(input.details)
      ),
      rpc_retry: true,
      rpc_metadata: input.,
      rpc_timeout: input.rpc_timeout
    )
  else
    @client.workflow_service.respond_activity_task_canceled(
      Api::WorkflowService::V1::RespondActivityTaskCanceledRequest.new(
        task_token: input.task_token_or_id_reference,
        namespace: @client.namespace,
        identity: @client.connection.identity,
        details: @client.data_converter.to_payloads(input.details)
      ),
      rpc_retry: true,
      rpc_metadata: input.,
      rpc_timeout: input.rpc_timeout
    )
  end
  nil
end

#signal_workflow(input) ⇒ Object



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'lib/temporalio/internal/client/implementation.rb', line 176

def signal_workflow(input)
  @client.workflow_service.signal_workflow_execution(
    Api::WorkflowService::V1::SignalWorkflowExecutionRequest.new(
      namespace: @client.namespace,
      workflow_execution: Api::Common::V1::WorkflowExecution.new(
        workflow_id: input.workflow_id,
        run_id: input.run_id || ''
      ),
      signal_name: input.signal,
      input: @client.data_converter.to_payloads(input.args),
      header: input.headers,
      identity: @client.connection.identity,
      request_id: SecureRandom.uuid
    ),
    rpc_retry: true,
    rpc_metadata: input.,
    rpc_timeout: input.rpc_timeout
  )
  nil
end

#start_workflow(input) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/temporalio/internal/client/implementation.rb', line 30

def start_workflow(input)
  # TODO(cretz): Signal/update with start
  req = Api::WorkflowService::V1::StartWorkflowExecutionRequest.new(
    request_id: SecureRandom.uuid,
    namespace: @client.namespace,
    workflow_type: Api::Common::V1::WorkflowType.new(name: input.workflow.to_s),
    workflow_id: input.workflow_id,
    task_queue: Api::TaskQueue::V1::TaskQueue.new(name: input.task_queue.to_s),
    input: @client.data_converter.to_payloads(input.args),
    workflow_execution_timeout: ProtoUtils.seconds_to_duration(input.execution_timeout),
    workflow_run_timeout: ProtoUtils.seconds_to_duration(input.run_timeout),
    workflow_task_timeout: ProtoUtils.seconds_to_duration(input.task_timeout),
    identity: @client.connection.identity,
    workflow_id_reuse_policy: input.id_reuse_policy,
    workflow_id_conflict_policy: input.id_conflict_policy,
    retry_policy: input.retry_policy&.to_proto,
    cron_schedule: input.cron_schedule,
    memo: ProtoUtils.memo_to_proto(input.memo, @client.data_converter),
    search_attributes: input.search_attributes&.to_proto,
    workflow_start_delay: ProtoUtils.seconds_to_duration(input.start_delay),
    request_eager_execution: input.request_eager_start,
    header: input.headers
  )

  # Send request
  begin
    resp = @client.workflow_service.start_workflow_execution(
      req,
      rpc_retry: true,
      rpc_metadata: input.,
      rpc_timeout: input.rpc_timeout
    )
  rescue Error::RPCError => e
    # Unpack and raise already started if that's the error, otherwise default raise
    if e.code == Error::RPCError::Code::ALREADY_EXISTS && e.grpc_status.details.first
      details = e.grpc_status.details.first.unpack(
        Api::ErrorDetails::V1::WorkflowExecutionAlreadyStartedFailure
      )
      if details
        raise Error::WorkflowAlreadyStartedError.new(
          workflow_id: req.workflow_id,
          workflow_type: req.workflow_type.name,
          run_id: details.run_id
        )
      end
    end
    raise
  end

  # Return handle
  Temporalio::Client::WorkflowHandle.new(
    client: @client,
    id: input.workflow_id,
    run_id: nil,
    result_run_id: resp.run_id,
    first_execution_run_id: resp.run_id
  )
end

#start_workflow_update(input) ⇒ Object



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/temporalio/internal/client/implementation.rb', line 235

def start_workflow_update(input)
  if input.wait_for_stage == Temporalio::Client::WorkflowUpdateWaitStage::ADMITTED
    raise ArgumentError, 'ADMITTED wait stage not supported'
  end

  req = Api::WorkflowService::V1::UpdateWorkflowExecutionRequest.new(
    namespace: @client.namespace,
    workflow_execution: Api::Common::V1::WorkflowExecution.new(
      workflow_id: input.workflow_id,
      run_id: input.run_id || ''
    ),
    request: Api::Update::V1::Request.new(
      meta: Api::Update::V1::Meta.new(
        update_id: input.update_id,
        identity: @client.connection.identity
      ),
      input: Api::Update::V1::Input.new(
        name: input.update,
        args: @client.data_converter.to_payloads(input.args),
        header: input.headers
      )
    ),
    wait_policy: Api::Update::V1::WaitPolicy.new(
      lifecycle_stage: input.wait_for_stage
    )
  )

  # Repeatedly try to invoke start until the update reaches user-provided
  # wait stage or is at least ACCEPTED (as of the time of this writing,
  # the user cannot specify sooner than ACCEPTED)
  # @type var resp: untyped
  resp = nil
  loop do
    resp = @client.workflow_service.update_workflow_execution(
      req,
      rpc_retry: true,
      rpc_metadata: input.,
      rpc_timeout: input.rpc_timeout
    )

    # We're only done if the response stage is after the requested stage
    # or the response stage is accepted
    if resp.stage >= req.wait_policy.lifecycle_stage ||
       resp.stage >= Temporalio::Client::WorkflowUpdateWaitStage::ACCEPTED
      break
    end
  rescue Error::RPCError => e
    # Deadline exceeded or cancel is a special error type
    if e.code == Error::RPCError::Code::DEADLINE_EXCEEDED || e.code == Error::RPCError::Code::CANCELLED
      raise Error::WorkflowUpdateRPCTimeoutOrCanceledError
    end

    raise
  end

  # If the user wants to wait until completed, we must poll until outcome
  # if not already there
  if input.wait_for_stage == Temporalio::Client::WorkflowUpdateWaitStage::COMPLETED && !resp.outcome
    resp.outcome = @client._impl.poll_workflow_update(
      Temporalio::Client::Interceptor::PollWorkflowUpdateInput.new(
        workflow_id: input.workflow_id,
        run_id: input.run_id,
        update_id: input.update_id,
        rpc_metadata: input.,
        rpc_timeout: input.rpc_timeout
      )
    )
  end

  Temporalio::Client::WorkflowUpdateHandle.new(
    client: @client,
    id: input.update_id,
    workflow_id: input.workflow_id,
    workflow_run_id: input.run_id,
    known_outcome: resp.outcome
  )
end

#terminate_workflow(input) ⇒ Object



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
# File 'lib/temporalio/internal/client/implementation.rb', line 367

def terminate_workflow(input)
  @client.workflow_service.terminate_workflow_execution(
    Api::WorkflowService::V1::TerminateWorkflowExecutionRequest.new(
      namespace: @client.namespace,
      workflow_execution: Api::Common::V1::WorkflowExecution.new(
        workflow_id: input.workflow_id,
        run_id: input.run_id || ''
      ),
      reason: input.reason || '',
      first_execution_run_id: input.first_execution_run_id,
      details: @client.data_converter.to_payloads(input.details),
      identity: @client.connection.identity
    ),
    rpc_retry: true,
    rpc_metadata: input.,
    rpc_timeout: input.rpc_timeout
  )
  nil
end