Skip to content

Commit 0c9a0c7

Browse files
authoredJul 22, 2024··
Add pagination to get_workflow_history (#290)
* Add pagination to get_workflow_history * Fix CI
1 parent dc937e8 commit 0c9a0c7

File tree

2 files changed

+54
-18
lines changed

2 files changed

+54
-18
lines changed
 

‎lib/temporal/client.rb

+19-9
Original file line numberDiff line numberDiff line change
@@ -400,13 +400,23 @@ def fail_activity(async_token, exception)
400400
#
401401
# @return [Temporal::Workflow::History] workflow's execution history
402402
def get_workflow_history(namespace: nil, workflow_id:, run_id:)
403-
history_response = connection.get_workflow_execution_history(
404-
namespace: namespace || config.default_execution_options.namespace,
405-
workflow_id: workflow_id,
406-
run_id: run_id
407-
)
403+
next_page_token = nil
404+
events = []
405+
loop do
406+
response =
407+
connection.get_workflow_execution_history(
408+
namespace: namespace || config.default_execution_options.namespace,
409+
workflow_id: workflow_id,
410+
run_id: run_id,
411+
next_page_token: next_page_token,
412+
)
413+
events.concat(response.history.events.to_a)
414+
next_page_token = response.next_page_token
415+
416+
break if next_page_token.empty?
417+
end
408418

409-
Workflow::History.new(history_response.history.events)
419+
Workflow::History.new(events)
410420
end
411421

412422
# Fetch workflow's execution history as JSON. This output can be used for replay testing.
@@ -459,12 +469,12 @@ def list_closed_workflow_executions(namespace, from, to = Time.now, filter: {},
459469

460470
def query_workflow_executions(namespace, query, filter: {}, next_page_token: nil, max_page_size: nil)
461471
validate_filter(filter, :status, :workflow, :workflow_id)
462-
472+
463473
Temporal::Workflow::Executions.new(connection: connection, status: :all, request_options: { namespace: namespace, query: query, next_page_token: next_page_token, max_page_size: max_page_size }.merge(filter))
464474
end
465475

466476
# Count the number of workflows matching the provided query
467-
#
477+
#
468478
# @param namespace [String]
469479
# @param query [String]
470480
#
@@ -500,7 +510,7 @@ def remove_custom_search_attributes(*attribute_names, namespace: nil)
500510
def list_schedules(namespace, maximum_page_size:, next_page_token: '')
501511
connection.list_schedules(namespace: namespace, maximum_page_size: maximum_page_size, next_page_token: next_page_token)
502512
end
503-
513+
504514
# Describe a schedule in a namespace
505515
#
506516
# @param namespace [String] namespace to list schedules in

‎spec/unit/lib/temporal/client_spec.rb

+35-9
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ def expect_signal_with_start(expected_arguments, expected_signal_argument)
300300
it 'raises when signal_input is given but signal_name is not' do
301301
expect do
302302
subject.start_workflow(
303-
TestStartWorkflow,
303+
TestStartWorkflow,
304304
[42, 54],
305305
[43, 55],
306306
options: { signal_input: 'what do you get if you multiply six by nine?', }
@@ -361,7 +361,7 @@ def expect_signal_with_start(expected_arguments, expected_signal_argument)
361361

362362
describe '#describe_namespace' do
363363
before { allow(connection).to receive(:describe_namespace).and_return(Temporalio::Api::WorkflowService::V1::DescribeNamespaceResponse.new) }
364-
364+
365365
it 'passes the namespace to the connection' do
366366
result = subject.describe_namespace('new-namespace')
367367

@@ -381,7 +381,7 @@ def expect_signal_with_start(expected_arguments, expected_signal_argument)
381381
.to have_received(:signal_workflow_execution)
382382
.with(
383383
namespace: 'default-test-namespace',
384-
signal: 'signal',
384+
signal: 'signal',
385385
workflow_id: 'workflow_id',
386386
run_id: 'run_id',
387387
input: nil,
@@ -395,7 +395,7 @@ def expect_signal_with_start(expected_arguments, expected_signal_argument)
395395
.to have_received(:signal_workflow_execution)
396396
.with(
397397
namespace: 'default-test-namespace',
398-
signal: 'signal',
398+
signal: 'signal',
399399
workflow_id: 'workflow_id',
400400
run_id: 'run_id',
401401
input: 'input',
@@ -409,7 +409,7 @@ def expect_signal_with_start(expected_arguments, expected_signal_argument)
409409
.to have_received(:signal_workflow_execution)
410410
.with(
411411
namespace: 'other-test-namespace',
412-
signal: 'signal',
412+
signal: 'signal',
413413
workflow_id: 'workflow_id',
414414
run_id: 'run_id',
415415
input: nil,
@@ -449,7 +449,7 @@ class NamespacedWorkflow < Temporal::Workflow
449449
)
450450
end
451451

452-
it 'can override the namespace' do
452+
it 'can override the namespace' do
453453
completed_event = Fabricate(:workflow_completed_event, result: nil)
454454
response = Fabricate(:workflow_execution_history, events: [completed_event])
455455

@@ -534,7 +534,7 @@ class NamespacedWorkflow < Temporal::Workflow
534534
end.to raise_error(Temporal::WorkflowCanceled)
535535
end
536536

537-
it 'raises TimeoutError when the server times out' do
537+
it 'raises TimeoutError when the server times out' do
538538
response = Fabricate(:workflow_execution_history, events: [])
539539
expect(connection)
540540
.to receive(:get_workflow_execution_history)
@@ -895,6 +895,32 @@ class NamespacedWorkflow < Temporal::Workflow
895895
end
896896
end
897897

898+
describe '#get_workflow_history' do
899+
it 'gets full history with pagination' do
900+
completed_event = Fabricate(:workflow_completed_event, result: nil)
901+
response_1 = Fabricate(:workflow_execution_history, events: [completed_event], next_page_token: 'a')
902+
response_2 = Fabricate(:workflow_execution_history, events: [completed_event], next_page_token: '')
903+
904+
allow(connection)
905+
.to receive(:get_workflow_execution_history)
906+
.and_return(response_1, response_2)
907+
908+
subject.get_workflow_history(namespace: namespace, workflow_id: workflow_id, run_id: run_id)
909+
910+
expect(connection)
911+
.to have_received(:get_workflow_execution_history)
912+
.with(namespace: namespace, workflow_id: workflow_id, run_id: run_id, next_page_token: nil)
913+
.ordered
914+
915+
expect(connection)
916+
.to have_received(:get_workflow_execution_history)
917+
.with(namespace: namespace, workflow_id: workflow_id, run_id: run_id, next_page_token: 'a')
918+
.ordered
919+
920+
expect(connection).to have_received(:get_workflow_execution_history).exactly(2).times
921+
end
922+
end
923+
898924
describe '#list_open_workflow_executions' do
899925
let(:from) { Time.now - 600 }
900926
let(:now) { Time.now }
@@ -977,7 +1003,7 @@ class NamespacedWorkflow < Temporal::Workflow
9771003
end
9781004
end
9791005

980-
it 'returns the next page token and paginates correctly' do
1006+
it 'returns the next page token and paginates correctly' do
9811007
executions1 = subject.list_open_workflow_executions(namespace, from, max_page_size: 10)
9821008
executions1.map do |execution|
9831009
expect(execution).to be_an_instance_of(Temporal::Workflow::ExecutionInfo)
@@ -1009,7 +1035,7 @@ class NamespacedWorkflow < Temporal::Workflow
10091035
.once
10101036
end
10111037

1012-
it 'returns the next page and paginates correctly' do
1038+
it 'returns the next page and paginates correctly' do
10131039
executions1 = subject.list_open_workflow_executions(namespace, from, max_page_size: 10)
10141040
executions1.map do |execution|
10151041
expect(execution).to be_an_instance_of(Temporal::Workflow::ExecutionInfo)

0 commit comments

Comments
 (0)
Please sign in to comment.