Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/temporal/connection/serializer/parent_close_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'temporal/connection/serializer/base'

module Temporal
module Connection
module Serializer
class ParentClosePolicy < Base
def to_proto
return Temporal::Api::Enums::V1::ParentClosePolicy::PARENT_CLOSE_POLICY_UNSPECIFIED unless object
mapping = {
abandon: Temporal::Api::Enums::V1::ParentClosePolicy::PARENT_CLOSE_POLICY_ABANDON,
terminate: Temporal::Api::Enums::V1::ParentClosePolicy::PARENT_CLOSE_POLICY_TERMINATE,
request_cancel: Temporal::Api::Enums::V1::ParentClosePolicy::PARENT_CLOSE_POLICY_REQUEST_CANCEL,
}.compact

mapping[object.policy]
end
end
end
end
end
2 changes: 2 additions & 0 deletions lib/temporal/connection/serializer/start_child_workflow.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'temporal/connection/serializer/base'
require 'temporal/connection/serializer/retry_policy'
require 'temporal/connection/serializer/parent_close_policy'
require 'temporal/concerns/payloads'

module Temporal
Expand All @@ -22,6 +23,7 @@ def to_proto
workflow_run_timeout: object.timeouts[:run],
workflow_task_timeout: object.timeouts[:task],
retry_policy: Temporal::Connection::Serializer::RetryPolicy.new(object.retry_policy).to_proto,
parent_close_policy: Temporal::Connection::Serializer::ParentClosePolicy.new(object.parent_close_policy).to_proto,
header: serialize_headers(object.headers)
)
)
Expand Down
11 changes: 10 additions & 1 deletion lib/temporal/execution_options.rb
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
require 'temporal/concerns/executable'
require 'temporal/retry_policy'
require 'temporal/parent_close_policy'

module Temporal
class ExecutionOptions
attr_reader :name, :namespace, :task_queue, :retry_policy, :timeouts, :headers
attr_reader :name, :namespace, :task_queue, :retry_policy, :parent_close_policy, :timeouts, :headers

def initialize(object, options, defaults = nil)
# Options are treated as overrides and take precedence
@name = options[:name] || object.to_s
@namespace = options[:namespace]
@task_queue = options[:task_queue] || options[:task_list]
@retry_policy = options[:retry_policy] || {}
@parent_close_policy = options[:parent_close_policy] || {}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ExecutionOptions are shared between workflows and activities, so this wouldn't be the best place to add workflow-specific options. Can you please move these inline with the child workflow invocation?

@timeouts = options[:timeouts] || {}
@headers = options[:headers] || {}

Expand Down Expand Up @@ -38,6 +40,13 @@ def initialize(object, options, defaults = nil)
@retry_policy.validate!
end

if @parent_close_policy.empty?
@parent_close_policy = nil
else
@parent_close_policy = Temporal::ParentClosePolicy.new(@parent_close_policy)
@parent_close_policy.validate!
end

freeze
end

Expand Down
20 changes: 20 additions & 0 deletions lib/temporal/parent_close_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
require 'temporal/errors'

module Temporal
# See https://docs.temporal.io/docs/content/what-is-a-parent-close-policy/ for more information
class ParentClosePolicy
class InvalidParentClosePolicy < ClientError; end

attr_reader :policy

def initialize(policy)
@policy = policy
end

def validate!
unless %i[terminate abandon request_cancel].include?(@policy)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please define these as constants so they can be used without hardcoding symbols?

raise InvalidParentClosePolicy, 'Invalid parent close policy, only :abandon, :terminate, :request_cancel are allowed'
end
end
end
end
2 changes: 1 addition & 1 deletion lib/temporal/workflow/command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class Workflow
module Command
# TODO: Move these classes into their own directories under workflow/command/*
ScheduleActivity = Struct.new(:activity_type, :activity_id, :input, :namespace, :task_queue, :retry_policy, :timeouts, :headers, keyword_init: true)
StartChildWorkflow = Struct.new(:workflow_type, :workflow_id, :input, :namespace, :task_queue, :retry_policy, :timeouts, :headers, keyword_init: true)
StartChildWorkflow = Struct.new(:workflow_type, :workflow_id, :input, :namespace, :task_queue, :retry_policy, :parent_close_policy, :timeouts, :headers, keyword_init: true)
ContinueAsNew = Struct.new(:workflow_type, :task_queue, :input, :timeouts, :retry_policy, :headers, keyword_init: true)
RequestActivityCancellation = Struct.new(:activity_id, keyword_init: true)
RecordMarker = Struct.new(:name, :details, keyword_init: true)
Expand Down
1 change: 1 addition & 0 deletions lib/temporal/workflow/context.rb
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ def execute_workflow(workflow_class, *input, **args)
namespace: execution_options.namespace,
task_queue: execution_options.task_queue,
retry_policy: execution_options.retry_policy,
parent_close_policy: execution_options.parent_close_policy,
timeouts: execution_options.timeouts,
headers: execution_options.headers
)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'temporal/parent_close_policy'
require 'temporal/connection/serializer/parent_close_policy'

describe Temporal::Connection::Serializer::ParentClosePolicy do
describe 'to_proto' do
let(:terminate_policy) { Temporal::ParentClosePolicy.new(:terminate) }
let(:abandon_policy) { Temporal::ParentClosePolicy.new(:abandon) }
let(:request_cancel_policy) { Temporal::ParentClosePolicy.new(:request_cancel) }

it 'converts to proto' do
expect do
described_class.new(terminate_policy).to_proto
end.not_to raise_error

expect do
described_class.new(abandon_policy).to_proto
end.not_to raise_error

expect do
described_class.new(request_cancel_policy).to_proto
end.not_to raise_error
end
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please split these into 3 examples? It's easier to figure out which ones have failed that way

end
end
27 changes: 27 additions & 0 deletions spec/unit/lib/temporal/parent_close_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'temporal/parent_close_policy'

describe Temporal::ParentClosePolicy do
describe '#validate!' do
subject { described_class.new(param) }

context 'with valid param' do
%i[abandon terminate request_cancel].each do |policy|
context "with #{policy}" do
let(:param) { policy }

it 'does not raise' do
expect { subject.validate! }.not_to raise_error
end
end
end
end

context 'with invalid param' do
let(:param) { :coinbase }

it 'does not raise' do
expect { subject.validate! }.to raise_error(described_class::InvalidParentClosePolicy)
end
end
end
end