Skip to content
Merged
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
23 changes: 20 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,28 @@ jobs:
- name: Test protobuf coexistence
run: bundle exec rspec spec/unit/lib/temporal/protobuf_loading_spec.rb

test_examples:
test_examples_matrix:
name: test_examples (${{ matrix.server }})
runs-on: ubuntu-latest

strategy:
fail-fast: false
matrix:
include:
- server: legacy
compose_file: examples/docker-compose.yml
rspec_args: "--tag ~workflow_id_conflict_policy"
- server: conflict-policy
compose_file: examples/docker-compose-conflict-policy.yml
rspec_args: spec/integration/start_workflow_spec.rb

steps:
- uses: actions/checkout@v3

- name: Start dependencies
run: |
docker compose \
-f examples/docker-compose.yml \
-f ${{ matrix.compose_file }} \
up -d

- name: Set up Ruby
Expand Down Expand Up @@ -96,4 +105,12 @@ jobs:
env:
USE_ERROR_SERIALIZATION_V2: 1
run: |
cd examples && bundle exec rspec
cd examples && bundle exec rspec ${{ matrix.rspec_args }}

test_examples:
if: always()
needs: test_examples_matrix
runs-on: ubuntu-latest
steps:
- name: Require both example configurations to pass
run: test "${{ needs.test_examples_matrix.result }}" = "success"
3 changes: 2 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ PROTO_DIRS = $(sort $(dir $(PROTO_FILES)))
PROTO_OUT := lib/gen

proto:
$(foreach PROTO_DIR,$(PROTO_DIRS),bundle exec grpc_tools_ruby_protoc -Iproto --ruby_out=$(PROTO_OUT) --grpc_out=$(PROTO_OUT) $(PROTO_DIR)*.proto;)
@set -e; $(foreach PROTO_DIR,$(PROTO_DIRS),bundle exec grpc_tools_ruby_protoc -Iproto --ruby_out=$(PROTO_OUT) --grpc_out=$(PROTO_OUT) $(PROTO_DIR)*.proto;)
@for script in scripts/backport_*.rb; do bundle exec ruby "$$script" "$(PROTO_OUT)" || exit $$?; done
sed -i "/^require 'temporal\// s/$$/ unless ENV['COINBASE_TEMPORAL_RUBY_DISABLE_PROTO_LOAD'] == '1'/" $(PROTO_OUT)/temporal/api/workflowservice/v1/service_services_pb.rb $(PROTO_OUT)/temporal/api/operatorservice/v1/service_services_pb.rb

.PHONY: proto
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,19 @@ workflows by supplying the `workflow_id_reuse_policy:` argument with one of thes
- `:allow` will allow re-running any finished workflows both failed and completed
- `:reject` will reject any subsequent attempt to run a workflow

For an **already running** workflow, `Temporal.start_workflow` can instead return its existing run ID:

```ruby
run_id = Temporal.start_workflow(RenewSubscriptionWorkflow, user_id,
options: { workflow_id: user_id, workflow_id_conflict_policy: :use_existing })
```

This is opt-in. The existing workflow keeps its original input; arguments in the second start call
are not delivered to it. The default remains to raise `Temporal::WorkflowExecutionAlreadyStartedFailure`.
The separate `workflow_id_reuse_policy` controls what happens after the prior run closes.
`workflow_id_conflict_policy` is supported only for ordinary `start_workflow` calls, not
signal-with-start, `schedule_workflow`, or `Temporal::Testing.local!`.


## Execution Options

Expand Down Expand Up @@ -602,3 +615,15 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

### Workflow conflict-policy integration tests

The legacy example suite uses Temporal 1.22. Run it with
`bundle exec rspec --tag ~workflow_id_conflict_policy` from `examples/`.
The running-workflow `USE_EXISTING` case requires a newer server: use
`docker compose -f examples/docker-compose-conflict-policy.yml up -d`
instead of the legacy Compose stack, then follow the same namespace/worker
setup and run `bundle exec rspec spec/integration/start_workflow_spec.rb`
from `examples/`. Stop the first stack before starting the other; both use
port 7233. CI runs both configurations, including all six workflow-start cases
against Temporal 1.29.1 with PostgreSQL visibility.
22 changes: 22 additions & 0 deletions examples/docker-compose-conflict-policy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
services:
temporal:
image: temporalio/auto-setup:1.29.1
ports:
- "7233:7233"
environment:
- "DB=postgres12"
- "DB_PORT=5432"
- "POSTGRES_USER=temporal"
- "POSTGRES_PWD=temporal"
- "POSTGRES_SEEDS=postgresql"
- "DYNAMIC_CONFIG_FILE_PATH=/etc/temporal/config/dynamicconfig/temporal-ruby.yaml"
volumes:
- ./dynamic-config.yml:/etc/temporal/config/dynamicconfig/temporal-ruby.yaml
depends_on:
- postgresql

