Exemple #1
0
        /// <summary>
        /// 开始工作
        /// </summary>
        private bool ExecuteMeanwhile(ITaskschedStrategy strategy, TaskschedNode task, ExecutingNode[] nodes, ExecutingNode node, IList<KeyValueTuple<IWorkStep, string>> worksteps, WorkContext context, IWorkStepMessage preMsg)
        {
            var finishNodes = new List<ExecutingNode>(nodes.Count());
            var messages = new MeanwhileWorkStepMessage();
            var msgs = new List<IWorkStepMessage>(nodes.Count());
            messages.TaskId = task.TaskId;

            IWorkStepMessage msg = null;
            var timeSpan = TimeSpan.Zero;
            Exception lastException = null;

            foreach (var workstep in worksteps)
            {
                try
                {
                    msg = workstep.Key.Execute(context, preMsg);
                    if (msg != null)
                    {
                        task.AttachState = msg.AttachState;
                    }
                }
                catch (Exception ex)
                {
                    lastException = ex;
                    node.ExecutingMessage = ex.GetInnerException().GetFullMessage();
                    node.FailTimes++;
                    timeSpan = strategy.NextTime(task, node, ex);
                }
                if (msg is EmptyAndAbortedWorkStepMessage)
                {
                    foreach (var t in nodes.Where(t => t.OrderNo >= node.OrderNo))
                    {
                        if (t.OrderNo == 0 || t.OrderNo == nodes.Length - 1)
                        {
                            continue;
                        }

                        t.ExecutingMessage = string.Format("当前步骤{0}执行了中断行为,消息结果为:{1}", node.OrderNo, ((EmptyAndAbortedWorkStepMessage)msg).Message);
                        finishNodes.Add(t);
                    }

                    this.executingRepository.Abort(task, finishNodes.ToArray());
                    if (this.MessagePublisher != null)
                    {
                        try
                        {
                            this.MessagePublisher.PublishMessageAsync(msg);
                        }
                        catch
                        {
                        }
                    }
                    return false;
                }
                if (msg is EmptyButWaitingWorkStepMessage)
                {
                    var span = strategy.NextWorkTimeOnMessageIsWaiting(task, node);
                    node.ExecutingMessage = ((EmptyButWaitingWorkStepMessage)msg).Message;
                    task.NextTime = task.NextTime.HasValue ? task.NextTime.Value.Add(span) : DateTime.Now.Add(span);
                    this.executingRepository.NextWork(task, node);
                    if (this.MessagePublisher != null)
                    {
                        try
                        {
                            this.MessagePublisher.PublishMessageAsync(msg);
                        }
                        catch
                        {
                        }
                    }
                    return false;
                }

                if (lastException != null && this.MessagePublisher != null)
                {
                    try
                    {
                        this.MessagePublisher.PublishMessageAsync(new ExecutingErrorWorkStepMessage() { TaskId = task.TaskId, CommandType = task.CommandType, UserSign = task.UserSign, Exception = lastException, PushMessage = task.PushMessage });
                    }
                    finally
                    {
                    }
                }

                if (timeSpan != TimeSpan.Zero)
                {
                    task.NextTime = task.NextTime.HasValue ? task.NextTime.Value.Add(timeSpan) : DateTime.Now.Add(timeSpan);
                    this.executingRepository.Exception(task, node);
                    return false;
                }

                if (msg == null)
                {
                    timeSpan = strategy.NextWorkTimeOnMessageIsNull(task, node, node.WaitTimes + 1);
                    if (timeSpan != TimeSpan.Zero)
                    {
                        node.WaitTimes++;
                        task.NextTime = task.NextTime.HasValue ? task.NextTime.Value.Add(timeSpan) : DateTime.Now.Add(timeSpan);
                        this.executingRepository.NextWork(task, node);
                        return false;
                    }

                    foreach (var t in nodes.Where(t => t.OrderNo >= node.OrderNo))
                    {
                        if (t.OrderNo == 0 || t.OrderNo == nodes.Length - 1)
                        {
                            continue;
                        }

                        t.ExecutingMessage = string.Format("当前步骤{0}协同方式为全部,但工作实例{1}返回空消息,执行中断", node.OrderNo, workstep.Value);
                        finishNodes.Add(t);
                    }

                    this.executingRepository.Abort(task, finishNodes.ToArray());
                    return false;
                }

                if (msg.TaskId == Guid.Empty)
                {
                    this.SetMessageTaskId(msg, task);
                }

                msgs.Add(msg);

                messages.Messages.Add(this.engine.JsonSerializer.Serialize(new Never.Messages.MessagePacket()
                {
                    ContentType = Never.Messages.MessagePacket.GetContentType(msg),
                    Body = this.engine.JsonSerializer.Serialize(msg)
                }));
            }

            var mulitePacket = new Never.Messages.MessagePacket()
            {
                ContentType = Never.Messages.MessagePacket.GetContentType(messages),
                Body = this.engine.JsonSerializer.Serialize(messages),
            };

            context.StepClear();
            node.ResultMessage = this.engine.JsonSerializer.Serialize(mulitePacket);
            node.FinishTime = DateTime.Now;
            task.ProcessPercent = task.StepCount <= 0 ? 100m : ((100 * (task.NextStep) / ((decimal)task.StepCount)).FormatR(2));

            if (task.NextStep == task.StepCount)
            {
                task.FinishTime = DateTime.Now;
                this.executingRepository.AllDone(task, node);
            }
            else
            {
                task.NextStep = task.NextStep + 1;
                this.executingRepository.Done(task, node);
            }

            if (this.MessagePublisher != null)
            {
                try
                {
                    foreach (var m in msgs)
                    {
                        this.MessagePublisher.PublishMessageAsync(m);
                    }
                }
                catch
                {
                }
            }

            return true;
        }
