Exemplo n.º 1
0
        private bool Run()
        {
            var workflowInstances = TheWorkflowInstanceRepository.List();
            var actions           = 0;

            foreach (var workflowInstance in workflowInstances)
            {
                var hydratedInstance = TheWorkflowInstanceRepository.RestoreState(workflowInstance);

                if (hydratedInstance.Ended)
                {
                    Log.Debug(string.Format("Ending workflow '{0}' and removing from runtime.", hydratedInstance.Id.ToString(CultureInfo.InvariantCulture)));
                    TheWorkflowInstanceRepository.Delete(hydratedInstance);
                }

                var task = hydratedInstance.CurrentTask;
                if (task == null)
                {
                    continue;
                }
                Log.Debug(string.Format("Checking task '{0}' for runnable", task.Name));

                if (task is IRunnableWorkflowTask)
                {
                    try
                    {
                        ((IRunnableWorkflowTask)task).Run(hydratedInstance, this);
                        actions++;
                    }
                    catch (System.Exception ex)
                    {
                        Log.Error("An error occured during a runnable task (workflow will terminate) - " + task.GetType().FullName, ex);
                        Log.Error(ex.StackTrace);

                        EndWorkflow(hydratedInstance);
                        TheWorkflowInstanceRepository.Update(hydratedInstance);

                        actions++;
                    }
                }
                else if (task is IDelayWorkflowTask)
                {
                    if (((IDelayWorkflowTask)task).IsComplete())
                    {
                        Log.Debug("Delay task completed");
                        Transition(hydratedInstance, task, task.AvailableTransitions[0]);
                        actions++;
                    }
                }
                else if (task is EndWorkflowTask || typeof(EndWorkflowTask) == task.GetType())
                {
                    Log.Debug(string.Format("Workflow {0} will end as it entered an end task", hydratedInstance.Id));
                    EndWorkflow(hydratedInstance);
                    TheWorkflowInstanceRepository.Update(hydratedInstance);
                    actions++;
                }
            }

            return(actions > 0);
        }
Exemplo n.º 2
0
        public void Start(IWorkflowInstance workflowInstance)
        {
            if (workflowInstance.Started)
            {
                LogAndThrow(new WorkflowInstanceAlreadyStartedException(string.Format("Workflow instance '{0}' has already been started", workflowInstance.Id)));
            }

            workflowInstance.Started = true;
            var startTasks = workflowInstance.Tasks.Where(task => task.IsStartTask).ToList();

            if (startTasks.Count == 0)
            {
                LogAndThrow(new WorkflowInstanceHasNoStartTaskException(string.Format("Workflow instance '{0}' does not have a start task", workflowInstance.Id)));
            }

            if (startTasks.Count > 1)
            {
                LogAndThrow(new WorkflowInstanceHasTooManyStartTasksException(string.Format("Workflow instance '{0}' has more than one start task", workflowInstance.Id)));
            }

            workflowInstance.StartTime   = DateTime.Now;
            workflowInstance.CurrentTask = startTasks[0];

            if (workflowInstance.CurrentTask is IDelayWorkflowTask)
            {
                ((IDelayWorkflowTask)workflowInstance.CurrentTask).StartTime = DateTime.Now;
            }

            TheWorkflowInstanceRepository.Update(workflowInstance);
        }
        public IWorkflowInstance Instantiate(int workflowConfigurationId)
        {
            var workflowInstance = CreateInstance(workflowConfigurationId);

            TheWorkflowInstanceRepository.Create(workflowInstance);
            return(workflowInstance);
        }
        public IWorkflowInstance Instantiate(int workflowConfigurationId, IDictionary <string, object> properties)
        {
            var workflowInstance = CreateInstance(workflowConfigurationId);

            TheWorkflowInstanceRepository.Create(workflowInstance);
            return(workflowInstance);
        }
        public IWorkflowInstance GetInstance(int workflowInstanceId)
        {
            var workflowInstance         = TheWorkflowInstanceRepository.GetById(workflowInstanceId);
            var hyrdatedWorkflowInstance = TheWorkflowInstanceRepository.RestoreState(workflowInstance);

            return(hyrdatedWorkflowInstance);
        }
        public IList <IWorkflowInstance> ListInstances()
        {
            var workflowInstances    = new List <IWorkflowInstance>();
            var allWorkflowInstances = TheWorkflowInstanceRepository.List();

            foreach (var instance in allWorkflowInstances)
            {
                if (instance.Started && !instance.Ended)
                {
                    try
                    {
                        var i = TheWorkflowInstanceRepository.RestoreState(instance);
                        workflowInstances.Add(i);
                    }
                    catch (System.Exception ex)
                    {
                        Log.Warn("Restoring instance", ex);
                    }
                }
                else
                {
                    workflowInstances.Add(instance);
                }
            }
            return(workflowInstances);
        }
        public void Start(int workflowInstanceId)
        {
            var workflowInstance = TheWorkflowInstanceRepository.GetById(workflowInstanceId);
            var hydratedInstance = TheWorkflowInstanceRepository.RestoreState(workflowInstance);

            TheWorkflowRuntime.Start(hydratedInstance);
        }
        public IWorkflowInstance Instantiate(int workflowConfigurationId, string comment)
        {
            var workflowInstance = Instantiate(workflowConfigurationId);

            workflowInstance.Comment = comment;
            Log.Info(string.Format("Instantiation comment for '{0}' '{1}' -> '{2}'", workflowInstance.Name, workflowInstance.Id, comment));

            TheWorkflowInstanceRepository.Update(workflowInstance);
            return(workflowInstance);
        }
        public IList <IWorkflowInstance> ListInstances()
        {
            var workflowInstances    = new List <IWorkflowInstance>();
            var allWorkflowInstances = TheWorkflowInstanceRepository.List();

            foreach (var instance in allWorkflowInstances)
            {
                if (instance.Started && !instance.Ended)
                {
                    workflowInstances.Add(TheWorkflowInstanceRepository.RestoreState(instance));
                }
                else
                {
                    workflowInstances.Add(instance);
                }
            }
            return(workflowInstances);
        }
