Skip to content

Commit a6654a1

Browse files
feat: add has_last_result? to workflow info
1 parent 66434e6 commit a6654a1

File tree

5 files changed

+123
-0
lines changed

5 files changed

+123
-0
lines changed

temporalio/lib/temporalio/internal/worker/workflow_instance.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ def initialize(details)
133133
last_result: if @init_job.last_completion_result
134134
@payload_converter.from_payloads(@init_job.last_completion_result).first
135135
end,
136+
has_last_result?: !@init_job.last_completion_result.nil?,
136137
namespace: details.namespace,
137138
parent: if @init_job.parent_workflow_info
138139
Workflow::Info::ParentInfo.new(

temporalio/lib/temporalio/workflow/info.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ module Workflow
1111
:headers,
1212
:last_failure,
1313
:last_result,
14+
:has_last_result?,
1415
:namespace,
1516
:parent,
1617
:priority,
@@ -44,6 +45,9 @@ module Workflow
4445
# @return [Exception, nil] Failure if this workflow run is a continuation of a failure.
4546
# @!attribute last_result
4647
# @return [Object, nil] Successful result if this workflow is a continuation of a success.
48+
# @!attribute has_last_result?
49+
# @return [Boolean] Successful result if this workflow is a continuation of a success.
50+
4751
# @!attribute namespace
4852
# @return [String] Namespace for the workflow.
4953
# @!attribute parent

temporalio/sig/temporalio/workflow/info.rbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ module Temporalio
99
attr_reader headers: Hash[String, untyped]
1010
attr_reader last_failure: Exception?
1111
attr_reader last_result: Object?
12+
attr_reader has_last_result?: bool
1213
attr_reader namespace: String
1314
attr_reader parent: ParentInfo?
1415
attr_reader priority: Temporalio::Priority
@@ -31,6 +32,7 @@ module Temporalio
3132
headers: Hash[String, untyped],
3233
last_failure: Exception?,
3334
last_result: Object?,
35+
has_last_result?: bool,
3436
namespace: String,
3537
parent: ParentInfo?,
3638
priority: Temporalio::Priority?,

temporalio/test/client_test.rb

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,4 +267,90 @@ def test_binary_metadata
267267
ensure
268268
env.client.connection.rpc_metadata = orig_metadata
269269
end
270+
271+
class ScheduleResult
272+
def initialize(client, handle)
273+
@handle = handle
274+
@client = client
275+
end
276+
277+
def result
278+
desc = @handle.describe
279+
# oldest first
280+
workflow_id = desc.info.recent_actions.last&.action&.workflow_id
281+
return nil if workflow_id.nil?
282+
283+
workflow_handle = @client.workflow_handle(workflow_id)
284+
workflow_handle.result
285+
end
286+
end
287+
288+
class LastResultWorkflow < Temporalio::Workflow::Definition
289+
def execute
290+
last_result = Temporalio::Workflow.info.last_result
291+
return "The last result was #{last_result}" unless last_result.nil?
292+
293+
'First result'
294+
end
295+
end
296+
297+
def test_last_completion_result
298+
id = "wf-#{SecureRandom.uuid}"
299+
task_queue = "tq-#{SecureRandom.uuid}"
300+
handle = env.client.create_schedule(
301+
'last-result-workflow',
302+
Temporalio::Client::Schedule.new(
303+
action: Temporalio::Client::Schedule::Action::StartWorkflow.new(
304+
LastResultWorkflow,
305+
id:, task_queue:
306+
),
307+
spec: Temporalio::Client::Schedule::Spec.new
308+
)
309+
)
310+
311+
schedule = ScheduleResult.new(env.client, handle)
312+
313+
Temporalio::Worker.new(client: env.client, task_queue:, workflows: [LastResultWorkflow]).run do
314+
handle.trigger
315+
assert_equal 'First result', schedule.result
316+
317+
handle.trigger
318+
assert_equal 'The last result was First result', schedule.result
319+
end
320+
321+
handle.delete
322+
end
323+
324+
class HasLastResultWorkflow < Temporalio::Workflow::Definition
325+
def execute # rubocop:disable Naming/PredicateMethod
326+
Temporalio::Workflow.info.has_last_result?
327+
end
328+
end
329+
330+
def test_has_last_completion_result
331+
id = "wf-#{SecureRandom.uuid}"
332+
task_queue = "tq-#{SecureRandom.uuid}"
333+
handle = env.client.create_schedule(
334+
'has-last-result-workflow',
335+
Temporalio::Client::Schedule.new(
336+
action: Temporalio::Client::Schedule::Action::StartWorkflow.new(
337+
HasLastResultWorkflow,
338+
id:, task_queue:
339+
),
340+
spec: Temporalio::Client::Schedule::Spec.new
341+
)
342+
)
343+
344+
schedule = ScheduleResult.new(env.client, handle)
345+
346+
Temporalio::Worker.new(client: env.client, task_queue:, workflows: [HasLastResultWorkflow]).run do
347+
handle.trigger
348+
assert_equal false, schedule.result
349+
350+
handle.trigger
351+
assert_equal true, schedule.result
352+
end
353+
354+
handle.delete
355+
end
270356
end

temporalio/test/worker_workflow_test.rb

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ def test_info
206206
assert_nil info.fetch('execution_timeout')
207207
assert_nil info.fetch('last_failure')
208208
assert_nil info.fetch('last_result')
209+
assert_equal false, info.fetch('has_last_result?')
209210
assert_equal env.client.namespace, info['namespace']
210211
assert_nil info.fetch('parent')
211212
assert_nil info.fetch('retry_policy')
@@ -2653,6 +2654,35 @@ def test_non_durable_timer
26532654
.map { |a| a.start_to_fire_timeout.to_f })
26542655
end
26552656
end
2657+
2658+
class LastFailureWorkflow < Temporalio::Workflow::Definition
2659+
workflow_query_attr_reader :failure
2660+
2661+
def execute
2662+
info = Temporalio::Workflow.info
2663+
2664+
@failure = info.last_failure
2665+
2666+
return 'Done' if info.attempt != 1
2667+
2668+
raise Temporalio::Error::ApplicationError.new('Intentional failure', category: Temporalio::Error::ApplicationError::Category::BENIGN)
2669+
end
2670+
end
2671+
2672+
def test_last_failure
2673+
execute_workflow(LastFailureWorkflow, retry_policy:
2674+
Temporalio::RetryPolicy.new(
2675+
initial_interval: 0,
2676+
max_attempts: 2
2677+
)) do |handle|
2678+
result = handle.result
2679+
2680+
assert_equal 'Done', result
2681+
2682+
previous_failure = handle.query(:failure)
2683+
assert_equal 'Intentional failure', previous_failure
2684+
end
2685+
end
26562686
end
26572687

26582688
# TODO(cretz): To test

0 commit comments

Comments
 (0)