Skip to content

Commit 4fed10a

Browse files
authored
Expose root execution (#454)
Fixes #415
1 parent 6a0eb69 commit 4fed10a

3 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/Temporalio/Worker/WorkflowInstance.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,13 @@ public WorkflowInstance(WorkflowInstanceDetails details)
163163
RunId: start.ParentWorkflowInfo.RunId,
164164
WorkflowId: start.ParentWorkflowInfo.WorkflowId);
165165
}
166+
WorkflowInfo.RootInfo? root = null;
167+
if (start.RootWorkflow != null)
168+
{
169+
root = new(
170+
RunId: start.RootWorkflow.RunId,
171+
WorkflowId: start.RootWorkflow.WorkflowId);
172+
}
166173
var lastFailure = start.ContinuedFailure == null ?
167174
null : failureConverter.ToException(start.ContinuedFailure, PayloadConverter);
168175
var lastResult = start.LastCompletionResult?.Payloads_.Select(v => new RawValue(v)).ToArray();
@@ -178,6 +185,7 @@ public WorkflowInstance(WorkflowInstanceDetails details)
178185
Namespace: details.Namespace,
179186
Parent: parent,
180187
RetryPolicy: start.RetryPolicy == null ? null : Common.RetryPolicy.FromProto(start.RetryPolicy),
188+
Root: root,
181189
RunId: act.RunId,
182190
RunTimeout: start.WorkflowRunTimeout?.ToTimeSpan(),
183191
StartTime: act.Timestamp.ToDateTime(),

src/Temporalio/Workflows/WorkflowInfo.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ namespace Temporalio.Workflows
1818
/// <param name="Namespace">Namespace for the workflow.</param>
1919
/// <param name="Parent">Parent information for the workflow if this is a child.</param>
2020
/// <param name="RetryPolicy">Retry policy for the workflow.</param>
21+
/// <param name="Root">Root information for the workflow. This is nil in pre-1.27.0 server
22+
/// versions or if there is no root (i.e. the root is itself).</param>
2123
/// <param name="RunId">Run ID for the workflow.</param>
2224
/// <param name="RunTimeout">Run timeout for the workflow.</param>
2325
/// <param name="StartTime">Time when the workflow started.</param>
@@ -40,6 +42,7 @@ public record WorkflowInfo(
4042
string Namespace,
4143
WorkflowInfo.ParentInfo? Parent,
4244
RetryPolicy? RetryPolicy,
45+
WorkflowInfo.RootInfo? Root,
4346
string RunId,
4447
TimeSpan? RunTimeout,
4548
DateTime StartTime,
@@ -73,5 +76,14 @@ public record ParentInfo(
7376
string Namespace,
7477
string RunId,
7578
string WorkflowId);
79+
80+
/// <summary>
81+
/// Information about a parent of a workflow.
82+
/// </summary>
83+
/// <param name="RunId">Run ID for the root.</param>
84+
/// <param name="WorkflowId">Workflow ID for the root.</param>
85+
public record RootInfo(
86+
string RunId,
87+
string WorkflowId);
7688
}
7789
}

tests/Temporalio.Tests/Worker/WorkflowWorkerTests.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,9 +373,18 @@ public class InfoWorkflow
373373
public Task<WorkflowInfo> RunAsync() => Task.FromResult(Workflow.Info);
374374
}
375375

376+
[Workflow]
377+
public class InfoFromChildWorkflow
378+
{
379+
[WorkflowRun]
380+
public Task<WorkflowInfo> RunAsync() =>
381+
Workflow.ExecuteChildWorkflowAsync((InfoWorkflow wf) => wf.RunAsync());
382+
}
383+
376384
[Fact]
377385
public async Task ExecuteWorkflowAsync_Info_Succeeds()
378386
{
387+
// Normal info
379388
await ExecuteWorkerAsync<InfoWorkflow>(async worker =>
380389
{
381390
var handle = await Env.Client.StartWorkflowAsync(
@@ -389,6 +398,7 @@ await ExecuteWorkerAsync<InfoWorkflow>(async worker =>
389398
Assert.Equal(worker.Client.Options.Namespace, result.Namespace);
390399
Assert.Null(result.Parent);
391400
Assert.Null(result.RetryPolicy);
401+
Assert.Null(result.Root);
392402
Assert.Equal(handle.ResultRunId, result.RunId);
393403
Assert.Null(result.RunTimeout);
394404
Assert.InRange(
@@ -401,6 +411,22 @@ await ExecuteWorkerAsync<InfoWorkflow>(async worker =>
401411
Assert.Equal(handle.Id, result.WorkflowId);
402412
Assert.Equal("InfoWorkflow", result.WorkflowType);
403413
});
414+
// Child info
415+
await ExecuteWorkerAsync<InfoFromChildWorkflow>(
416+
async worker =>
417+
{
418+
var handle = await Env.Client.StartWorkflowAsync(
419+
(InfoFromChildWorkflow wf) => wf.RunAsync(),
420+
new(id: $"workflow-{Guid.NewGuid()}", taskQueue: worker.Options.TaskQueue!));
421+
var result = await handle.GetResultAsync();
422+
Assert.Equal(
423+
new WorkflowInfo.ParentInfo(Env.Client.Options.Namespace, handle.ResultRunId!, handle.Id),
424+
result.Parent);
425+
Assert.Equal(
426+
new WorkflowInfo.RootInfo(handle.ResultRunId!, handle.Id),
427+
result.Root);
428+
},
429+
new TemporalWorkerOptions().AddWorkflow<InfoWorkflow>());
404430
}
405431

406432
public record HistoryInfo(int HistoryLength, int HistorySize, bool ContinueAsNewSuggested);

0 commit comments

Comments
 (0)