Esempio n. 1
0
        private void ProcessTaskStatusChanged(WorkflowStatus status, WorkflowTask task)
        {
            task.Properties[WorkflowProperties.WP_STATUS] = status;

            if (this.TaskStatusChanged != null)
            {
                this.TaskStatusChanged(this, status, task);
            }
        }
Esempio n. 2
0
        /// <summary>
        /// 执行一个任务
        /// </summary>
        /// <param name="taskDef">要执行的任务信息</param>
        /// <param name="output">任务的输出参数</param>
        /// <returns></returns>
        private bool ExecuteTask(TaskDefinition taskDef)
        {
            WorkflowTask task        = null;
            bool         initialized = false;
            int          rc          = DotNETCode.SUCCESS;

            try
            {
                task = this.ModuleFactory.CreateInstance <WorkflowTask>(taskDef);

                // 如果Task有Disabled标记,那么跳过运行
                if (taskDef.HasFlag(ModuleFlags.Disabled))
                {
                    this.ProcessTaskStatusChanged(WorkflowStatus.SKIP, task);
                    logger.InfoFormat("{0}未启用, 跳过", taskDef.Name);
                    return(true);
                }

                // 延时运行
                if (taskDef.Delay > 0)
                {
                    logger.InfoFormat("延时{0}秒运行{1}", (double)taskDef.Delay / (double)1000, taskDef.Name);
                    this.ProcessTaskStatusChanged(WorkflowStatus.WAIT, task);
                    Thread.Sleep(taskDef.Delay);
                }

                logger.InfoFormat("开始运行:{0}", taskDef.Name);

                this.ProcessTaskStatusChanged(WorkflowStatus.RUN, task);

                // 解析参数表达式
                IDictionary inputParams;
                if ((inputParams = ExpressionUtility.EvaluateExpressions(taskDef.InputParameters, this.EvaluationContext)) == null)
                {
                    this.ProcessTaskStatusChanged(WorkflowStatus.FAIL, task);
                    logger.ErrorFormat("计算输入参数表达式失败, 运行任务失败, code = {0}, {1}", rc, DotNETCode.GetMessage(rc));
                    return(false);
                }

                if ((rc = task.Initialize(inputParams)) != DotNETCode.SUCCESS)
                {
                    this.ProcessTaskStatusChanged(WorkflowStatus.FAIL, task);
                    logger.ErrorFormat("初始化{0}失败, code = {1}, {2}", taskDef.Name, rc, DotNETCode.GetMessage(rc));
                    return(false);
                }

                initialized = true;

                if ((rc = task.Run()) != DotNETCode.SUCCESS)
                {
                    this.ProcessTaskStatusChanged(WorkflowStatus.FAIL, task);
                    logger.ErrorFormat("运行{0}失败, code = {1}, {2}", taskDef.Name, rc, DotNETCode.GetMessage(rc));
                    return(false);
                }

                this.ProcessTaskStatusChanged(WorkflowStatus.PASS, task);
                logger.InfoFormat("运行{0}成功", taskDef.Name);
                return(true);
            }
            catch (Exception ex)
            {
                logger.Error(string.Format("{0}出现异常", taskDef.Name), ex);
                if (taskDef.HasFlag((int)TaskFlags.IgnoreFailure))
                {
                    logger.WarnFormat("{0}运行出现异常, 执行下一个流程", taskDef.Name);
                }
                else
                {
                    logger.WarnFormat("{0}运行出现异常, 退出", taskDef.Name);
                }
                this.ProcessTaskStatusChanged(WorkflowStatus.EXCEPTION, task);
                return(false);
            }
            finally
            {
                if (task != null)
                {
                    // 保存输出参数,供其他的任务解析输入表达式使用
                    this.taskProperties[task.ID] = task.Properties;

                    if (initialized)
                    {
                        task.Reset();
                    }
                }
            }
        }