forked from dtm-labs/client-csharp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWorkflowGlobalTransaction.cs
More file actions
70 lines (58 loc) · 2.17 KB
/
WorkflowGlobalTransaction.cs
File metadata and controls
70 lines (58 loc) · 2.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace Dtmworkflow
{
public class WorkflowGlobalTransaction
{
private readonly Dictionary<string, WfItem> _handlers;
private readonly IWorkflowFactory _workflowFactory;
private readonly ILogger _logger;
public WorkflowGlobalTransaction(IWorkflowFactory workflowFactory, ILoggerFactory loggerFactory)
{
this._handlers = new Dictionary<string, WfItem>();
this._workflowFactory = workflowFactory;
this._logger = loggerFactory.CreateLogger<WorkflowGlobalTransaction>();
}
public async Task<byte[]> Execute(string name, string gid, byte[] data, 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);
foreach (var fn in handler.Custom)
{
fn(wf);
}
return await wf.Process(handler.Fn, data);
}
public void Register(string name, WfFunc2 handler, params Action<Workflow>[] custom)
{
if (this._handlers.TryGetValue(name, out _))
{
throw new DtmCommon.DtmException($"a handler already exists for {name}");
}
_logger.LogDebug("workflow '{0}' registered.", name);
this._handlers.Add(name, new WfItem
{
Fn = handler,
Custom = custom.ToList()
});
}
#if NET5_0_OR_GREATER
public async Task ExecuteByQS(Microsoft.AspNetCore.Http.IQueryCollection query, byte[] body)
{
_ = query.TryGetValue("gid", out var gid);
_ = query.TryGetValue("op", out var op);
await Execute(op, gid, body, true);
}
#endif
public bool Exists(string name)
{
return this._handlers.ContainsKey(name);
}
}
}