Exemplo n.º 1
0
        /// <summary>
        /// 扫描命令队列并添加到任务池
        /// </summary>
        private static void ScanCommandAndExecute()
        {
            //1.扫描分配给该节点的任务命令(读库或者直接读webapi)
            //2.遍历命令集合,并从命令池中找寻命令并执行,执行成功,更新命令队列,执行失败也更新命令状态
            var r = NodeProxy.PostToServer <LoadCommandQueueListResponse, LoadCommandQueueListRequest>(ProxyUrl.LoadCommandQueueList_Url, new LoadCommandQueueListRequest()
            {
                NodeId = GlobalNodeConfig.NodeID, Source = Source.Node, LastMaxId = lastMaxID
            });

            if (r.Status != ResponesStatus.Success)
            {
                onInitTaskPool("读取命令失败,服务器返回:" + JsonConvert.SerializeObject(r));
                return;
            }

            if (r.Data.CommandQueueList.Count > 0)
            {
                onInitTaskPool("当前节点扫描到" + r.Data.CommandQueueList.Count + "条命令,并执行中");
            }
            foreach (var item in r.Data.CommandQueueList)
            {
                try
                {
                    CommandPoolManager.CreateInstance().Get(item.commanddetailid.ToString()).commandBase.Execute();
                    //更新命令队列节点状态
                    var r2 = NodeProxy.PostToServer <EmptyResponse, UpdateCommandQueueStatusRequest>(ProxyUrl.UpdateCommandQueueStatus_Url, new UpdateCommandQueueStatusRequest()
                    {
                        NodeId = GlobalNodeConfig.NodeID, Source = Source.Node, CommandQueueId = item.id, CommandStatus = CommandQueueStatus.ExecuteSuccess
                    });
                    if (r2.Status != ResponesStatus.Success)
                    {
                        onInitTaskPool("更新命令状态(成功)失败,服务器返回:" + JsonConvert.SerializeObject(r));
                        //记录节点错误日志
                        return;
                    }
                    //更新命令状态为成功
                    onInitTaskPool(string.Format("当前节点执行命令成功! id:{0},命令名:{1},命令参数:{2}", item.id, item.commandmainclassname, item.commandparams));
                }
                catch (Exception ex)
                {
                    //更新命令状态为错误
                    var r2 = NodeProxy.PostToServer <EmptyResponse, UpdateCommandQueueStatusRequest>(ProxyUrl.UpdateCommandQueueStatus_Url, new UpdateCommandQueueStatusRequest()
                    {
                        NodeId = GlobalNodeConfig.NodeID, Source = Source.Node, CommandQueueId = item.id, CommandStatus = CommandQueueStatus.ExecuteError
                    });
                    if (r2.Status != ResponesStatus.Success)
                    {
                        onInitTaskPool("执行节点命令异常,更新命令状态为错误,服务器返回:" + JsonConvert.SerializeObject(r));
                        //记录节点错误日志
                        return;
                    }
                    //添加任务执行失败日志
                    onInitTaskPool("执行节点命令异常:任务id:" + item.taskid + ",命令名称:" + item.commandmainclassname + "(" + item.id + ")", ex);
                }
            }
        }
        /// <summary>
        /// 构建命令池
        /// </summary>
        public void BuildCommandPool()
        {
            try
            {
                //读取库中命令,然后反射创建对象,封装命令运行时信息,并添加到命令池中
                var r = NodeProxy.PostToServer <LoadCommandListResponse, LoadCommandListRequest>(ProxyUrl.LoadCommandList_Url, new LoadCommandListRequest()
                {
                    NodeId = GlobalNodeConfig.NodeID.ToString(), Source = Source.Node
                });
                if (r.Status != ResponesStatus.Success)
                {
                    onInit("读取命令失败,服务器返回:" + JsonConvert.SerializeObject(r));
                    return;
                }
                if (r.Data.CommandLibDetailList.Count <= 0)
                {
                    onInit("读取到0条命令,无法构建命令池");
                    return;
                }
                int commandCount = r.Data.CommandLibDetailList.Count;

                onInit("读取到" + commandCount.ToString() + "条命令");
                onInit("开始创建命令对象,并添加到命令池");
                int sucess = 0;
                int failed = 0;
                foreach (var item in r.Data.CommandLibDetailList)
                {
                    try
                    {
                        var cmdruntime = CommandFactory.Create(item);
                        if (cmdruntime != null)
                        {
                            CommandPoolManager.CreateInstance().Add(item.id.ToString(), cmdruntime);
                            sucess += 1;
                        }
                        else
                        {
                            onInit("命令:" + item.commandname + "(" + item.id.ToString() + "),添加到命令池失败!创建命令对象为null");
                            failed += 1;
                        }
                    }
                    catch (Exception ex)
                    {
                        onInit("命令:" + item.commandname + "(" + item.id.ToString() + "),添加到命令池失败!", ex);
                        failed += 1;
                    }
                }
                onInit("构建命令池完成,命令总数量:" + commandCount + ",成功数量:" + sucess.ToString() + ",失败数量:" + failed.ToString());
            }
            catch (Exception ex)
            {
                onInit("命令池构建失败:" + ex.Message, ex);
            }
        }
        ///// <summary>
        ///// 上一次日志扫描的最大id
        ///// </summary>
        //public static int lastMaxID = -1;

        #region 开启任务扫描
        /// <summary>
        /// 开启任务扫描
        /// </summary>
        /// <returns></returns>
        public override bool StartScanCommandQueue()
        {
            //1.扫描分配给该节点的任务命令(读库或者直接读webapi)
            //2.遍历命令集合,并从命令池中找寻命令并执行,执行成功,更新命令队列,执行失败也更新命令状态
            var r = NodeProxy.PostToServer <LoadCommandQueueListResponse, LoadCommandQueueListRequest>(ProxyUrl.LoadCommandQueueList_Url, new LoadCommandQueueListRequest()
            {
                NodeId = GlobalNodeConfig.NodeID, Source = Source.Node, LastMaxId = GlobalNodeConfig.NodeInfo.lastmaxid, CommandStatus = (int)Model.enums.ExecuteStatus.NoExecute
            });

            if (r.Status != ResponesStatus.Success)
            {
                onScanMsg("读取命令失败,服务器返回:" + JsonConvert.SerializeObject(r));
                return(false);
            }
            int           sucess         = 0;
            int           failed         = 0;
            List <string> cmdQueueIdList = new List <string>();

            r.Data.CommandQueueList.ForEach(x => cmdQueueIdList.Add(x.id.ToString()));
            if (r.Data.CommandQueueList.Count > 0)
            {
                onScanMsg("当前节点扫描到" + r.Data.CommandQueueList.Count + "条命令(" + string.Join(",", cmdQueueIdList.ToArray()) + "),并执行中");
            }

            foreach (var item in r.Data.CommandQueueList)
            {
                try
                {
                    GlobalNodeConfig.NodeInfo.lastmaxid = item.id > GlobalNodeConfig.NodeInfo.lastmaxid ? item.id : GlobalNodeConfig.NodeInfo.lastmaxid;
                    CommandPoolManager.CreateInstance().Get(item.commanddetailid.ToString()).commandBase.CommandQueue = item;
                    CommandPoolManager.CreateInstance().Get(item.commanddetailid.ToString()).commandBase.TryExecute();

                    sucess += 1;
                    //更新命令状态为成功
                    onScanMsg(string.Format("当前节点执行命令成功! id:{0},命令名:{1},命令参数:{2}", item.id, item.commandmainclassname, item.commandparams));
                }
                catch (Exception ex)
                {
                    failed += 1;
                    //添加任务执行失败日志
                    onScanMsg("执行节点命令异常:任务id:" + item.taskid + ",命令名称:" + item.commandmainclassname + "(" + item.id + ")", ex);
                }
            }
            onScanMsg("当前循环扫描命令队列完成,保存上次最大命令队列id值:" + GlobalNodeConfig.NodeInfo.lastmaxid + ",扫描到命令个数:" + r.Data.CommandQueueList.Count + ",执行成功个数:" + sucess + ",执行失败个数:" + failed);
            return(true);
        }
 /// <summary>
 /// 静态初始化
 /// </summary>
 static CommandPoolManager()
 {
     _commandpoolmanager = new CommandPoolManager();
 }