postgresql:
image: postgres:16
environment:
POSTGRES_USER: temporal
POSTGRES_PASSWORD: temporal
53 changes: 53 additions & 0 deletions examples/spec/integration/start_workflow_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
require 'workflows/hello_world_workflow'
require 'workflows/long_workflow'
require 'workflows/wait_for_external_signal_workflow'

describe 'Temporal.start_workflow', :integration do
let(:workflow_id) { SecureRandom.uuid }
Expand Down Expand Up @@ -35,6 +36,50 @@
expect(result).to eq('Hello World, Test')
end

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

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 })

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
end
expect(started.workflow_execution_started_event_attributes.input.payloads.map(&:data)).to eq(['"original"'])

Temporal.signal_workflow(WaitForExternalSignalWorkflow, 'original', workflow_id, original_run_id, 'received')
result = Temporal.await_workflow_result(WaitForExternalSignalWorkflow,
workflow_id: workflow_id, run_id: original_run_id)
expect(result).to eq(received: { 'original' => 'received' }, counts: { 'original' => 1 })
end

it 'uses the reuse policy after the previous run closes' do
original_run_id = Temporal.start_workflow(LongWorkflow,
options: { workflow_id: workflow_id })
Temporal.terminate_workflow(workflow_id, run_id: original_run_id)

expect do
Temporal.start_workflow(LongWorkflow, options: {
workflow_id: workflow_id, workflow_id_reuse_policy: :reject,
workflow_id_conflict_policy: :use_existing
})
end.to raise_error(Temporal::WorkflowExecutionAlreadyStartedFailure)

new_run_id = Temporal.start_workflow(LongWorkflow, options: {
workflow_id: workflow_id, workflow_id_reuse_policy: :allow,
workflow_id_conflict_policy: :use_existing
})
expect(new_run_id).not_to eq(original_run_id)
ensure
Temporal.terminate_workflow(workflow_id, run_id: new_run_id) if new_run_id
end

