Skip to content

Commit e04925d

Browse files
authored
[Durable] Custom orchestration status (#592)
1 parent 30250f4 commit e04925d

File tree

13 files changed

+145
-14
lines changed

13 files changed

+145
-14
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"bindings": [
3+
{
4+
"name": "Context",
5+
"type": "orchestrationTrigger",
6+
"direction": "in"
7+
}
8+
]
9+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using namespace System.Net
2+
3+
param($Context)
4+
5+
Write-Host 'CustomStatusOrchestrator: started.'
6+
7+
$output = @()
8+
9+
Set-DurableCustomStatus -CustomStatus 'Processing Tokyo'
10+
$output += Invoke-ActivityFunction -FunctionName 'SayHello' -Input 'Tokyo'
11+
12+
Set-DurableCustomStatus -CustomStatus @{ ProgressMessage = 'Processing Seattle'; Stage = 2 }
13+
$output += Invoke-ActivityFunction -FunctionName 'SayHello' -Input 'Seattle'
14+
15+
Set-DurableCustomStatus -CustomStatus @('Processing London', 'Last stage')
16+
$output += Invoke-ActivityFunction -FunctionName 'SayHello' -Input 'London'
17+
18+
Set-DurableCustomStatus 'Processing completed'
19+
20+
Write-Host 'CustomStatusOrchestrator: finished.'
21+
22+
$output
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"bindings": [
3+
{
4+
"authLevel": "function",
5+
"type": "httpTrigger",
6+
"direction": "in",
7+
"name": "Request",
8+
"methods": [
9+
"get",
10+
"post"
11+
]
12+
},
13+
{
14+
"type": "http",
15+
"direction": "out",
16+
"name": "Response"
17+
},
18+
{
19+
"name": "starter",
20+
"type": "durableClient",
21+
"direction": "in"
22+
}
23+
]
24+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using namespace System.Net
2+
3+
param($Request, $TriggerMetadata)
4+
5+
Write-Host 'CustomStatusStart started'
6+
7+
$InstanceId = Start-NewOrchestration -FunctionName 'CustomStatusOrchestrator' -InputObject 'Hello'
8+
Write-Host "Started orchestration with ID = '$InstanceId'"
9+
10+
$Response = New-OrchestrationCheckStatusResponse -Request $Request -InstanceId $InstanceId
11+
Push-OutputBinding -Name Response -Value $Response
12+
13+
Write-Host 'CustomStatusStart completed'
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
#pragma warning disable 1591 // Missing XML comment for publicly visible type or member 'member'
7+
8+
namespace Microsoft.Azure.Functions.PowerShellWorker.Durable.Commands
9+
{
10+
using System.Collections;
11+
using System.Management.Automation;
12+
13+
[Cmdlet("Set", "DurableCustomStatus")]
14+
public class SetDurableCustomStatusCommand : PSCmdlet
15+
{
16+
[Parameter(
17+
Mandatory = true,
18+
Position = 0,
19+
ValueFromPipeline = true)]
20+
public object CustomStatus { get; set; }
21+
22+
protected override void EndProcessing()
23+
{
24+
var privateData = (Hashtable)MyInvocation.MyCommand.Module.PrivateData;
25+
var context = (OrchestrationContext)privateData[SetFunctionInvocationContextCommand.ContextKey];
26+
context.CustomStatus = CustomStatus;
27+
}
28+
}
29+
}

src/Durable/OrchestrationContext.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ public class OrchestrationContext
3131
[DataMember]
3232
internal HistoryEvent[] History { get; set; }
3333

34-
public DateTime CurrentUtcDateTime {get; internal set; }
34+
public DateTime CurrentUtcDateTime { get; internal set; }
3535

3636
internal OrchestrationActionCollector OrchestrationActionCollector { get; } = new OrchestrationActionCollector();
37+
38+
internal object CustomStatus { get; set; }
3739
}
3840
}

src/Durable/OrchestrationFailureException.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,23 @@ public OrchestrationFailureException()
2424
{
2525
}
2626

27-
public OrchestrationFailureException(List<List<OrchestrationAction>> actions, Exception innerException)
28-
: base(FormatOrchestrationFailureMessage(actions, innerException), innerException)
27+
public OrchestrationFailureException(
28+
List<List<OrchestrationAction>> actions,
29+
object customStatus,
30+
Exception innerException)
31+
: base(FormatOrchestrationFailureMessage(actions, customStatus, innerException), innerException)
2932
{
3033
}
3134