Exemple #2
0
        /// <summary>
        /// 将当前激发的消息压入队列中,开始进行按模板的步骤进行工作流,并返回当前模板组织的节点信息
        /// </summary>
        /// <param name="commandType">命令类型,长度不能超过20个字符</param>
        /// <param name="userSign">用户标识,长度不能超过80个字符</param>
        /// <param name="templateName"></param>
        /// <param name="message"></param>
        /// <returns></returns>
        public TaskschedNode Push <TWorkStepMessage>(string templateName, string userSign, string commandType, TWorkStepMessage message) where TWorkStepMessage : IWorkStepMessage
        {
            var template = this.templateRepository.Get(this.workflowengine.JsonSerializer, templateName);

            if (template == null)
            {
                throw new TemplateNotFoundException("没有找到该{0}模板内容", new[] { templateName });
            }

            if (commandType.IsNullOrWhiteSpace())
            {
                throw new WorkFlowKeyTooLongException("commandType is null");
            }

            if (commandType.Length > 20)
            {
                throw new WorkFlowKeyTooLongException("commandType长度超过了20个字符");
            }

            if (userSign.IsNotNullOrEmpty() && userSign.Length > 80)
            {
                throw new WorkFlowKeyTooLongException("userSign长度超过了80个字符");
            }

            var task = new TaskschedNode()
            {
                TaskId         = NewId.GenerateGuid(),
                ProcessPercent = 0m,
                StartTime      = DateTime.Now.AddSeconds(10),
                NextStep       = 1,
                Status         = TaskschedStatus.Working,
                UserSign       = userSign,
                UserSignState  = this.GetASCII(userSign),
                CommandType    = commandType,
                StepCount      = template.StepCount - 2,
            };

            task.PushMessage = this.SerializeObject(task, message);
            var nodes = new List <ExecutingNode>(task.StepCount - 2);

            foreach (var step in template.Steps)
            {
                if (step.Order == 0)
                {
                    continue;
                }

                if (step.Order == template.StepCount - 1)
                {
                    continue;
                }

                var node = new ExecutingNode()
                {
                    TaskId               = task.TaskId,
                    StepCount            = task.StepCount - 2,
                    RowId                = NewId.GenerateGuid(),
                    OrderNo              = step.Order,
                    StepType             = step.ToStepType(),
                    StepCoordinationMode = step.Mode,
                    StartTime            = DateTime.Now,
                    FailTimes            = 0,
                    ExecutingMessage     = string.Empty,
                    ResultMessage        = string.Empty,
                };

                nodes.Add(node);
            }

            this.excutingRepository.Save(task, nodes.ToArray());
            return(task);
        }
