diff --git a/release_notes.md b/release_notes.md index bedf5824..24d68654 100644 --- a/release_notes.md +++ b/release_notes.md @@ -1,2 +1,3 @@ * Bug fix: [Context.InstanceId can now be accessed](https://github.com/Azure/azure-functions-powershell-worker/issues/727) -* Bug fix: [Data in External Events is now read and returned to orchestrator](https://github.com/Azure/azure-functions-powershell-worker/issues/68) \ No newline at end of file +* Bug fix: [Data in External Events is now read and returned to orchestrator](https://github.com/Azure/azure-functions-powershell-worker/issues/68) +* New feature (external contribution): [Get-TaskResult can now be used to obtain the result of an already-completed Durable Functions Task](https://github.com/Azure/azure-functions-powershell-worker/pull/786) \ No newline at end of file diff --git a/src/Durable/Commands/GetDurableTaskResult.cs b/src/Durable/Commands/GetDurableTaskResult.cs new file mode 100644 index 00000000..74f5493b --- /dev/null +++ b/src/Durable/Commands/GetDurableTaskResult.cs @@ -0,0 +1,36 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +#pragma warning disable 1591 // Missing XML comment for publicly visible type or member 'member' + +namespace Microsoft.Azure.Functions.PowerShellWorker.Durable.Commands +{ + using System.Collections; + using System.Management.Automation; + using Microsoft.Azure.Functions.PowerShellWorker.Durable.Tasks; + + [Cmdlet("Get", "DurableTaskResult")] + public class GetDurableTaskResultCommand : PSCmdlet + { + [Parameter(Mandatory = true)] + [ValidateNotNull] + public DurableTask[] Task { get; set; } + + private readonly DurableTaskHandler _durableTaskHandler = new DurableTaskHandler(); + + protected override void EndProcessing() + { + var privateData = (Hashtable)MyInvocation.MyCommand.Module.PrivateData; + var context = (OrchestrationContext)privateData[SetFunctionInvocationContextCommand.ContextKey]; + + _durableTaskHandler.GetTaskResult(Task, context, WriteObject); + } + + protected override void StopProcessing() + { + _durableTaskHandler.Stop(); + } + } +} diff --git a/src/Durable/DurableTaskHandler.cs b/src/Durable/DurableTaskHandler.cs index a49c1148..c65b1275 100644 --- a/src/Durable/DurableTaskHandler.cs +++ b/src/Durable/DurableTaskHandler.cs @@ -206,6 +206,21 @@ public void WaitAny( } } + public void GetTaskResult( + IReadOnlyCollection tasksToQueryResultFor, + OrchestrationContext context, + Action output) + { + foreach (var task in tasksToQueryResultFor) { + var scheduledHistoryEvent = task.GetScheduledHistoryEvent(context, true); + var processedHistoryEvent = task.GetCompletedHistoryEvent(context, scheduledHistoryEvent, true); + if (processedHistoryEvent != null) + { + output(GetEventResult(processedHistoryEvent)); + } + } + } + public void Stop() { _waitForStop.Set(); diff --git a/src/Durable/Tasks/ActivityInvocationTask.cs b/src/Durable/Tasks/ActivityInvocationTask.cs index 87bf61c2..a0ba49b4 100644 --- a/src/Durable/Tasks/ActivityInvocationTask.cs +++ b/src/Durable/Tasks/ActivityInvocationTask.cs @@ -37,15 +37,15 @@ internal ActivityInvocationTask(string functionName, object functionInput) { } - internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context) + internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context, bool processed) { return context.History.FirstOrDefault( e => e.EventType == HistoryEventType.TaskScheduled && e.Name == FunctionName && - !e.IsProcessed); + e.IsProcessed == processed); } - internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent) + internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent, bool processed) { return scheduledHistoryEvent == null ? null diff --git a/src/Durable/Tasks/DurableTask.cs b/src/Durable/Tasks/DurableTask.cs index af71f36d..228e769b 100644 --- a/src/Durable/Tasks/DurableTask.cs +++ b/src/Durable/Tasks/DurableTask.cs @@ -11,9 +11,9 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Durable.Tasks public abstract class DurableTask { - internal abstract HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context); + internal abstract HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context, bool processed = false); - internal abstract HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent); + internal abstract HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent, bool processed = false); internal abstract OrchestrationAction CreateOrchestrationAction(); } diff --git a/src/Durable/Tasks/DurableTimerTask.cs b/src/Durable/Tasks/DurableTimerTask.cs index 86a75abe..4bb357ef 100644 --- a/src/Durable/Tasks/DurableTimerTask.cs +++ b/src/Durable/Tasks/DurableTimerTask.cs @@ -28,7 +28,7 @@ internal DurableTimerTask( Action = new CreateDurableTimerAction(FireAt); } - internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context) + internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context, bool processed) { return context.History.FirstOrDefault( e => e.EventType == HistoryEventType.TimerCreated && @@ -36,7 +36,7 @@ internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext con !e.IsProcessed); } - internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent) + internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent, bool processed) { return scheduledHistoryEvent == null ? null diff --git a/src/Durable/Tasks/ExternalEventTask.cs b/src/Durable/Tasks/ExternalEventTask.cs index 1bd1fc58..5d12ad66 100644 --- a/src/Durable/Tasks/ExternalEventTask.cs +++ b/src/Durable/Tasks/ExternalEventTask.cs @@ -21,17 +21,17 @@ public ExternalEventTask(string externalEventName) } // There is no corresponding history event for an expected external event - internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context) + internal override HistoryEvent GetScheduledHistoryEvent(OrchestrationContext context, bool processed) { return null; } - internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent taskScheduled) + internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent taskScheduled, bool processed) { return context.History.FirstOrDefault( e => e.EventType == HistoryEventType.EventRaised && e.Name == ExternalEventName && - !e.IsProcessed); + e.IsPlayed == processed); } internal override OrchestrationAction CreateOrchestrationAction() diff --git a/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1 b/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1 index 7ae51c2b..7bed18b9 100644 --- a/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1 +++ b/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1 @@ -59,6 +59,7 @@ FunctionsToExport = @( # Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export. CmdletsToExport = @( 'Get-OutputBinding', + 'Get-DurableTaskResult' 'Invoke-DurableActivity', 'Push-OutputBinding', 'Set-DurableCustomStatus', diff --git a/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psm1 b/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psm1 index 2fa1ee02..c87f08b6 100644 --- a/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psm1 +++ b/src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psm1 @@ -243,6 +243,8 @@ function New-DurableOrchestrationCheckStatusResponse { The TaskHubName of the orchestration instance that will handle the external event. .PARAMETER ConnectionName The name of the connection string associated with TaskHubName +.PARAMETER AppCode + The Azure Functions system key #> function Send-DurableExternalEvent { [CmdletBinding()] @@ -271,12 +273,16 @@ function Send-DurableExternalEvent { [Parameter( ValueFromPipelineByPropertyName=$true)] - [string] $ConnectionName + [string] $ConnectionName, + + [Parameter( + ValueFromPipelineByPropertyName=$true)] + [string] $AppCode ) $DurableClient = GetDurableClientFromModulePrivateData - $RequestUrl = GetRaiseEventUrl -DurableClient $DurableClient -InstanceId $InstanceId -EventName $EventName -TaskHubName $TaskHubName -ConnectionName $ConnectionName + $RequestUrl = GetRaiseEventUrl -DurableClient $DurableClient -InstanceId $InstanceId -EventName $EventName -TaskHubName $TaskHubName -ConnectionName $ConnectionName -AppCode $AppCode $Body = $EventData | ConvertTo-Json -Compress @@ -288,7 +294,8 @@ function GetRaiseEventUrl( [string] $InstanceId, [string] $EventName, [string] $TaskHubName, - [string] $ConnectionName) { + [string] $ConnectionName, + [string] $AppCode) { $RequestUrl = $DurableClient.BaseUrl + "/instances/$InstanceId/raiseEvent/$EventName" @@ -299,6 +306,9 @@ function GetRaiseEventUrl( if ($null -eq $ConnectionName) { $query += "connection=$ConnectionName" } + if ($null -eq $AppCode) { + $query += "code=$AppCode" + } if ($query.Count -gt 0) { $RequestUrl += "?" + [string]::Join("&", $query) } diff --git a/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/DurableEndToEndTests.cs b/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/DurableEndToEndTests.cs index 181d294e..30f30669 100644 --- a/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/DurableEndToEndTests.cs +++ b/test/E2E/Azure.Functions.PowerShellWorker.E2E/Azure.Functions.PowerShellWorker.E2E/DurableEndToEndTests.cs @@ -262,6 +262,58 @@ public async Task ExternalEventReturnsData() } } + [Fact] + public async Task OrchestratationCanAlwaysObtainTaskResult() + { + var initialResponse = await Utilities.GetHttpTriggerResponse("DurableClient", queryString: "?FunctionName=DurableOrchestratorGetTaskResult"); + Assert.Equal(HttpStatusCode.Accepted, initialResponse.StatusCode); + + var initialResponseBody = await initialResponse.Content.ReadAsStringAsync(); + dynamic initialResponseBodyObject = JsonConvert.DeserializeObject(initialResponseBody); + var statusQueryGetUri = (string)initialResponseBodyObject.statusQueryGetUri; + + var startTime = DateTime.UtcNow; + + using (var httpClient = new HttpClient()) + { + while (true) + { + var statusResponse = await httpClient.GetAsync(statusQueryGetUri); + switch (statusResponse.StatusCode) + { + case HttpStatusCode.Accepted: + { + var statusResponseBody = await GetResponseBodyAsync(statusResponse); + var runtimeStatus = (string)statusResponseBody.runtimeStatus; + Assert.True( + runtimeStatus == "Running" || runtimeStatus == "Pending", + $"Unexpected runtime status: {runtimeStatus}"); + + if (DateTime.UtcNow > startTime + _orchestrationCompletionTimeout) + { + Assert.True(false, $"The orchestration has not completed after {_orchestrationCompletionTimeout}"); + } + + await Task.Delay(TimeSpan.FromSeconds(2)); + break; + } + + case HttpStatusCode.OK: + { + var statusResponseBody = await GetResponseBodyAsync(statusResponse); + Assert.Equal("Completed", (string)statusResponseBody.runtimeStatus); + Assert.Equal("Hello world", statusResponseBody.output.ToString()); + return; + } + + default: + Assert.True(false, $"Unexpected orchestration status code: {statusResponse.StatusCode}"); + break; + } + } + } + } + [Fact] public async Task ActivityCanHaveQueueBinding() { diff --git a/test/E2E/TestFunctionApp/DurableOrchestratorGetTaskResult/function.json b/test/E2E/TestFunctionApp/DurableOrchestratorGetTaskResult/function.json new file mode 100644 index 00000000..336f5a18 --- /dev/null +++ b/test/E2E/TestFunctionApp/DurableOrchestratorGetTaskResult/function.json @@ -0,0 +1,9 @@ +{ + "bindings": [ + { + "name": "Context", + "type": "orchestrationTrigger", + "direction": "in" + } + ] +} \ No newline at end of file diff --git a/test/E2E/TestFunctionApp/DurableOrchestratorGetTaskResult/run.ps1 b/test/E2E/TestFunctionApp/DurableOrchestratorGetTaskResult/run.ps1 new file mode 100644 index 00000000..23380916 --- /dev/null +++ b/test/E2E/TestFunctionApp/DurableOrchestratorGetTaskResult/run.ps1 @@ -0,0 +1,8 @@ +param($Context) + +$output = @() + +$task = Invoke-DurableActivity -FunctionName 'DurableActivity' -Input "world" -NoWait +$firstTask = Wait-DurableTask -Task @($task) -Any +$output += Get-DurableTaskResult -Task @($firstTask) +$output diff --git a/test/Unit/Durable/ActivityInvocationTaskTests.cs b/test/Unit/Durable/ActivityInvocationTaskTests.cs index c96cc755..530711ca 100644 --- a/test/Unit/Durable/ActivityInvocationTaskTests.cs +++ b/test/Unit/Durable/ActivityInvocationTaskTests.cs @@ -225,8 +225,8 @@ public void GetCompletedHistoryEvent_ReturnsTaskCompletedOrTaskFailed(bool succe var orchestrationContext = new OrchestrationContext { History = history }; var task = new ActivityInvocationTask(FunctionName, FunctionInput); - var scheduledEvent = task.GetScheduledHistoryEvent(orchestrationContext); - var completedEvent = task.GetCompletedHistoryEvent(orchestrationContext, scheduledEvent); + var scheduledEvent = task.GetScheduledHistoryEvent(orchestrationContext, false); + var completedEvent = task.GetCompletedHistoryEvent(orchestrationContext, scheduledEvent, false); Assert.Equal(scheduledEvent.EventId, completedEvent.TaskScheduledId); }