From b0d6b0c5ca7f085459d06856da56568aadfd258f Mon Sep 17 00:00:00 2001 From: Arya Kumar Date: Wed, 16 Mar 2022 13:01:29 -0700 Subject: [PATCH] Add parent_run_id, parent_id to workflow metadata --- examples/spec/integration/parent_close_workflow_spec.rb | 2 +- examples/workflows/slow_child_workflow.rb | 2 +- lib/temporal/metadata.rb | 2 ++ lib/temporal/metadata/workflow.rb | 8 ++++++-- lib/temporal/testing/temporal_override.rb | 2 ++ lib/temporal/testing/workflow_override.rb | 2 ++ spec/fabricators/workflow_metadata_fabricator.rb | 2 ++ spec/unit/lib/temporal/metadata/workflow_spec.rb | 2 ++ .../lib/temporal/testing/local_workflow_context_spec.rb | 2 ++ spec/unit/lib/temporal/workflow/executor_spec.rb | 2 ++ 10 files changed, 22 insertions(+), 4 deletions(-) diff --git a/examples/spec/integration/parent_close_workflow_spec.rb b/examples/spec/integration/parent_close_workflow_spec.rb index e307dbd8..44f9348f 100644 --- a/examples/spec/integration/parent_close_workflow_spec.rb +++ b/examples/spec/integration/parent_close_workflow_spec.rb @@ -50,6 +50,6 @@ workflow_id: child_workflow_id, ) - expect(result).to eq('slow child ran') + expect(result).to eq({ parent_workflow_id: workflow_id }) end end diff --git a/examples/workflows/slow_child_workflow.rb b/examples/workflows/slow_child_workflow.rb index 8de8e3cd..332b9a33 100644 --- a/examples/workflows/slow_child_workflow.rb +++ b/examples/workflows/slow_child_workflow.rb @@ -4,6 +4,6 @@ def execute(delay) workflow.sleep(delay) end - return 'slow child ran' + return { parent_workflow_id: workflow.metadata.parent_id } end end diff --git a/lib/temporal/metadata.rb b/lib/temporal/metadata.rb index 4bcfade8..f5649672 100644 --- a/lib/temporal/metadata.rb +++ b/lib/temporal/metadata.rb @@ -47,6 +47,8 @@ def generate_workflow_metadata(event, task_metadata) name: event.attributes.workflow_type.name, id: task_metadata.workflow_id, run_id: event.attributes.original_execution_run_id, + parent_id: event.attributes.parent_workflow_execution&.workflow_id, + parent_run_id: event.attributes.parent_workflow_execution&.run_id, attempt: event.attributes.attempt, namespace: task_metadata.namespace, task_queue: event.attributes.task_queue.name, diff --git a/lib/temporal/metadata/workflow.rb b/lib/temporal/metadata/workflow.rb index f4715dde..e912391d 100644 --- a/lib/temporal/metadata/workflow.rb +++ b/lib/temporal/metadata/workflow.rb @@ -3,13 +3,15 @@ module Temporal module Metadata class Workflow < Base - attr_reader :namespace, :id, :name, :run_id, :attempt, :task_queue, :headers, :run_started_at, :memo + attr_reader :namespace, :id, :name, :run_id, :parent_id, :parent_run_id, :attempt, :task_queue, :headers, :run_started_at, :memo - def initialize(namespace:, id:, name:, run_id:, attempt:, task_queue:, headers:, run_started_at:, memo:) + def initialize(namespace:, id:, name:, run_id:, parent_id:, parent_run_id:, attempt:, task_queue:, headers:, run_started_at:, memo:) @namespace = namespace @id = id @name = name @run_id = run_id + @parent_id = parent_id + @parent_run_id = parent_run_id @attempt = attempt @task_queue = task_queue @headers = headers @@ -29,6 +31,8 @@ def to_h 'workflow_id' => id, 'workflow_name' => name, 'workflow_run_id' => run_id, + 'parent_workflow_id' => parent_id, + 'parent_workflow_run_id' => parent_run_id, 'attempt' => attempt, 'task_queue' => task_queue, 'run_started_at' => run_started_at.to_f, diff --git a/lib/temporal/testing/temporal_override.rb b/lib/temporal/testing/temporal_override.rb index 1e76cce7..1fae6c36 100644 --- a/lib/temporal/testing/temporal_override.rb +++ b/lib/temporal/testing/temporal_override.rb @@ -100,6 +100,8 @@ def start_locally(workflow, schedule, *input, **args) id: workflow_id, name: execution_options.name, run_id: run_id, + parent_id: nil, + parent_run_id: nil, attempt: 1, task_queue: execution_options.task_queue, run_started_at: Time.now, diff --git a/lib/temporal/testing/workflow_override.rb b/lib/temporal/testing/workflow_override.rb index 9d43f953..45e989d4 100644 --- a/lib/temporal/testing/workflow_override.rb +++ b/lib/temporal/testing/workflow_override.rb @@ -32,6 +32,8 @@ def execute_locally(*input) id: workflow_id, name: name, # Workflow class name run_id: run_id, + parent_id: nil, + parent_run_id: nil, attempt: 1, task_queue: 'unit-test-task-queue', headers: {}, diff --git a/spec/fabricators/workflow_metadata_fabricator.rb b/spec/fabricators/workflow_metadata_fabricator.rb index c32fd3e1..3f3a1b7a 100644 --- a/spec/fabricators/workflow_metadata_fabricator.rb +++ b/spec/fabricators/workflow_metadata_fabricator.rb @@ -5,6 +5,8 @@ id { SecureRandom.uuid } name 'TestWorkflow' run_id { SecureRandom.uuid } + parent_id { nil } + parent_run_id { nil } attempt 1 task_queue { Fabricate(:api_task_queue) } run_started_at { Time.now } diff --git a/spec/unit/lib/temporal/metadata/workflow_spec.rb b/spec/unit/lib/temporal/metadata/workflow_spec.rb index be3f50b9..0ad4af48 100644 --- a/spec/unit/lib/temporal/metadata/workflow_spec.rb +++ b/spec/unit/lib/temporal/metadata/workflow_spec.rb @@ -32,6 +32,8 @@ 'attempt' => subject.attempt, 'workflow_name' => subject.name, 'workflow_run_id' => subject.run_id, + 'parent_workflow_id' => nil, + 'parent_workflow_run_id' => nil, 'task_queue' => subject.task_queue, 'run_started_at' => subject.run_started_at.to_f, 'memo' => subject.memo, diff --git a/spec/unit/lib/temporal/testing/local_workflow_context_spec.rb b/spec/unit/lib/temporal/testing/local_workflow_context_spec.rb index a62d866a..ae8eca8c 100644 --- a/spec/unit/lib/temporal/testing/local_workflow_context_spec.rb +++ b/spec/unit/lib/temporal/testing/local_workflow_context_spec.rb @@ -19,6 +19,8 @@ id: workflow_id, name: 'HelloWorldWorkflow', run_id: run_id, + parent_id: nil, + parent_run_id: nil, attempt: 1, task_queue: task_queue, headers: {}, diff --git a/spec/unit/lib/temporal/workflow/executor_spec.rb b/spec/unit/lib/temporal/workflow/executor_spec.rb index a74c3387..bc9828da 100644 --- a/spec/unit/lib/temporal/workflow/executor_spec.rb +++ b/spec/unit/lib/temporal/workflow/executor_spec.rb @@ -70,6 +70,8 @@ def execute id: workflow_metadata.workflow_id, name: event_attributes.workflow_type.name, run_id: event_attributes.original_execution_run_id, + parent_id: nil, + parent_run_id: nil, attempt: event_attributes.attempt, task_queue: event_attributes.task_queue.name, run_started_at: workflow_started_event.event_time.to_time,