Skip to content

Commit 299dd37

Browse files
authored
Merge pull request danielgerlag#760 from xlegalles/ExtendRunWorkflowSync-
Extend RunWorkflowSync
2 parents 24f05e3 + 4d3181f commit 299dd37

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

src/WorkflowCore/Interface/ISyncWorkflowRunner.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Threading;
23
using System.Threading.Tasks;
34
using WorkflowCore.Models;
45

@@ -8,5 +9,8 @@ public interface ISyncWorkflowRunner
89
{
910
Task<WorkflowInstance> RunWorkflowSync<TData>(string workflowId, int version, TData data, string reference, TimeSpan timeOut, bool persistSate = true)
1011
where TData : new();
12+
13+
Task<WorkflowInstance> RunWorkflowSync<TData>(string workflowId, int version, TData data, string reference, CancellationToken token, bool persistSate = true)
14+
where TData : new();
1115
}
1216
}

src/WorkflowCore/Services/SyncWorkflowRunner.cs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,15 @@ public SyncWorkflowRunner(IWorkflowHost host, IWorkflowExecutor executor, IDistr
3232
_dateTimeProvider = dateTimeProvider;
3333
}
3434

35-
public async Task<WorkflowInstance> RunWorkflowSync<TData>(string workflowId, int version, TData data, string reference, TimeSpan timeOut, bool persistSate = true)
35+
public Task<WorkflowInstance> RunWorkflowSync<TData>(string workflowId, int version, TData data,
36+
string reference, TimeSpan timeOut, bool persistSate = true)
37+
where TData : new()
38+
{
39+
return RunWorkflowSync(workflowId, version, data, reference, new CancellationTokenSource(timeOut).Token,
40+
persistSate);
41+
}
42+
43+
public async Task<WorkflowInstance> RunWorkflowSync<TData>(string workflowId, int version, TData data, string reference, CancellationToken token, bool persistSate = true)
3644
where TData : new()
3745
{
3846
var def = _registry.GetDefinition(workflowId, version);
@@ -63,8 +71,6 @@ public async Task<WorkflowInstance> RunWorkflowSync<TData>(string workflowId, in
6371

6472
wf.ExecutionPointers.Add(_pointerFactory.BuildGenesisPointer(def));
6573

66-
var stopWatch = new Stopwatch();
67-
6874
var id = Guid.NewGuid().ToString();
6975

7076
if (persistSate)
@@ -81,8 +87,7 @@ public async Task<WorkflowInstance> RunWorkflowSync<TData>(string workflowId, in
8187

8288
try
8389
{
84-
stopWatch.Start();
85-
while ((wf.Status == WorkflowStatus.Runnable) && (timeOut.TotalMilliseconds > stopWatch.ElapsedMilliseconds))
90+
while ((wf.Status == WorkflowStatus.Runnable) && !token.IsCancellationRequested)
8691
{
8792
await _executor.Execute(wf);
8893
if (persistSate)
@@ -91,7 +96,6 @@ public async Task<WorkflowInstance> RunWorkflowSync<TData>(string workflowId, in
9196
}
9297
finally
9398
{
94-
stopWatch.Stop();
9599
await _lockService.ReleaseLock(id);
96100
}
97101

0 commit comments

Comments
 (0)