Skip to content

Issue 273 #281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions src/WorkflowCore/Services/CancellationProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ public void ProcessCancellations(WorkflowInstance workflow, WorkflowDefinition w

foreach (var ptr in toCancel)
{
ptr.EndTime = DateTime.Now.ToUniversalTime();
ptr.Active = false;
ptr.Status = PointerStatus.Cancelled;

if (step.ProceedOnCancel)
{
_executionResultProcessor.ProcessExecutionResult(workflow, workflowDef, ptr, step, ExecutionResult.Next(), executionResult);
}


ptr.EndTime = DateTime.Now.ToUniversalTime();
ptr.Active = false;
ptr.Status = PointerStatus.Cancelled;

foreach (var descendent in workflow.ExecutionPointers.FindByScope(ptr.Id).Where(x => x.Status != PointerStatus.Complete && x.Status != PointerStatus.Cancelled))
{
descendent.EndTime = DateTime.Now.ToUniversalTime();
Expand Down
191 changes: 101 additions & 90 deletions src/WorkflowCore/Services/WorkflowExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,12 @@ public async Task<WorkflowExecutorResult> Execute(WorkflowInstance workflow)
_logger.LogError("Workflow {0} version {1} is not registered", workflow.WorkflowDefinitionId, workflow.Version);
return wfResult;
}

_cancellationProcessor.ProcessCancellations(workflow, def, wfResult);

foreach (var pointer in exePointers)
{
if (pointer.Status == PointerStatus.Cancelled)
if (!pointer.Active)
continue;

var step = def.Steps.FindById(pointer.StepId);
Expand All @@ -64,97 +66,17 @@ public async Task<WorkflowExecutorResult> Execute(WorkflowInstance workflow)
WorkflowId = workflow.Id,
ExecutionPointerId = pointer.Id,
ErrorTime = _datetimeProvider.Now.ToUniversalTime(),
Message = String.Format("Unable to find step {0} in workflow definition", pointer.StepId)
Message = $"Unable to find step {pointer.StepId} in workflow definition"
});
continue;
}

try
{
switch (step.InitForExecution(wfResult, def, workflow, pointer))
{
case ExecutionPipelineDirective.Defer:
continue;
case ExecutionPipelineDirective.EndWorkflow:
workflow.Status = WorkflowStatus.Complete;
workflow.CompleteTime = _datetimeProvider.Now.ToUniversalTime();
continue;
}

if (pointer.Status != PointerStatus.Running)
{
pointer.Status = PointerStatus.Running;
_publisher.PublishNotification(new StepStarted()
{
EventTimeUtc = _datetimeProvider.Now,
Reference = workflow.Reference,
ExecutionPointerId = pointer.Id,
StepId = step.Id,
WorkflowInstanceId = workflow.Id,
WorkflowDefinitionId = workflow.WorkflowDefinitionId,
Version = workflow.Version
});
}

if (!pointer.StartTime.HasValue)
{
pointer.StartTime = _datetimeProvider.Now.ToUniversalTime();
}
{
if (!InitializeStep(workflow, step, wfResult, def, pointer))
continue;

using (var scope = _scopeProvider.CreateScope())
{
_logger.LogDebug("Starting step {0} on workflow {1}", step.Name, workflow.Id);

IStepBody body = step.ConstructBody(scope.ServiceProvider);

if (body == null)
{
_logger.LogError("Unable to construct step body {0}", step.BodyType.ToString());
pointer.SleepUntil = _datetimeProvider.Now.ToUniversalTime().Add(_options.ErrorRetryInterval);
wfResult.Errors.Add(new ExecutionError()
{
WorkflowId = workflow.Id,
ExecutionPointerId = pointer.Id,
ErrorTime = _datetimeProvider.Now.ToUniversalTime(),
Message = String.Format("Unable to construct step body {0}", step.BodyType.ToString())
});
continue;
}

IStepExecutionContext context = new StepExecutionContext()
{
Workflow = workflow,
Step = step,
PersistenceData = pointer.PersistenceData,
ExecutionPointer = pointer,
Item = pointer.ContextItem
};

foreach (var input in step.Inputs)
input.AssignInput(workflow.Data, body, context);


switch (step.BeforeExecute(wfResult, context, pointer, body))
{
case ExecutionPipelineDirective.Defer:
continue;
case ExecutionPipelineDirective.EndWorkflow:
workflow.Status = WorkflowStatus.Complete;
workflow.CompleteTime = _datetimeProvider.Now.ToUniversalTime();
continue;
}

var result = await body.RunAsync(context);

if (result.Proceed)
{
foreach (var output in step.Outputs)
output.AssignOutput(workflow.Data, body, context);
}

_executionResultProcessor.ProcessExecutionResult(workflow, def, pointer, step, result, wfResult);
step.AfterExecute(wfResult, context, result, pointer);
}
await ExecuteStep(workflow, step, pointer, wfResult, def);
}
catch (Exception ex)
{
Expand All @@ -171,16 +93,105 @@ public async Task<WorkflowExecutorResult> Execute(WorkflowInstance workflow)
Host.ReportStepError(workflow, step, ex);
}
_cancellationProcessor.ProcessCancellations(workflow, def, wfResult);



}
ProcessAfterExecutionIteration(workflow, def, wfResult);
DetermineNextExecutionTime(workflow);