Exemple #3
0
        /// <summary>
        /// 检查工作
        /// </summary>
        /// <param name="strategy"></param>
        /// <param name="task"></param>
        /// <param name="nodes"></param>
        /// <param name="node"></param>
        /// <param name="worksteps"></param>
        /// <param name="preMsg"></param>
        /// <returns></returns>
        private bool IsReady(ITaskschedStrategy strategy, TaskschedNode task, ExecutingNode[] nodes, ExecutingNode node, IList<KeyValueTuple<IWorkStep, string>> worksteps, out IWorkStepMessage preMsg)
        {
            preMsg = null;
            /*获取前一个节点信息*/
            var preNode = nodes.FirstOrDefault(o => o.OrderNo == node.OrderNo - 1);
            var finishNodes = new List<ExecutingNode>(nodes.Count());

            if (preNode == null)
            {
                foreach (var t in nodes.Where(t => t.OrderNo >= node.OrderNo))
                {
                    if (t.OrderNo == 0 || t.OrderNo == nodes.Length - 1)
                    {
                        continue;
                    }

                    t.ExecutingMessage = string.Format("当前步骤{0}找不到上一步{1}信息,执行中断", node.OrderNo, node.OrderNo - 1);
                    finishNodes.Add(t);
                }

                this.executingRepository.Abort(task, finishNodes.ToArray());
                return false;
            }

            var prePacket = this.engine.JsonSerializer.Deserialize<Never.Messages.MessagePacket>(preNode.ResultMessage);
            if (prePacket == null)
            {
                foreach (var t in nodes.Where(t => t.OrderNo >= node.OrderNo))
                {
                    if (t.OrderNo == 0 || t.OrderNo == nodes.Length - 1)
                    {
                        continue;
                    }

                    t.ExecutingMessage = string.Format("当前步骤{0}消息类型不是Never.Messages.MessagePacket,执行中断", node.OrderNo);
                    finishNodes.Add(t);
                }

                this.executingRepository.Abort(task, finishNodes.ToArray());
                return false;
            }

            var preMsgType = Type.GetType(prePacket.ContentType);
            if (preMsgType == null)
            {
                foreach (var t in nodes.Where(t => t.OrderNo >= node.OrderNo))
                {
                    if (t.OrderNo == 0 || t.OrderNo == nodes.Length - 1)
                    {
                        continue;
                    }

                    t.ExecutingMessage = string.Format("当前步骤{0}消息类型{1}不能在当前环境中找到类型,执行中断", node.OrderNo, prePacket.ContentType);
                    finishNodes.Add(t);
                }

                this.executingRepository.Abort(task, finishNodes.ToArray());
                return false;
            }

            preMsg = this.engine.JsonSerializer.DeserializeObject(prePacket.Body, preMsgType) as IWorkStepMessage;
            if (preMsgType == typeof(MeanwhileWorkStepMessage))
            {
                var multiplemsg = new MultipleWorkStepMessage();
                multiplemsg.TaskId = ((MeanwhileWorkStepMessage)preMsg).TaskId;
                foreach (var msg in ((MeanwhileWorkStepMessage)preMsg).Messages)
                {
                    var pck = this.engine.JsonSerializer.Deserialize<Never.Messages.MessagePacket>(msg);
                    var pmsg = this.engine.JsonSerializer.DeserializeObject(pck.Body, Type.GetType(pck.ContentType)) as IWorkStepMessage;
                    multiplemsg.Append(pmsg);
                }
            }

            if (preMsg == null)
            {
                foreach (var t in nodes.Where(t => t.OrderNo >= node.OrderNo))
                {
                    if (t.OrderNo == 0 || t.OrderNo == nodes.Length - 1)
                    {
                        continue;
                    }

                    t.ExecutingMessage = string.Format("当前步骤{0}消息类型{1}不是指定的IWorkStepMessage消息,执行中断", node.OrderNo, prePacket.ContentType);
                    finishNodes.Add(t);
                }

                this.executingRepository.Abort(task, finishNodes.ToArray());
                return false;
            }

            var stepTypes = node.StepType.Split(new[] { '|' }, StringSplitOptions.RemoveEmptyEntries);
            string nullStepType = string.Empty;

            /*fix guid*/
            foreach (var step in stepTypes)
            {
                var stepType = TemplateWorkStep.Find(step);
                if (stepType != null)
                {
                    if (ContainerContext.Current == null)
                    {
                        var workstep = Activator.CreateInstance(stepType) as IWorkStep;
                        worksteps.Add(new KeyValueTuple<IWorkStep, string>(workstep, step));
                    }
                    else
                    {
                        var workstep = ContainerContext.Current.ServiceLocator.Resolve(stepType) as IWorkStep;
                        worksteps.Add(new KeyValueTuple<IWorkStep, string>(workstep, step));
                    }
                }
                else
                {
                    nullStepType = step;
                    worksteps.Add(new KeyValueTuple<IWorkStep, string>(null, step));
                }
            }

            if (worksteps.All(o => o.Key == null))
            {
                foreach (var t in nodes.Where(t => t.OrderNo >= node.OrderNo))
                {
                    if (t.OrderNo == 0 || t.OrderNo == nodes.Length - 1)
                    {
                        continue;
                    }

                    t.ExecutingMessage = string.Format("当前步骤{0}所有工作实例不能在当前环境中实例化,执行中断", node.OrderNo);
                    finishNodes.Add(t);
                }

                this.executingRepository.Abort(task, finishNodes.ToArray());
                return false;
            }

            if (node.StepCoordinationMode == CoordinationMode.Meanwhile && nullStepType.IsNotNullOrEmpty())
            {
                foreach (var t in nodes.Where(t => t.OrderNo >= node.OrderNo))
                {
                    if (t.OrderNo == 0 || t.OrderNo == nodes.Length - 1)
                    {
                        continue;
                    }

                    t.ExecutingMessage = string.Format("当前步骤{0}工作实例{1}不能在当前环境中找到类型,执行中断", node.OrderNo, nullStepType);
                    finishNodes.Add(t);
                }

                this.executingRepository.Abort(task, finishNodes.ToArray());
                return false;
            }

            return true;
        }