Skip to content

Commit 5f2ae75

Browse files
committed
feat(Workflow): add context and wfAction to Execute method
- Add Context property to Workflow class - Update Execute method in WorkflowGlobalTransaction to include wfAction parameter - Add test case for Execute method with wfAction
1 parent 16f174d commit 5f2ae75

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

src/Dtmworkflow/Workflow.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,13 @@ public partial class Workflow
1717

1818
public virtual WorkflowImp WorkflowImp { get; set; } = new WorkflowImp();
1919

20+
public Dictionary<string, object> Context => _context ??= new();
21+
2022
private readonly IDtmClient _httpClient;
2123
private readonly IDtmgRPCClient _grpcClient;
2224
private readonly Dtmcli.IBranchBarrierFactory _bbFactory;
2325
private readonly ILogger _logger;
26+
private Dictionary<string, object> _context;
2427

2528
public Workflow(IDtmClient httpClient, IDtmgRPCClient grpcClient, Dtmcli.IBranchBarrierFactory bbFactory, ILogger logger)
2629
{

src/Dtmworkflow/WorkflowGlobalTransaction.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,20 @@ public WorkflowGlobalTransaction(IWorkflowFactory workflowFactory, ILoggerFactor
2020
}
2121

2222
public async Task<byte[]> Execute(string name, string gid, byte[] data, bool isHttp = true)
23+
{
24+
return await this.Execute(name, gid, data, null, isHttp);
25+
}
26+
27+
public async Task<byte[]> Execute(string name, string gid, byte[] data, Action<Workflow> wfAction, bool isHttp = true)
2328
{
2429
if (!this._handlers.TryGetValue(name, out var handler))
2530
{
2631
throw new DtmCommon.DtmException($"workflow '{name}' not registered. please register at startup");
2732
}
2833

2934
var wf = _workflowFactory.NewWorkflow(name, gid, data, isHttp);
35+
if (wfAction != null)
36+
wfAction(wf);
3037

3138
foreach (var fn in handler.Custom)
3239
{

tests/Dtmgrpc.IntegrationTests/WorkflowGrpcTest.cs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,5 +94,32 @@ public async Task Execute_Success()
9494
status = await ITTestHelper.GetTranStatus(gid);
9595
Assert.Equal("succeed", status);
9696
}
97+
98+
99+
[Fact]
100+
public async Task ExecuteWithWfAction()
101+
{
102+
var provider = ITTestHelper.AddDtmGrpc();
103+
var workflowFactory = provider.GetRequiredService<IWorkflowFactory>();
104+
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
105+
WorkflowGlobalTransaction workflowGlobalTransaction = new WorkflowGlobalTransaction(workflowFactory, loggerFactory);
106+
string wfName = Guid.NewGuid().ToString();
107+
workflowGlobalTransaction.Register(wfName, (workflow, data) =>
108+
{
109+
Assert.NotNull(workflow.Context);
110+
Assert.Equal(2, workflow.Context.Count);
111+
Assert.Equal("value1", workflow.Context["key1-string"]);
112+
Assert.Equal(7, workflow.Context["key2-int"]);
113+
114+
Assert.Equal("input", Encoding.UTF8.GetString(data));
115+
116+
return Task.FromResult("output"u8.ToArray());
117+
});
118+
await workflowGlobalTransaction.Execute(wfName, Guid.NewGuid().ToString(), "input"u8.ToArray(), workflow =>
119+
{
120+
workflow.Context.Add("key1-string", "value1");
121+
workflow.Context.Add("key2-int", 7);
122+
});
123+
}
97124
}
98125
}

0 commit comments

Comments
 (0)