예제 #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);
        }
        public void DeleteWorkflowInstance(int workflowInstanceId)
        {
            var workflowInstance = TheWorkflowInstanceRepository.GetById(workflowInstanceId);

            TheWorkflowInstanceRepository.Delete(workflowInstance);
        }