Skip to content
Open
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
7 changes: 5 additions & 2 deletions examples/spec/integration/start_workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@
end

it 'returns the running execution for :use_existing without delivering the new input', :workflow_id_conflict_policy do
outcomes = []
options = { workflow_id: workflow_id, workflow_id_conflict_policy: :use_existing }
original_run_id = Temporal.start_workflow(WaitForExternalSignalWorkflow, 'original',
options: { workflow_id: workflow_id })
options: options) { |started| outcomes << started }

expect do
Temporal.start_workflow(WaitForExternalSignalWorkflow, 'replacement', options: { workflow_id: workflow_id })
end.to raise_error(Temporal::WorkflowExecutionAlreadyStartedFailure)

returned_run_id = Temporal.start_workflow(WaitForExternalSignalWorkflow, 'replacement',
options: { workflow_id: workflow_id, workflow_id_conflict_policy: :use_existing })
options: options) { |started| outcomes << started }

expect(outcomes).to eq([true, false])
expect(returned_run_id).to eq(original_run_id)
started = fetch_history(workflow_id, original_run_id).history.events.find do |event|
event.event_type == :EVENT_TYPE_WORKFLOW_EXECUTION_STARTED
Expand Down

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions lib/temporal/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def initialize(config)
# @option options [Hash] :search_attributes
# @option options [Integer] :start_delay determines the amount of seconds to wait before initiating a Workflow
#
# @yield [Boolean] for :use_existing, whether this request started a new run.
# Requires a server that supports the conflict policy and reports the start outcome.
# @return [String] workflow's run ID
def start_workflow(workflow, *input, options: {}, **args)
input << args unless args.empty?
Expand Down Expand Up @@ -99,6 +101,7 @@ def start_workflow(workflow, *input, options: {}, **args)
)
end

yield response.started if block_given? && options[:workflow_id_conflict_policy] == :use_existing
response.run_id
end

Expand Down
18 changes: 17 additions & 1 deletion scripts/backport_workflow_id_conflict_policy.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# The checked-in Temporal API bindings predate the workflow ID conflict policy.
# Apply only the two additive descriptor changes needed for ordinary starts.
# Apply only the additive descriptor changes needed for ordinary starts.

proto_out = ARGV[0] || File.expand_path('../lib/gen', __dir__)
enum_path = File.join(proto_out, 'temporal/api/enums/v1/workflow_pb.rb')
Expand Down Expand Up @@ -42,3 +42,19 @@ def replace_once(path, before, after, marker)
) or raise 'Unexpected StartWorkflowExecutionRequest fields'
File.write(request_path, request_source)
end

# Inspect the descriptor itself: another backport may already provide the field
# and may render the generated Ruby string literal with different escapes.
require 'google/protobuf/descriptor_pb'
source = File.read(request_path)
line = source.lines.find { |l| l.start_with?('descriptor_data = ') } or raise 'Missing descriptor'
descriptor = Google::Protobuf::FileDescriptorProto.decode(eval(line.delete_prefix('descriptor_data = ')))
response = descriptor.message_type.find { |msg| msg.name == 'StartWorkflowExecutionResponse' }
started = Google::Protobuf::FieldDescriptorProto.new(name: 'started', number: 3, type: :TYPE_BOOL, label: :LABEL_OPTIONAL)
existing = response.field.find { |f| f.number == 3 || f.name == 'started' }
raise 'Conflicting StartWorkflowExecutionResponse.started field' if existing && existing != started
unless existing
response.field << started
encoded = Google::Protobuf::FileDescriptorProto.encode(descriptor)
File.write(request_path, source.sub(line) { "descriptor_data = #{encoded.dump}\n" })
end
Loading