return wfResult;
}


private bool InitializeStep(WorkflowInstance workflow, WorkflowStep step, WorkflowExecutorResult wfResult, WorkflowDefinition def, ExecutionPointer pointer)
{
switch (step.InitForExecution(wfResult, def, workflow, pointer))
{
case ExecutionPipelineDirective.Defer:
return false;
case ExecutionPipelineDirective.EndWorkflow:
workflow.Status = WorkflowStatus.Complete;
workflow.CompleteTime = _datetimeProvider.Now.ToUniversalTime();
return false;
}

if (pointer.Status != PointerStatus.Running)
{
pointer.Status = PointerStatus.Running;
_publisher.PublishNotification(new StepStarted()
{
EventTimeUtc = _datetimeProvider.Now,
Reference = workflow.Reference,
ExecutionPointerId = pointer.Id,
StepId = step.Id,
WorkflowInstanceId = workflow.Id,
WorkflowDefinitionId = workflow.WorkflowDefinitionId,
Version = workflow.Version
});
}

if (!pointer.StartTime.HasValue)
{
pointer.StartTime = _datetimeProvider.Now.ToUniversalTime();
}

return true;
}

private async Task ExecuteStep(WorkflowInstance workflow, WorkflowStep step, ExecutionPointer pointer, WorkflowExecutorResult wfResult, WorkflowDefinition def)
{
using (var scope = _scopeProvider.CreateScope())
{
_logger.LogDebug("Starting step {0} on workflow {1}", step.Name, workflow.Id);

IStepBody body = step.ConstructBody(scope.ServiceProvider);

if (body == null)
{
_logger.LogError("Unable to construct step body {0}", step.BodyType.ToString());
pointer.SleepUntil = _datetimeProvider.Now.ToUniversalTime().Add(_options.ErrorRetryInterval);
wfResult.Errors.Add(new ExecutionError()
{
WorkflowId = workflow.Id,
ExecutionPointerId = pointer.Id,
ErrorTime = _datetimeProvider.Now.ToUniversalTime(),
Message = $"Unable to construct step body {step.BodyType.ToString()}"
});
return;
}

IStepExecutionContext context = new StepExecutionContext()
{
Workflow = workflow,
Step = step,
PersistenceData = pointer.PersistenceData,
ExecutionPointer = pointer,
Item = pointer.ContextItem
};

foreach (var input in step.Inputs)
input.AssignInput(workflow.Data, body, context);

switch (step.BeforeExecute(wfResult, context, pointer, body))
{
case ExecutionPipelineDirective.Defer:
return;
case ExecutionPipelineDirective.EndWorkflow:
workflow.Status = WorkflowStatus.Complete;
workflow.CompleteTime = _datetimeProvider.Now.ToUniversalTime();
return;
}

var result = await body.RunAsync(context);

if (result.Proceed)
{
foreach (var output in step.Outputs)
output.AssignOutput(workflow.Data, body, context);
}

_executionResultProcessor.ProcessExecutionResult(workflow, def, pointer, step, result, wfResult);
step.AfterExecute(wfResult, context, result, pointer);
}
}

private void ProcessAfterExecutionIteration(WorkflowInstance workflow, WorkflowDefinition workflowDef, WorkflowExecutorResult workflowResult)
{
var pointers = workflow.ExecutionPointers.Where(x => x.EndTime == null);
Expand Down