Skip to content

[Durable] Add Version property to $Context #1108

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

Merged
merged 7 commits into from
Jun 6, 2025
Merged
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
2 changes: 1 addition & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
@@ -1 +1 @@
* Set environment variable to avoid Get-AzAccessToken breaking change
* [Durable] Add Version property to $Context
3 changes: 3 additions & 0 deletions src/DurableSDK/HistoryEvent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ internal class HistoryEvent

[DataMember]
public string Name { get; set; }

[DataMember]
public string Version { get; set; }

[DataMember]
public string Result { get; set; }
Expand Down
15 changes: 15 additions & 0 deletions src/DurableSDK/OrchestrationContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,20 @@ public class OrchestrationContext
internal OrchestrationActionCollector OrchestrationActionCollector { get; } = new OrchestrationActionCollector();

internal object CustomStatus { get; set; }

private readonly Lazy<string> _version;

public string Version
{
get
{
return _version.Value;
}
}

public OrchestrationContext()
{
_version = new Lazy<string>(() => OrchestrationVersionExtractor.GetVersionFromHistory(History));
}
}
}
31 changes: 31 additions & 0 deletions src/DurableSDK/OrchestrationVersionExtractor.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.Azure.Functions.PowerShellWorker.Durable
{
using System;

/// <summary>
/// Helper class to extract version information from orchestration context.
/// </summary>
internal static class OrchestrationVersionExtractor
{
/// <summary>
/// Gets the orchestration version from a collection of history events.
/// </summary>
/// <param name="historyEvents">The history events to search.</param>
/// <returns>The version, or null if not found.</returns>
public static string GetVersionFromHistory(HistoryEvent[] historyEvents)
{
if (historyEvents == null)
{
return null;
}

var executionStartedEvent = Array.Find(historyEvents, e => e.EventType == HistoryEventType.ExecutionStarted);
return executionStartedEvent?.Version;
}
}
}
64 changes: 64 additions & 0 deletions test/Unit/Durable/OrchestrationVersionExtractorTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

namespace Microsoft.Azure.Functions.PowerShellWorker.Test.Durable
{
using System;
using Microsoft.Azure.Functions.PowerShellWorker.Durable;
using Xunit;

public class OrchestrationVersionExtractorTests
{
[Fact]
public void GetVersionFromHistory_ReturnsNull_WhenHistoryIsNull()
{
string result = OrchestrationVersionExtractor.GetVersionFromHistory(null);

Assert.Null(result);
}

[Fact]
public void GetVersionFromHistory_ReturnsNull_WhenHistoryHasNoExecutionStartedEvent()
{
var historyEvents = new[]
{
new HistoryEvent { EventType = HistoryEventType.OrchestratorStarted },
new HistoryEvent { EventType = HistoryEventType.TaskScheduled }
};

string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents);

Assert.Null(result);
}

[Fact]
public void GetVersionFromHistory_ReturnsVersion_WhenExecutionStartedEventExists()
{
var historyEvents = new[]
{
new HistoryEvent { EventType = HistoryEventType.OrchestratorStarted },
new HistoryEvent { EventType = HistoryEventType.ExecutionStarted, Version = "1.0" },
};

string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents);

Assert.Equal("1.0", result);
}

[Fact]
public void GetVersionFromHistory_ReturnsFirstExecutionStartedVersion_WhenMultipleExecutionStartedEventsExist()
{
var historyEvents = new[]
{
new HistoryEvent { EventType = HistoryEventType.ExecutionStarted, Version = "1.0" },
new HistoryEvent { EventType = HistoryEventType.ExecutionStarted, Version = "2.0" }
};

string result = OrchestrationVersionExtractor.GetVersionFromHistory(historyEvents);

Assert.Equal("1.0", result);
}
}
}
Loading