32-
private static string FormatOrchestrationFailureMessage(List<List<OrchestrationAction>> actions, Exception exception)
35+
private static string FormatOrchestrationFailureMessage(
36+
List<List<OrchestrationAction>> actions,
37+
object customStatus,
38+
Exception exception)
3339
{
3440
// For more details on why this message looks like this, see:
3541
// - https://github.com/Azure/azure-functions-durable-js/pull/145
3642
// - https://github.com/Azure/azure-functions-durable-extension/pull/1171
37-
var orchestrationMessage = new OrchestrationMessage(isDone: false, actions, output: null, exception.Message);
43+
var orchestrationMessage = new OrchestrationMessage(isDone: false, actions, output: null, customStatus, exception.Message);
3844
var message = $"{exception.Message}{OutOfProcDataLabel}{JsonConvert.SerializeObject(orchestrationMessage)}";
3945
return message;
4046
}

src/Durable/OrchestrationInvoker.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public Hashtable Invoke(OrchestrationBindingInfo orchestrationBindingInfo, IPowe
4040
{
4141
// The orchestration function should be stopped and restarted
4242
pwsh.StopInvoke();
43-
return CreateOrchestrationResult(isDone: false, actions, output: null);
43+
return CreateOrchestrationResult(isDone: false, actions, output: null, context.CustomStatus);
4444
}
4545
else
4646
{
@@ -49,13 +49,13 @@ public Hashtable Invoke(OrchestrationBindingInfo orchestrationBindingInfo, IPowe
4949
// The orchestration function completed
5050
pwsh.EndInvoke(asyncResult);
5151
var result = FunctionReturnValueBuilder.CreateReturnValueFromFunctionOutput(outputBuffer);
52-
return CreateOrchestrationResult(isDone: true, actions, output: result);
52+
return CreateOrchestrationResult(isDone: true, actions, output: result, context.CustomStatus);
5353
}
5454
catch (Exception e)
5555
{
5656
// The orchestrator code has thrown an unhandled exception:
5757
// this should be treated as an entire orchestration failure
58-
throw new OrchestrationFailureException(actions, e);
58+
throw new OrchestrationFailureException(actions, context.CustomStatus, e);
5959
}
6060
}
6161
}
@@ -68,9 +68,10 @@ public Hashtable Invoke(OrchestrationBindingInfo orchestrationBindingInfo, IPowe
6868
private static Hashtable CreateOrchestrationResult(
6969
bool isDone,
7070
List<List<OrchestrationAction>> actions,
71-
object output)
71+
object output,
72+
object customStatus)
7273
{
73-
var orchestrationMessage = new OrchestrationMessage(isDone, actions, output);
74+
var orchestrationMessage = new OrchestrationMessage(isDone, actions, output, customStatus);
7475
return new Hashtable { { AzFunctionInfo.DollarReturn, orchestrationMessage } };
7576
}
7677
}

src/Durable/OrchestrationMessage.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@ namespace Microsoft.Azure.Functions.PowerShellWorker.Durable
1414
/// </summary>
1515
internal class OrchestrationMessage
1616
{
17-
public OrchestrationMessage(bool isDone, List<List<OrchestrationAction>> actions, object output, string error = null)
17+
public OrchestrationMessage(
18+
bool isDone,
19+
List<List<OrchestrationAction>> actions,
20+
object output,
21+
object customStatus,
22+
string error = null)
1823
{
1924
IsDone = isDone;
2025
Actions = actions;
2126
Output = output;
2227
Error = error;
28+
CustomStatus = customStatus;
2329
}
2430

2531
/// <summary>
@@ -41,5 +47,10 @@ public OrchestrationMessage(bool isDone, List<List<OrchestrationAction>> actions
4147
/// The orchestration error message.
4248
/// </summary>
4349
public readonly string Error;
50+
51+
/// <summary>
52+
/// Custom orchestration status.
53+
/// </summary>
54+
public readonly object CustomStatus;
4455
}
4556
}

src/Modules/Microsoft.Azure.Functions.PowerShellWorker/Microsoft.Azure.Functions.PowerShellWorker.psd1

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ CmdletsToExport = @(
5959
'Get-OutputBinding',
6060
'Invoke-ActivityFunction',
6161
'Push-OutputBinding',
62+
'Set-DurableCustomStatus',
6263
'Set-FunctionInvocationContext',
6364
'Start-DurableExternalEventListener',
6465
'Start-DurableTimer',

0 commit comments

Comments
 (0)