Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replicate official SDK to fetch results from Temporal #305

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
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
39 changes: 22 additions & 17 deletions lib/temporal/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,25 +229,30 @@ def await_workflow_result(workflow, workflow_id:, run_id: nil, timeout: nil, nam
execution_options = ExecutionOptions.new(workflow, options, config.default_execution_options)
max_timeout = Temporal::Connection::GRPC::SERVER_MAX_GET_WORKFLOW_EXECUTION_HISTORY_POLL
history_response = nil
begin
history_response = connection.get_workflow_execution_history(
namespace: execution_options.namespace,
workflow_id: workflow_id,
run_id: run_id,
wait_for_new_event: true,
event_type: :close,
timeout: timeout || max_timeout,
)
rescue GRPC::DeadlineExceeded => e
message = if timeout
"Timed out after your specified limit of timeout: #{timeout} seconds"
else
"Timed out after #{max_timeout} seconds, which is the maximum supported amount."
closed_event = nil
loop do
begin
history_response = connection.get_workflow_execution_history(
namespace: execution_options.namespace,
workflow_id: workflow_id,
run_id: run_id,
wait_for_new_event: true,
event_type: :close,
timeout: timeout || max_timeout,
)
rescue GRPC::DeadlineExceeded => e
message = if timeout
"Timed out after your specified limit of timeout: #{timeout} seconds"
else
"Timed out after #{max_timeout} seconds, which is the maximum supported amount."
end
raise TimeoutError.new(message)
end
raise TimeoutError.new(message)
history = Workflow::History.new(history_response.history.events)
closed_event = history.events.first

break if closed_event
end
history = Workflow::History.new(history_response.history.events)
closed_event = history.events.first
case closed_event.type
when 'WORKFLOW_EXECUTION_COMPLETED'
payloads = closed_event.attributes.result
Expand Down
25 changes: 25 additions & 0 deletions spec/unit/lib/temporal/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,31 @@ class NamespacedWorkflow < Temporal::Workflow
)
end

it 'retries if there is till there is no closed event' do
completed_event = Fabricate(:workflow_completed_event, result: nil)
response_with_no_closed_event = Fabricate(:workflow_execution_history, events: [])
response_with_closed_event = Fabricate(:workflow_execution_history, events: [completed_event])

expect(connection)
.to receive(:get_workflow_execution_history)
.with(
namespace: 'some-namespace',
workflow_id: workflow_id,
run_id: run_id,
wait_for_new_event: true,
event_type: :close,
timeout: 30,
)
.and_return(response_with_no_closed_event, response_with_closed_event)


subject.await_workflow_result(
NamespacedWorkflow,
workflow_id: workflow_id,
run_id: run_id,
)
end

it 'can override the namespace' do
completed_event = Fabricate(:workflow_completed_event, result: nil)
response = Fabricate(:workflow_execution_history, events: [completed_event])
Expand Down