it 'rejects duplicate workflow ids based on workflow_id_reuse_policy' do
# Run it once...
run_id = Temporal.start_workflow(HelloWorldWorkflow, 'Test', options: {
Expand All @@ -49,6 +94,14 @@

expect(result).to eq('Hello World, Test')

expect do
Temporal.start_workflow(HelloWorldWorkflow, 'Test', options: {
workflow_id: workflow_id,
workflow_id_reuse_policy: :allow_failed,
workflow_id_conflict_policy: :use_existing
})
end.to raise_error(Temporal::WorkflowExecutionAlreadyStartedFailure)

# And again, allowing duplicates...
run_id = Temporal.start_workflow(HelloWorldWorkflow, 'Test', options: {
workflow_id: workflow_id,
Expand Down
3 changes: 2 additions & 1 deletion lib/gen/temporal/api/enums/v1/workflow_pb.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions lib/temporal/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ def initialize(config)
# @param options [Hash, nil] optional overrides
# @option options [String] :workflow_id
# @option options [Symbol] :workflow_id_reuse_policy check Temporal::Connection::GRPC::WORKFLOW_ID_REUSE_POLICY
# @option options [Symbol] :workflow_id_conflict_policy :use_existing returns the running workflow's run ID without delivering new input
# @option options [String] :name workflow name
# @option options [String] :namespace
# @option options [String] :task_queue
Expand All @@ -49,6 +50,9 @@ def start_workflow(workflow, *input, options: {}, **args)

signal_name = options.delete(:signal_name)
signal_input = options.delete(:signal_input)
if (!signal_name.nil? || !signal_input.nil?) && options.key?(:workflow_id_conflict_policy)
raise ArgumentError, 'workflow_id_conflict_policy is not supported with signal-with-start'
end

execution_options = ExecutionOptions.new(workflow, options, config.default_execution_options)
workflow_id = options[:workflow_id] || SecureRandom.uuid
Expand All @@ -65,6 +69,7 @@ def start_workflow(workflow, *input, options: {}, **args)
run_timeout: compute_run_timeout(execution_options),
task_timeout: execution_options.timeouts[:task],
workflow_id_reuse_policy: options[:workflow_id_reuse_policy],
workflow_id_conflict_policy: options[:workflow_id_conflict_policy],
headers: config.header_propagator_chain.inject(execution_options.headers),
memo: execution_options.memo,
search_attributes: Workflow::Context::Helpers.process_search_attributes(execution_options.search_attributes),
Expand Down Expand Up @@ -117,6 +122,9 @@ def start_workflow(workflow, *input, options: {}, **args)
#
# @return [String] workflow's run ID
def schedule_workflow(workflow, cron_schedule, *input, options: {}, **args)
if options.key?(:workflow_id_conflict_policy)
raise ArgumentError, 'workflow_id_conflict_policy is not supported with schedule_workflow'
end
input << args unless args.empty?

execution_options = ExecutionOptions.new(workflow, options, config.default_execution_options)
Expand Down
8 changes: 8 additions & 0 deletions lib/temporal/connection/grpc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
require 'temporal/connection/serializer/backfill'
require 'temporal/connection/serializer/schedule'
require 'temporal/connection/serializer/workflow_id_reuse_policy'
require 'temporal/connection/serializer/workflow_id_conflict_policy'
require 'temporal/connection/serializer/retry_policy'

module Temporal
Expand Down Expand Up @@ -122,13 +123,19 @@ def start_workflow_execution(
task_timeout:,
input: nil,
workflow_id_reuse_policy: nil,
workflow_id_conflict_policy: nil,
headers: nil,
cron_schedule: nil,
memo: nil,
search_attributes: nil,
start_delay: nil,
retry_policy: nil
)
if !workflow_id_conflict_policy.nil? && workflow_id_reuse_policy == :terminate_if_running
raise Temporal::Connection::ArgumentError,
'workflow_id_conflict_policy cannot be combined with workflow_id_reuse_policy :terminate_if_running'
end

request = Temporalio::Api::WorkflowService::V1::StartWorkflowExecutionRequest.new(
identity: identity,
namespace: namespace,
Expand All @@ -137,6 +144,7 @@ def start_workflow_execution(
),
workflow_id: workflow_id,
workflow_id_reuse_policy: Temporal::Connection::Serializer::WorkflowIdReusePolicy.new(workflow_id_reuse_policy, converter).to_proto,
workflow_id_conflict_policy: Temporal::Connection::Serializer::WorkflowIdConflictPolicy.new(workflow_id_conflict_policy, converter).to_proto,
task_queue: Temporalio::Api::TaskQueue::V1::TaskQueue.new(
name: task_queue
),
Expand Down
23 changes: 23 additions & 0 deletions lib/temporal/connection/serializer/workflow_id_conflict_policy.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
require 'temporal/connection'

module Temporal
module Connection
module Serializer
class WorkflowIdConflictPolicy < Base
WORKFLOW_ID_CONFLICT_POLICY = {
fail: Temporalio::Api::Enums::V1::WorkflowIdConflictPolicy::WORKFLOW_ID_CONFLICT_POLICY_FAIL,
use_existing: Temporalio::Api::Enums::V1::WorkflowIdConflictPolicy::WORKFLOW_ID_CONFLICT_POLICY_USE_EXISTING
}.freeze

def to_proto
return if object.nil?

policy = WORKFLOW_ID_CONFLICT_POLICY[object]
raise ArgumentError, "Unknown workflow_id_conflict_policy specified: #{object}" unless policy

policy
end
end
end
end
end
4 changes: 4 additions & 0 deletions lib/temporal/testing/temporal_override.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def start_locally(workflow, schedule, *input, **args)
options = args.delete(:options) || {}
input << args unless args.empty?

if options.key?(:workflow_id_conflict_policy)
raise NotImplementedError, 'workflow_id_conflict_policy is not available when Temporal::Testing.local! is on'
end

# signals aren't supported at all, so let's prohibit start_workflow calls that try to signal
signal_name = options.delete(:signal_name)
signal_input = options.delete(:signal_input)
Expand Down
Loading
Loading