/// <summary> /// Two stage workflow execution start. /// First create and initialise the workflow variables /// Then schedule the first task /// If this process is interrupted then the variables /// </summary> /// <param name="db"></param> /// <param name="workflow"></param> /// <param name="execution"></param> /// <param name="history"></param> /// <param name="decisions"></param> private static void ProcessWorkflowExecutionStartedEvent(Database db, WorkflowObj workflow, Execution execution, History history, List <History> decisions) { var evt = WorkflowExecutionStartedEvent.Create(history); CreateVariables(db, evt, execution, workflow); // Ensure that the variables are set up for the first task // This should never fail due to conflict db.SubmitChanges(); // Scheduling the first task var startTaskId = workflow.Tasks.Single(t => t.ActivityName == "start").Outflows.Single(o => o.Name == "Out").Target; var startTask = workflow.Tasks.Single(t => t.TaskId == startTaskId); CreateTaskScheduledEvent(db, execution, startTask, decisions); }
/// <summary> /// Create all workflow variables and initialise them from the workflow input data /// </summary> /// <param name="db"></param> /// <param name="evt"></param> /// <param name="execution"></param> /// <param name="workflow"></param> private static void CreateVariables(Database db, WorkflowExecutionStartedEvent evt, Execution execution, WorkflowObj workflow) { foreach (var vdefn in workflow.Variables) { // Get the initial value of the variable JToken value = null; if (!string.IsNullOrWhiteSpace(vdefn.Value.Path)) { value = evt.Input.SelectToken(vdefn.Value.Path); if (value == null) // The path expression failed { if (vdefn.Value.Required) { throw new ApplicationException("Uninitialised required variable - " + vdefn.Key); } } // Empty default is equivalent to an explicit null if (IsNullOrUndefined(value) && !string.IsNullOrWhiteSpace(vdefn.Value.Default)) { value = JToken.Parse(vdefn.Value.Default); } } // Empty literal is equivalent to an explicit null else if (!string.IsNullOrWhiteSpace(vdefn.Value.Lit)) { value = JToken.Parse(vdefn.Value.Lit); } // TODO: Check datatypes for validity db.Variables.InsertOnSubmit(new Variable { ExecutionId = execution.ExecutionId, Name = vdefn.Key, Json = JsonConvert.SerializeObject(value) }); } }