Exemplo n.º 10
0
        public void Transition(IWorkflowInstance workflowInstance, IWorkflowTask workflowTask, string transitionName, string comment)
        {
            if (workflowTask.Transitions.Values.Count == 0)
            {
                Log.Warn(string.Format("Ending workflow '{0}' as it has no transitions.", workflowInstance.Id));
                EndWorkflow(workflowInstance);
                TheWorkflowInstanceRepository.Update(workflowInstance);
                return;
            }

            if (!workflowTask.Transitions.ContainsKey(transitionName))
            {
                LogAndThrow(new WorkflowTaskInvalidTransitionException(string.Format("Inavlid transition '{0}' for task '{1}'", transitionName, workflowTask.Name)));
            }

            var transitionGuid = workflowTask.Transitions[transitionName];
            var transitionTask = workflowInstance.Tasks.SingleOrDefault(task => task.Id == transitionGuid);

            if (transitionTask == null)
            {
                LogAndThrow(new WorkflowTaskInvalidTransitionException(string.Format("Can't transition to task '{0}' in workflow '{1}' as it is not found", transitionGuid.ToString(), workflowInstance.Id)));
            }

            Log.Info(string.Format("Transitioning workflow '{0}' - '{1}' from '{2}' to '{3}' using transition '{4}'", workflowInstance.Name, workflowInstance.Id, workflowInstance.CurrentTask.Name, transitionTask.Name, transitionName));

            workflowInstance.TransitionHistory.Add(string.Format("{0} - Transition '{1}' - '{2}' -> '{3}' {4}", DateTime.Now, transitionName, workflowInstance.CurrentTask.Name, transitionTask.Name, comment));

            workflowInstance.CurrentTask = transitionTask;

            if (workflowInstance.CurrentTask is IDelayWorkflowTask)
            {
                ((IDelayWorkflowTask)workflowInstance.CurrentTask).StartTime = DateTime.Now;
            }

            TheWorkflowInstanceRepository.Update(workflowInstance);
        }
        public void DeleteWorkflowInstance(int workflowInstanceId)
        {
            var workflowInstance = TheWorkflowInstanceRepository.GetById(workflowInstanceId);

            TheWorkflowInstanceRepository.Delete(workflowInstance);
        }
 public void Update(IWorkflowInstance workflowInstance)
 {
     TheWorkflowInstanceRepository.Update(workflowInstance);
 }