|
1 | | -namespace CaseManagement.CMMN.Infrastructures.Scheduler |
| 1 | +using CaseManagement.CMMN.Domains; |
| 2 | +using CaseManagement.Workflow.Domains; |
| 3 | +using CaseManagement.Workflow.Engine; |
| 4 | +using CaseManagement.Workflow.Infrastructure; |
| 5 | +using CaseManagement.Workflow.Infrastructure.EvtStore; |
| 6 | +using CaseManagement.Workflow.Infrastructure.Scheduler; |
| 7 | +using CaseManagement.Workflow.ISO8601; |
| 8 | +using System.Collections.Generic; |
| 9 | +using System.Linq; |
| 10 | +using System.Threading; |
| 11 | +using System.Threading.Tasks; |
| 12 | + |
| 13 | +namespace CaseManagement.CMMN.Infrastructures.Scheduler |
2 | 14 | { |
3 | | - public class CMMNTimerEventHandler |
| 15 | + public class CMMNTimerEventHandler : IScheduleJobHandler<TimerEventMessage> |
4 | 16 | { |
| 17 | + private readonly IProcessFlowElementProcessorFactory _factory; |
| 18 | + private readonly ICommitAggregateHelper _commitAggregateHelper; |
| 19 | + private readonly IEventStoreRepository _eventStoreRepository; |
| 20 | + |
| 21 | + public CMMNTimerEventHandler(IProcessFlowElementProcessorFactory factory, ICommitAggregateHelper commitAggregateHelper, IEventStoreRepository eventStoreRepository) |
| 22 | + { |
| 23 | + _factory = factory; |
| 24 | + _commitAggregateHelper = commitAggregateHelper; |
| 25 | + _eventStoreRepository = eventStoreRepository; |
| 26 | + } |
| 27 | + |
| 28 | + public async Task Handle(TimerEventMessage message, CancellationToken token) |
| 29 | + { |
| 30 | + var flowInstance = await _eventStoreRepository.GetLastAggregate<ProcessFlowInstance>(message.ProcessId, ProcessFlowInstance.GetStreamName(message.ProcessId)); |
| 31 | + if (flowInstance == null) |
| 32 | + { |
| 33 | + return; |
| 34 | + } |
| 35 | + |
| 36 | + try |
| 37 | + { |
| 38 | + var tasks = new List<Task>(); |
| 39 | + var element = flowInstance.Elements.First(e => e.Id == message.ElementId) as CMMNPlanItem; |
| 40 | + flowInstance.StartElement(element); |
| 41 | + flowInstance.OccurPlanItem(element); |
| 42 | + foreach(var nextElt in flowInstance.NextElements(element.Id)) |
| 43 | + { |
| 44 | + var context = new WorkflowHandlerContext(flowInstance, nextElt, _factory); |
| 45 | + tasks.Add(Task.Run(() => Start(context, token))); |
| 46 | + } |
| 47 | + |
| 48 | + await Task.WhenAll(tasks); |
| 49 | + flowInstance.CompleteElement(element); |
| 50 | + var nbOccures = element.TransitionHistories.Where(t => t.Transition == CMMNPlanItemTransitions.Occur).Count(); |
| 51 | + var timerEventListener = element.PlanItemDefinitionTimerEventListener; |
| 52 | + var repeatingInterval = ISO8601Parser.ParseRepeatingTimeInterval(timerEventListener.TimerExpression.Body); |
| 53 | + var time = ISO8601Parser.ParseTime(timerEventListener.TimerExpression.Body); |
| 54 | + if ((repeatingInterval.RecurringTimeInterval == nbOccures || time != null) && flowInstance.IsFinished()) |
| 55 | + { |
| 56 | + if (flowInstance.IsFinished()) |
| 57 | + { |
| 58 | + flowInstance.Complete(); |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + finally |
| 63 | + { |
| 64 | + await _commitAggregateHelper.Commit(flowInstance, flowInstance.GetStreamName()); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + private async Task Start(WorkflowHandlerContext context, CancellationToken token) |
| 69 | + { |
| 70 | + var processor = _factory.Build(context.CurrentElement); |
| 71 | + await processor.Handle(context, token); |
| 72 | + if (context.CurrentElement.Status != ProcessFlowInstanceElementStatus.Finished) |
| 73 | + { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + var nextElts = context.ProcessFlowInstance.NextElements(context.CurrentElement.Id); |
| 78 | + if (!nextElts.Any()) |
| 79 | + { |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + foreach (var nextElt in nextElts) |
| 84 | + { |
| 85 | + await Start(context, token); |
| 86 | + } |
| 87 | + } |
5 | 88 | } |
6 | 89 | } |
0 commit comments