Skip to content

Allow completed Tasks to expose their results #753

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
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
36 changes: 36 additions & 0 deletions src/Durable/Commands/GetDurableTaskResult.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
17 changes: 17 additions & 0 deletions src/Durable/DurableTaskHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ public void WaitAny(
if (scheduledHistoryEvent != null)
{
scheduledHistoryEvent.IsProcessed = true;
scheduledHistoryEvent.IsPlayed = true;
}

if (completedHistoryEvent != null)
Expand All @@ -179,6 +180,7 @@ public void WaitAny(
}

completedHistoryEvent.IsProcessed = true;
completedHistoryEvent.IsPlayed = true;
}
}

Expand All @@ -195,6 +197,21 @@ public void WaitAny(
}
}

public void GetTaskResult(
IReadOnlyCollection<DurableTask> tasksToQueryResultFor,
OrchestrationContext context,
Action<object> 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();
Expand Down
6 changes: 3 additions & 3 deletions src/Durable/Tasks/ActivityInvocationTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/Durable/Tasks/DurableTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/Durable/Tasks/DurableTimerTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ 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 &&
e.FireAt.Equals(FireAt) &&
!e.IsProcessed);
}

internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent)
internal override HistoryEvent GetCompletedHistoryEvent(OrchestrationContext context, HistoryEvent scheduledHistoryEvent, bool processed)
{
return scheduledHistoryEvent == null
? null
Expand Down
6 changes: 3 additions & 3 deletions src/Durable/Tasks/ExternalEventTask.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,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()]
Expand Down Expand Up @@ -263,12 +265,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

Expand All @@ -280,7 +286,8 @@ function GetRaiseEventUrl(
[string] $InstanceId,
[string] $EventName,
[string] $TaskHubName,
[string] $ConnectionName) {
[string] $ConnectionName,
[string] $AppCode) {

$RequestUrl = $DurableClient.BaseUrl + "/instances/$InstanceId/raiseEvent/$EventName"

Expand All @@ -291,6 +298,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)
}
Expand Down
4 changes: 2 additions & 2 deletions test/Unit/Durable/ActivityInvocationTaskTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down