private void IterateWorkflow(WorkflowRun <TWfContext> run) { if (_finish.Exists(precondition => precondition.Met(run.FinishedTasks))) { WorkflowFinished?.Invoke(this, new WorkflowEventArgs <TWfContext>(run.RunId, run.Context)); return; } var tasksToExecute = _tasks.Where(kv => kv.Value.Met(run.FinishedTasks) && !run.HasFinished(kv.Key)) .Select(kv => kv.Key).ToList(); var anythingFinished = false; foreach (var task in tasksToExecute) { task.Action(run.Context); run.StartTask(task); TaskStarted?.Invoke(this, new WorkflowTaskEventArgs <TWfContext>(run.RunId, task.TaskId, run.Context)); if (task.Autocomplete) { TaskFinished?.Invoke(this, new WorkflowTaskEventArgs <TWfContext>(run.RunId, task.TaskId, run.Context)); run.FinishTask(task); anythingFinished = true; } } if (anythingFinished) { IterateWorkflow(run); } }
public void ConsumeMessage <TMessage>(TMessage message) { if (_startsOn.ContainsKey(typeof(TMessage))) { var run = new WorkflowRun <TWfContext>( ((Func <TMessage, TWfContext>)_startsOn[typeof(TMessage)])(message)); _runs.Add(run); WorkflowStarted?.Invoke(this, new WorkflowEventArgs <TWfContext>(run.RunId, run.Context)); IterateWorkflow(run); return; } var runs = _runs.ToArray(); var tasksToFinish = _continuesOn.Where(kv => kv.Value == typeof(TMessage)).Select(kv => kv.Key); foreach (var workflowTask in tasksToFinish) { var messageBasedTask = (WorkflowTask <TWfContext, TMessage>)workflowTask; var runsToProcess = runs.Where(r => (messageBasedTask).WorkflowRunSelector(r.Context, message)) .Where(r => r.HasStarted(workflowTask)); foreach (var run in runsToProcess) { messageBasedTask.AfterMessageReceived?.Invoke(run.Context, message); TaskFinished?.Invoke(this, new WorkflowTaskEventArgs <TWfContext>(run.RunId, workflowTask.TaskId, run.Context)); run.FinishTask(workflowTask); IterateWorkflow(run); } } }