Skip to content

Commit 106283b

Browse files
author
divotchenko.gg
committed
Set CompleteTime to WorkflowInstance when terminating workflow.
1 parent 7a8b01f commit 106283b

File tree

4 files changed

+31
-26
lines changed

4 files changed

+31
-26
lines changed

src/WorkflowCore/Models/WorkflowInstance.cs

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
43

54
namespace WorkflowCore.Models
@@ -36,11 +35,11 @@ public bool IsBranchComplete(string parentId)
3635
}
3736
}
3837

39-
public enum WorkflowStatus
38+
public enum WorkflowStatus
4039
{
41-
Runnable = 0,
42-
Suspended = 1,
43-
Complete = 2,
44-
Terminated = 3
40+
Runnable = 0,
41+
Suspended = 1,
42+
Complete = 2,
43+
Terminated = 3,
4544
}
4645
}

src/WorkflowCore/Services/ErrorHandlers/TerminateHandler.cs

+8-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Collections.Generic;
3-
using System.Text;
3+
44
using WorkflowCore.Interface;
55
using WorkflowCore.Models;
66
using WorkflowCore.Models.LifeCycleEvents;
@@ -10,21 +10,23 @@ namespace WorkflowCore.Services.ErrorHandlers
1010
public class TerminateHandler : IWorkflowErrorHandler
1111
{
1212
private readonly ILifeCycleEventPublisher _eventPublisher;
13-
private readonly IDateTimeProvider _datetimeProvider;
13+
private readonly IDateTimeProvider _dateTimeProvider;
1414
public WorkflowErrorHandling Type => WorkflowErrorHandling.Terminate;
1515

16-
public TerminateHandler(ILifeCycleEventPublisher eventPublisher, IDateTimeProvider datetimeProvider)
16+
public TerminateHandler(ILifeCycleEventPublisher eventPublisher, IDateTimeProvider dateTimeProvider)
1717
{
1818
_eventPublisher = eventPublisher;
19-
_datetimeProvider = datetimeProvider;
19+
_dateTimeProvider = dateTimeProvider;
2020
}
2121

2222
public void Handle(WorkflowInstance workflow, WorkflowDefinition def, ExecutionPointer pointer, WorkflowStep step, Exception exception, Queue<ExecutionPointer> bubbleUpQueue)
2323
{
2424
workflow.Status = WorkflowStatus.Terminated;
25-
_eventPublisher.PublishNotification(new WorkflowTerminated()
25+
workflow.CompleteTime = _dateTimeProvider.UtcNow;
26+
27+
_eventPublisher.PublishNotification(new WorkflowTerminated
2628
{
27-
EventTimeUtc = _datetimeProvider.UtcNow,
29+
EventTimeUtc = _dateTimeProvider.UtcNow,
2830
Reference = workflow.Reference,
2931
WorkflowInstanceId = workflow.Id,
3032
WorkflowDefinitionId = workflow.WorkflowDefinitionId,

src/WorkflowCore/Services/WorkflowController.cs

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using System;
22
using System.Linq;
3-
using System.Reflection;
43
using System.Threading;
54
using System.Threading.Tasks;
5+
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Logging;
88
using WorkflowCore.Exceptions;
@@ -47,10 +47,10 @@ public Task<string> StartWorkflow(string workflowId, int? version, object data =
4747
return StartWorkflow<object>(workflowId, version, data, reference);
4848
}
4949

50-
public Task<string> StartWorkflow<TData>(string workflowId, TData data = null, string reference=null)
50+
public Task<string> StartWorkflow<TData>(string workflowId, TData data = null, string reference = null)
5151
where TData : class, new()
5252
{
53-
return StartWorkflow<TData>(workflowId, null, data, reference);
53+
return StartWorkflow(workflowId, null, data, reference);
5454
}
5555

5656
public async Task<string> StartWorkflow<TData>(string workflowId, int? version, TData data = null, string reference=null)
@@ -204,7 +204,10 @@ public async Task<bool> TerminateWorkflow(string workflowId)
204204
try
205205
{
206206
var wf = await _persistenceStore.GetWorkflowInstance(workflowId);
207+
207208
wf.Status = WorkflowStatus.Terminated;
209+
wf.CompleteTime = _dateTimeProvider.UtcNow;
210+
208211
await _persistenceStore.PersistWorkflow(wf);
209212
await _queueProvider.QueueWork(workflowId, QueueType.Index);
210213
await _eventHub.PublishNotification(new WorkflowTerminated()
@@ -226,16 +229,16 @@ await _eventHub.PublishNotification(new WorkflowTerminated()
226229
public void RegisterWorkflow<TWorkflow>()
227230
where TWorkflow : IWorkflow
228231
{
229-
TWorkflow wf = ActivatorUtilities.CreateInstance<TWorkflow>(_serviceProvider);
232+
var wf = ActivatorUtilities.CreateInstance<TWorkflow>(_serviceProvider);
230233
_registry.RegisterWorkflow(wf);
231234
}
232235

233236
public void RegisterWorkflow<TWorkflow, TData>()
234237
where TWorkflow : IWorkflow<TData>
235238
where TData : new()
236239
{
237-
TWorkflow wf = ActivatorUtilities.CreateInstance<TWorkflow>(_serviceProvider);
238-
_registry.RegisterWorkflow<TData>(wf);
240+
var wf = ActivatorUtilities.CreateInstance<TWorkflow>(_serviceProvider);
241+
_registry.RegisterWorkflow(wf);
239242
}
240243
}
241244
}

src/WorkflowCore/Services/WorkflowExecutor.cs

+9-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
using Microsoft.Extensions.Logging;
2-
using System;
1+
using System;
32
using System.Collections.Generic;
43
using System.Linq;
54
using System.Threading;
65
using System.Threading.Tasks;
6+
7+
using Microsoft.Extensions.Logging;
78
using Microsoft.Extensions.DependencyInjection;
89
using WorkflowCore.Interface;
910
using WorkflowCore.Models;
@@ -62,7 +63,7 @@ public async Task<WorkflowExecutorResult> Execute(WorkflowInstance workflow, Can
6263
{
6364
_logger.LogError("Unable to find step {0} in workflow definition", pointer.StepId);
6465
pointer.SleepUntil = _datetimeProvider.UtcNow.Add(_options.ErrorRetryInterval);
65-
wfResult.Errors.Add(new ExecutionError()
66+
wfResult.Errors.Add(new ExecutionError
6667
{
6768
WorkflowId = workflow.Id,
6869
ExecutionPointerId = pointer.Id,
@@ -82,7 +83,7 @@ public async Task<WorkflowExecutorResult> Execute(WorkflowInstance workflow, Can
8283
catch (Exception ex)
8384
{
8485
_logger.LogError(ex, "Workflow {0} raised error on step {1} Message: {2}", workflow.Id, pointer.StepId, ex.Message);
85-
wfResult.Errors.Add(new ExecutionError()
86+
wfResult.Errors.Add(new ExecutionError
8687
{
8788
WorkflowId = workflow.Id,
8889
ExecutionPointerId = pointer.Id,
@@ -116,7 +117,7 @@ private bool InitializeStep(WorkflowInstance workflow, WorkflowStep step, Workfl
116117
if (pointer.Status != PointerStatus.Running)
117118
{
118119
pointer.Status = PointerStatus.Running;
119-
_publisher.PublishNotification(new StepStarted()
120+
_publisher.PublishNotification(new StepStarted
120121
{
121122
EventTimeUtc = _datetimeProvider.UtcNow,
122123
Reference = workflow.Reference,
@@ -159,12 +160,12 @@ private async Task ExecuteStep(WorkflowInstance workflow, WorkflowStep step, Exe
159160
{
160161
_logger.LogError("Unable to construct step body {0}", step.BodyType.ToString());
161162
pointer.SleepUntil = _datetimeProvider.UtcNow.Add(_options.ErrorRetryInterval);
162-
wfResult.Errors.Add(new ExecutionError()
163+
wfResult.Errors.Add(new ExecutionError
163164
{
164165
WorkflowId = workflow.Id,
165166
ExecutionPointerId = pointer.Id,
166167
ErrorTime = _datetimeProvider.UtcNow,
167-
Message = $"Unable to construct step body {step.BodyType.ToString()}"
168+
Message = $"Unable to construct step body {step.BodyType}"
168169
});
169170
return;
170171
}
@@ -253,7 +254,7 @@ private async Task DetermineNextExecutionTime(WorkflowInstance workflow, Workflo
253254
await middlewareRunner.RunPostMiddleware(workflow, def);
254255
}
255256

256-
_publisher.PublishNotification(new WorkflowCompleted()
257+
_publisher.PublishNotification(new WorkflowCompleted
257258
{
258259
EventTimeUtc = _datetimeProvider.UtcNow,
259260
Reference = workflow.Reference,

0 commit comments

Comments
 (0)