Skip to content

Commit bd1bfa6

Browse files
authored
[Durable] Add Version property to $Context (#1108)
1 parent 613e304 commit bd1bfa6

File tree

5 files changed

+114
-1
lines changed

5 files changed

+114
-1
lines changed

release_notes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
* Set environment variable to avoid Get-AzAccessToken breaking change
1+
* [Durable] Add Version property to $Context

src/DurableSDK/HistoryEvent.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@ internal class HistoryEvent
5050

5151
[DataMember]
5252
public string Name { get; set; }
53+
54+
[DataMember]
55+
public string Version { get; set; }
5356

5457
[DataMember]
5558
public string Result { get; set; }

src/DurableSDK/OrchestrationContext.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,20 @@ public class OrchestrationContext
3636
internal OrchestrationActionCollector OrchestrationActionCollector { get; } = new OrchestrationActionCollector();
3737

3838
internal object CustomStatus { get; set; }
39+
40+
private readonly Lazy<string> _version;
41+
42+
public string Version
43+
{
44+
get
45+
{
46+
return _version.Value;
47+
}
48+
}
49+
50+
public OrchestrationContext()
51+
{
52+
_version = new Lazy<string>(() => OrchestrationVersionExtractor.GetVersionFromHistory(History));
53+
}
3954
}
4055
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Microsoft.Azure.Functions.PowerShellWorker.Durable
7+
{
8+
using System;
9+
10+
/// <summary>
11+
/// Helper class to extract version information from orchestration context.
12+
/// </summary>
13+
internal static class OrchestrationVersionExtractor
14+
{
15+
/// <summary>
16+
/// Gets the orchestration version from a collection of history events.
17+
/// </summary>
18+
/// <param name="historyEvents">The history events to search.</param>
19+
/// <returns>The version, or null if not found.</returns>
20+
public static string GetVersionFromHistory(HistoryEvent[] historyEvents)
21+
{
22+
if (historyEvents == null)
23+
{
24+
return null;
25+
}
26+
27+
var executionStartedEvent = Array.Find(historyEvents, e => e.EventType == HistoryEventType.ExecutionStarted);
28+
return executionStartedEvent?.Version;
29+
}
30+
}
31+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//
2+
// Copyright (c) Microsoft. All rights reserved.
3+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
4+
//
5+
6+
namespace Microsoft.Azure.Functions.PowerShellWorker.Test.Durable
7+
{
8+
using System;
9+
using Microsoft.Azure.Functions.PowerShellWorker.Durable;
10+
using Xunit;
11+
12+
public class OrchestrationVersionExtractorTests
13+
{
14+
[Fact]
15+
public void GetVersionFromHistory_ReturnsNull_WhenHistoryIsNull()
16+
{
17+
string result = OrchestrationVersionExtractor.GetVersionFromHistory(null);
18+
19+
Assert.Null(result);
20+
}
21+
22+
[Fact]
23+
public void GetVersionFromHistory_ReturnsNull_WhenHistoryHasNoExecutionStartedEvent()
24+
{
25+
var historyEvents = new[]
26+
{
27+
new HistoryEvent { EventType = HistoryEventType.OrchestratorStarted },
28+
new HistoryEvent { EventType = HistoryEventType.TaskScheduled }
29+
};
30+
31+
string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents);
32+
33+
Assert.Null(result);
34+
}
35+
36+
[Fact]
37+
public void GetVersionFromHistory_ReturnsVersion_WhenExecutionStartedEventExists()
38+
{
39+
var historyEvents = new[]
40+
{
41+
new HistoryEvent { EventType = HistoryEventType.OrchestratorStarted },
42+
new HistoryEvent { EventType = HistoryEventType.ExecutionStarted, Version = "1.0" },
43+
};
44+
45+
string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents);
46+
47+
Assert.Equal("1.0", result);
48+
}
49+
50+
[Fact]
51+
public void GetVersionFromHistory_ReturnsFirstExecutionStartedVersion_WhenMultipleExecutionStartedEventsExist()
52+
{
53+
var historyEvents = new[]
54+
{
55+
new HistoryEvent { EventType = HistoryEventType.ExecutionStarted, Version = "1.0" },
56+
new HistoryEvent { EventType = HistoryEventType.ExecutionStarted, Version = "2.0" }
57+
};
58+
59+
string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents);
60+
61+
Assert.Equal("1.0", result);
62+
}
63+
}
64+
}

0 commit comments

Comments
 (0)