Skip to content

feat(Workflow): add context and wfAction to Execute method #103

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 3 additions & 0 deletions src/Dtmworkflow/Workflow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ public partial class Workflow

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

public Dictionary<string, object> Context => _context ??= new();

private readonly IDtmClient _httpClient;
private readonly IDtmgRPCClient _grpcClient;
private readonly Dtmcli.IBranchBarrierFactory _bbFactory;
private readonly ILogger _logger;
private Dictionary<string, object> _context;

public Workflow(IDtmClient httpClient, IDtmgRPCClient grpcClient, Dtmcli.IBranchBarrierFactory bbFactory, ILogger logger)
{
Expand Down
7 changes: 7 additions & 0 deletions src/Dtmworkflow/WorkflowGlobalTransaction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,20 @@
}

public async Task<byte[]> Execute(string name, string gid, byte[] data, bool isHttp = true)
{
return await this.Execute(name, gid, data, null, isHttp);
}

public async Task<byte[]> Execute(string name, string gid, byte[] data, Action<Workflow> wfAction, bool isHttp = true)
{
if (!this._handlers.TryGetValue(name, out var handler))
{
throw new DtmCommon.DtmException($"workflow '{name}' not registered. please register at startup");
}

var wf = _workflowFactory.NewWorkflow(name, gid, data, isHttp);
if (wfAction != null)
wfAction(wf);

Check warning on line 36 in src/Dtmworkflow/WorkflowGlobalTransaction.cs

View check run for this annotation

Codecov / codecov/patch

src/Dtmworkflow/WorkflowGlobalTransaction.cs#L36

Added line #L36 was not covered by tests

foreach (var fn in handler.Custom)
{
Expand Down
27 changes: 27 additions & 0 deletions tests/Dtmgrpc.IntegrationTests/WorkflowGrpcTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,5 +94,32 @@ public async Task Execute_Success()
status = await ITTestHelper.GetTranStatus(gid);
Assert.Equal("succeed", status);
}


[Fact]
public async Task ExecuteWithWfAction()
{
var provider = ITTestHelper.AddDtmGrpc();
var workflowFactory = provider.GetRequiredService<IWorkflowFactory>();
var loggerFactory = provider.GetRequiredService<ILoggerFactory>();
WorkflowGlobalTransaction workflowGlobalTransaction = new WorkflowGlobalTransaction(workflowFactory, loggerFactory);
string wfName = Guid.NewGuid().ToString();
workflowGlobalTransaction.Register(wfName, (workflow, data) =>
{
Assert.NotNull(workflow.Context);
Assert.Equal(2, workflow.Context.Count);
Assert.Equal("value1", workflow.Context["key1-string"]);
Assert.Equal(7, workflow.Context["key2-int"]);

Assert.Equal("input", Encoding.UTF8.GetString(data));

return Task.FromResult("output"u8.ToArray());
});
await workflowGlobalTransaction.Execute(wfName, Guid.NewGuid().ToString(), "input"u8.ToArray(), workflow =>
{
workflow.Context.Add("key1-string", "value1");
workflow.Context.Add("key2-int", 7);
});
}
}
}
Loading