From 33208a75fdefb3598198c9ff18c4c264fb423656 Mon Sep 17 00:00:00 2001 From: Nathan Glass Date: Thu, 10 Mar 2022 15:23:06 -0800 Subject: [PATCH] Expose scheduled_time and current_attempt_scheduled_time on activity metadata --- lib/temporal/metadata.rb | 5 ++++- lib/temporal/metadata/activity.rb | 10 +++++++--- lib/temporal/testing/local_workflow_context.rb | 8 ++++++-- spec/fabricators/activity_metadata_fabricator.rb | 2 ++ spec/fabricators/grpc/activity_task_fabricator.rb | 2 ++ spec/unit/lib/temporal/metadata/activity_spec.rb | 6 +++++- .../lib/temporal/testing/temporal_override_spec.rb | 2 ++ 7 files changed, 28 insertions(+), 7 deletions(-) diff --git a/lib/temporal/metadata.rb b/lib/temporal/metadata.rb index df7165ea..4602dfd1 100644 --- a/lib/temporal/metadata.rb +++ b/lib/temporal/metadata.rb @@ -21,7 +21,10 @@ def generate_activity_metadata(task, namespace) workflow_id: task.workflow_execution.workflow_id, workflow_name: task.workflow_type.name, headers: from_payload_map(task.header&.fields || {}), - heartbeat_details: from_details_payloads(task.heartbeat_details) + heartbeat_details: from_details_payloads(task.heartbeat_details), + # temporal doesn't render sub-second times, so we ignore the nanos field + scheduled_time: task.scheduled_time.seconds, + current_attempt_scheduled_time: task.current_attempt_scheduled_time.seconds ) end diff --git a/lib/temporal/metadata/activity.rb b/lib/temporal/metadata/activity.rb index ef2b34ef..ae2403c8 100644 --- a/lib/temporal/metadata/activity.rb +++ b/lib/temporal/metadata/activity.rb @@ -3,9 +3,9 @@ module Temporal module Metadata class Activity < Base - attr_reader :namespace, :id, :name, :task_token, :attempt, :workflow_run_id, :workflow_id, :workflow_name, :headers, :heartbeat_details + attr_reader :namespace, :id, :name, :task_token, :attempt, :workflow_run_id, :workflow_id, :workflow_name, :headers, :heartbeat_details, :scheduled_time, :current_attempt_scheduled_time - def initialize(namespace:, id:, name:, task_token:, attempt:, workflow_run_id:, workflow_id:, workflow_name:, headers: {}, heartbeat_details:) + def initialize(namespace:, id:, name:, task_token:, attempt:, workflow_run_id:, workflow_id:, workflow_name:, headers: {}, heartbeat_details:, scheduled_time:, current_attempt_scheduled_time:) @namespace = namespace @id = id @name = name @@ -16,6 +16,8 @@ def initialize(namespace:, id:, name:, task_token:, attempt:, workflow_run_id:, @workflow_name = workflow_name @headers = headers @heartbeat_details = heartbeat_details + @scheduled_time = scheduled_time + @current_attempt_scheduled_time = current_attempt_scheduled_time freeze end @@ -32,7 +34,9 @@ def to_h 'run_id' => workflow_run_id, 'activity_id' => id, 'activity_name' => name, - 'attempt' => attempt + 'attempt' => attempt, + 'scheduled_time' => scheduled_time, + 'current_attempt_scheduled_time' => current_attempt_scheduled_time, } end end diff --git a/lib/temporal/testing/local_workflow_context.rb b/lib/temporal/testing/local_workflow_context.rb index 460945aa..e6f844b0 100644 --- a/lib/temporal/testing/local_workflow_context.rb +++ b/lib/temporal/testing/local_workflow_context.rb @@ -60,7 +60,9 @@ def execute_activity(activity_class, *input, **args) workflow_id: workflow_id, workflow_name: self.metadata.name, headers: execution_options.headers, - heartbeat_details: nil + heartbeat_details: nil, + scheduled_time: Time.now.to_i, + current_attempt_scheduled_time: Time.now.to_i, ) context = LocalActivityContext.new(metadata) @@ -110,7 +112,9 @@ def execute_local_activity(activity_class, *input, **args) workflow_id: workflow_id, workflow_name: self.metadata.name, headers: execution_options.headers, - heartbeat_details: nil + heartbeat_details: nil, + scheduled_time: Time.now.to_i, + current_attempt_scheduled_time: Time.now.to_i, ) context = LocalActivityContext.new(metadata) diff --git a/spec/fabricators/activity_metadata_fabricator.rb b/spec/fabricators/activity_metadata_fabricator.rb index 06409da6..f6bf4e71 100644 --- a/spec/fabricators/activity_metadata_fabricator.rb +++ b/spec/fabricators/activity_metadata_fabricator.rb @@ -11,4 +11,6 @@ workflow_name 'TestWorkflow' headers { {} } heartbeat_details nil + scheduled_time 0 + current_attempt_scheduled_time 0 end diff --git a/spec/fabricators/grpc/activity_task_fabricator.rb b/spec/fabricators/grpc/activity_task_fabricator.rb index b6fc43fc..3dabfb53 100644 --- a/spec/fabricators/grpc/activity_task_fabricator.rb +++ b/spec/fabricators/grpc/activity_task_fabricator.rb @@ -11,6 +11,8 @@ workflow_execution { Fabricate(:api_workflow_execution) } current_attempt_scheduled_time { Google::Protobuf::Timestamp.new.tap { |t| t.from_time(Time.now) } } started_time { Google::Protobuf::Timestamp.new.tap { |t| t.from_time(Time.now) } } + scheduled_time { Google::Protobuf::Timestamp.new.tap { |t| t.from_time(Time.now) } } + current_attempt_scheduled_time { Google::Protobuf::Timestamp.new.tap { |t| t.from_time(Time.now) } } header do |attrs| fields = (attrs[:headers] || {}).each_with_object({}) do |(field, value), h| h[field] = Temporal.configuration.converter.to_payload(value) diff --git a/spec/unit/lib/temporal/metadata/activity_spec.rb b/spec/unit/lib/temporal/metadata/activity_spec.rb index d5729ef6..a2d11275 100644 --- a/spec/unit/lib/temporal/metadata/activity_spec.rb +++ b/spec/unit/lib/temporal/metadata/activity_spec.rb @@ -16,6 +16,8 @@ expect(subject.workflow_name).to eq(args.workflow_name) expect(subject.headers).to eq(args.headers) expect(subject.heartbeat_details).to eq(args.heartbeat_details) + expect(subject.scheduled_time).to eq(args.scheduled_time) + expect(subject.current_attempt_scheduled_time).to eq(args.current_attempt_scheduled_time) end it { is_expected.to be_frozen } @@ -36,7 +38,9 @@ 'namespace' => subject.namespace, 'workflow_id' => subject.workflow_id, 'workflow_name' => subject.workflow_name, - 'run_id' => subject.workflow_run_id + 'run_id' => subject.workflow_run_id, + 'scheduled_time' => subject.scheduled_time, + 'current_attempt_scheduled_time' => subject.current_attempt_scheduled_time, }) end end diff --git a/spec/unit/lib/temporal/testing/temporal_override_spec.rb b/spec/unit/lib/temporal/testing/temporal_override_spec.rb index 0788255f..99e1c0de 100644 --- a/spec/unit/lib/temporal/testing/temporal_override_spec.rb +++ b/spec/unit/lib/temporal/testing/temporal_override_spec.rb @@ -196,6 +196,8 @@ def execute expect(metadata.namespace).to eq('default-namespace') expect(metadata.workflow_id).to eq(workflow_id) expect(metadata.workflow_run_id).to eq(run_id) + expect(metadata.scheduled_time).to be > 0 + expect(metadata.current_attempt_scheduled_time).to be > 0 end describe 'execution control' do