Skip to content

Commit c00f40a

Browse files
authored
Allow hints for accessing last heartbeat details (#300)
Fixes #297
1 parent 5685901 commit c00f40a

File tree

5 files changed

+37
-16
lines changed

5 files changed

+37
-16
lines changed

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -352,9 +352,9 @@ signal of course). These definition-level hints are passed to converters both fr
352352
implementation side.
353353

354354
There are some advanced payload uses in the SDK that do not currently have a way to set hints. These include
355-
workflow/schedule memo, workflow get/upsert memo, activity last heartbeat details, and application error details. In
356-
some cases, users can use `Temporalio::Converters::RawValue` and then manually convert with hints. For others, hints can
357-
be added as needed, please open an issue or otherwise contact Temporal.
355+
workflow/schedule memo, workflow get/upsert memo, and application error details. In some cases, users can use
356+
`Temporalio::Converters::RawValue` and then manually convert with hints. For others, hints can be added as needed,
357+
please open an issue or otherwise contact Temporal.
358358

359359
### Workers
360360

temporalio/lib/temporalio/activity/info.rb

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# frozen_string_literal: true
22

3+
require 'temporalio/activity/context'
4+
require 'temporalio/internal/proto_utils'
5+
36
module Temporalio
47
module Activity
58
Info = Data.define(
69
:activity_id,
710
:activity_type,
811
:attempt,
912
:current_attempt_scheduled_time,
10-
:heartbeat_details,
1113
:heartbeat_timeout,
1214
:local?,
1315
:priority,
16+
:raw_heartbeat_details,
1417
:schedule_to_close_timeout,
1518
:scheduled_time,
1619
:start_to_close_timeout,
@@ -33,14 +36,15 @@ module Activity
3336
# @return [Integer] Attempt the activity is on.
3437
# @!attribute current_attempt_scheduled_time
3538
# @return [Time] When the current attempt was scheduled.
36-
# @!attribute heartbeat_details
37-
# @return [Array<Object>] Details from the last heartbeat of the last attempt.
3839
# @!attribute heartbeat_timeout
3940
# @return [Float, nil] Heartbeat timeout set by the caller.
4041
# @!attribute local?
4142
# @return [Boolean] Whether the activity is a local activity or not.
4243
# @!attribute priority
4344
# @return [Priority] The priority of this activity.
45+
# @!attribute raw_heartbeat_details
46+
# @return [Array<Converter::RawValue>] Raw details from the last heartbeat of the last attempt. Can use
47+
# {heartbeat_details} to get lazily-converted values.
4448
# @!attribute schedule_to_close_timeout
4549
# @return [Float, nil] Schedule to close timeout set by the caller.
4650
# @!attribute scheduled_time
@@ -65,6 +69,20 @@ module Activity
6569
#
6670
# @note WARNING: This class may have required parameters added to its constructor. Users should not instantiate this
6771
# class or it may break in incompatible ways.
68-
class Info; end # rubocop:disable Lint/EmptyClass
72+
class Info
73+
# Convert raw heartbeat details into Ruby types.
74+
#
75+
# Note, this live-converts every invocation.
76+
#
77+
# @param hints [Array<Object>, nil] Hints, if any, to assist conversion.
78+
# @return [Array<Object>] Converted details.
79+
def heartbeat_details(hints: nil)
80+
Internal::ProtoUtils.convert_from_payload_array(
81+
Context.current.payload_converter,
82+
raw_heartbeat_details.map(&:payload),
83+
hints:
84+
)
85+
end
86+
end
6987
end
7088
end

temporalio/lib/temporalio/internal/worker/activity_worker.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -176,14 +176,15 @@ def execute_activity(task_token, defn, start)
176176
current_attempt_scheduled_time: Internal::ProtoUtils.timestamp_to_time(
177177
start.current_attempt_scheduled_time
178178
) || raise, # Never nil
179-
heartbeat_details: ProtoUtils.convert_from_payload_array(
180-
@worker.options.client.data_converter,
181-
start.heartbeat_details.to_ary,
182-
hints: nil
183-
),
184179
heartbeat_timeout: Internal::ProtoUtils.duration_to_seconds(start.heartbeat_timeout),
185180
local?: start.is_local,
186181
priority: Priority._from_proto(start.priority),
182+
raw_heartbeat_details: begin
183+
payloads = start.heartbeat_details.to_ary
184+
codec = @worker.options.client.data_converter.payload_codec
185+
payloads = codec.decode(payloads) if codec
186+
payloads.map { |p| Temporalio::Converters::RawValue.new(p) }
187+
end,
187188
schedule_to_close_timeout: Internal::ProtoUtils.duration_to_seconds(start.schedule_to_close_timeout),
188189
scheduled_time: Internal::ProtoUtils.timestamp_to_time(start.scheduled_time) || raise, # Never nil
189190
start_to_close_timeout: Internal::ProtoUtils.duration_to_seconds(start.start_to_close_timeout),
@@ -194,7 +195,7 @@ def execute_activity(task_token, defn, start)
194195
workflow_namespace: start.workflow_namespace,
195196
workflow_run_id: start.workflow_execution.run_id,
196197
workflow_type: start.workflow_type
197-
).freeze
198+
)
198199

199200
# Build input
200201
input = Temporalio::Worker::Interceptor::Activity::ExecuteInput.new(

temporalio/lib/temporalio/testing/activity_environment.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ def self.default_info
2121
activity_type: 'unknown',
2222
attempt: 1,
2323
current_attempt_scheduled_time: Time.at(0),
24-
heartbeat_details: [],
2524
heartbeat_timeout: nil,
2625
local?: false,
2726
priority: Temporalio::Priority.default,
27+
raw_heartbeat_details: [],
2828
schedule_to_close_timeout: 1.0,
2929
scheduled_time: Time.at(0),
3030
start_to_close_timeout: 1.0,

temporalio/sig/temporalio/activity/info.rbs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ module Temporalio
55
attr_reader activity_type: String
66
attr_reader attempt: Integer
77
attr_reader current_attempt_scheduled_time: Time
8-
attr_reader heartbeat_details: Array[Object?]
98
attr_reader heartbeat_timeout: Float?
109
attr_reader local?: bool
1110
attr_reader priority: Temporalio::Priority
11+
attr_reader raw_heartbeat_details: Array[Converters::RawValue]
1212
attr_reader schedule_to_close_timeout: Float?
1313
attr_reader scheduled_time: Time
1414
attr_reader start_to_close_timeout: Float?
@@ -25,10 +25,10 @@ module Temporalio
2525
activity_type: String,
2626
attempt: Integer,
2727
current_attempt_scheduled_time: Time,
28-
heartbeat_details: Array[Object?],
2928
heartbeat_timeout: Float?,
3029
local?: bool,
3130
priority: Temporalio::Priority?,
31+
raw_heartbeat_details: Array[Converters::RawValue],
3232
schedule_to_close_timeout: Float?,
3333
scheduled_time: Time,
3434
start_to_close_timeout: Float?,
@@ -41,6 +41,8 @@ module Temporalio
4141
workflow_type: String
4242
) -> void
4343

44+
def heartbeat_details: (?hints: Array[Object]?) -> Array[Object?]
45+
4446
def with: (**untyped) -> Info
4547
end
4648
end

0 commit comments

Comments
 (0)