protected override void Run()
        {
            List<int> taskids = new List<int>();
            SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) =>
                {
                    tb_task_dal taskdal = new tb_task_dal();
                    taskids = taskdal.GetTaskIDsByState(c, (int)EnumTaskState.Stop,GlobalConfig.NodeID);
                });
            List<int> currentscantaskids = new List<int>();
            foreach (var taskid in taskids)
            {
                try
                {
                    var taskruntimeinfo = TaskPoolManager.CreateInstance().Get(taskid.ToString());
                    if (taskruntimeinfo != null)
                    {
                        currentscantaskids.Add(taskid);
                    }

                    var recovertaskids = (from o in lastscantaskids
                                          from c in currentscantaskids
                                          where o == c
                                          select o).ToList();
                    if (recovertaskids != null && recovertaskids.Count > 0)
                        recovertaskids.ForEach((c) => {
                            LogHelper.AddTaskError("任务资源运行异常,可能需要手动卸载",taskid,new Exception("任务处于停止状态,但是相应集群节点中,发现任务存在在运行池中未释放"));
                        });
                    lastscantaskids = currentscantaskids;
                }
                catch (Exception exp)
                {
                    LogHelper.AddNodeError("任务"+taskid+"资源回收出错", exp);
                }
            }
        }
 public override void Run()
 {
     //扫描
     SqlHelper.ExcuteSql(this.SystemRuntimeInfo.TaskConnectString, (c) =>
     {
         tb_task_dal taskdal = new tb_task_dal();
         var taskmodels = taskdal.GetLongRunningTaskIDs(c, maxlongrunningtime);
         foreach (var model in taskmodels)
         {
             this.OpenOperator.Error("检测到任务运行超时,任务id:" + model.id + " 任务标题:" + model.taskname, new Exception());
         }
     });
 }
 public ActionResult Add()
 {
     return this.Visit(Core.EnumUserRole.Admin, () =>
     {
         using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
         {
             PubConn.Open();
             List<tb_task_model> Task = new tb_task_dal().GetListAll(PubConn);
             List<tb_node_model> Node = new tb_node_dal().GetListAll(PubConn);
             ViewBag.Node = Node;
             ViewBag.Task = Task;
             return View();
         }
     });
 }
 public ActionResult Log(string keyword, string CStime, string CEtime, int id = -1, int logtype = -1, int taskid = -1, int nodeid = -1, int pagesize = 10, int pageindex = 1)
 {
     return this.Visit(Core.EnumUserRole.None, () =>
     {
         ViewBag.keyword = keyword; ViewBag.CStime = CStime; ViewBag.CEtime = CEtime; ViewBag.id = id; ViewBag.logtype = logtype; ViewBag.taskid = taskid;
         ViewBag.nodeid = nodeid; ViewBag.pagesize = pagesize; ViewBag.pageindex = pageindex;
         int count = 0;
         using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
         {
             PubConn.Open();
             tb_log_dal dal = new tb_log_dal();
             List<tb_loginfo_model> model = dal.GetList(PubConn, keyword, id, CStime, CEtime, logtype, taskid, nodeid, pagesize, pageindex, out count);
             PagedList<tb_loginfo_model> pageList = new PagedList<tb_loginfo_model>(model, pageindex, pagesize, count);
             List<tb_task_model> Task = new tb_task_dal().GetListAll(PubConn);
             List<tb_node_model> Node = new tb_node_dal().GetListAll(PubConn);
             ViewBag.Node = Node;
             ViewBag.Task = Task;
             return View(pageList);
         }
     });
 }
        //
        // GET: /Command/

        public ActionResult Index(int taskid = -1, int commandstate = -1, int nodeid = -1, int pagesize = 10, int pageindex = 1)
        {
            return this.Visit(Core.EnumUserRole.Admin, () =>
            {
                ViewBag.taskid = taskid; ViewBag.commandstate = commandstate; ViewBag.nodeid = nodeid; ViewBag.pagesize = pagesize; ViewBag.pageindex = pageindex;
                tb_command_dal dal = new tb_command_dal();
                PagedList<tb_command_model_Ex> pageList = null;
                int count = 0;
                using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
                {
                    PubConn.Open();
                    List<tb_command_model_Ex> List = dal.GetList(PubConn, commandstate, taskid, nodeid, pagesize, pageindex, out count);
                    List<tb_task_model> Task = new tb_task_dal().GetListAll(PubConn);
                    List<tb_node_model> Node = new tb_node_dal().GetListAll(PubConn);
                    ViewBag.Node = Node;
                    ViewBag.Task = Task;
                    pageList = new PagedList<tb_command_model_Ex>(List, pageindex, pagesize, count);
                }
                return View(pageList);
            });
        }
 /// <summary>
 /// 恢复已开启的任务
 /// </summary>
 static void RecoveryStartTasks()
 {
     try
     {
         LogHelper.AddNodeLog("当前节点启动成功,准备恢复已经开启的任务...");
         List<int> taskids = new List<int>();
         SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) =>
         {
             tb_task_dal taskdal = new tb_task_dal();
             taskids = taskdal.GetTaskIDsByState(c, (int)EnumTaskState.Running, GlobalConfig.NodeID);
         });
         foreach (var taskid in taskids)
         {
             try
             {
                 CommandFactory.Execute(new tb_command_model()
                 {
                     command = "",
                     commandcreatetime = DateTime.Now,
                     commandname = Core.EnumTaskCommandName.StartTask.ToString(),
                     commandstate = (int)Core.EnumTaskCommandState.None,
                     nodeid = GlobalConfig.NodeID,
                     taskid = taskid,
                     id = -1
                 });
             }
             catch (Exception exp)
             {
                 LogHelper.AddTaskError(string.Format("恢复已经开启的任务{0}失败",taskid),taskid,exp);
             }
         }
         LogHelper.AddNodeLog(string.Format("恢复已经开启的任务完毕,共{0}条任务重启", taskids.Count));
     }
     catch (Exception exp)
     {
         LogHelper.AddNodeError("恢复已经开启的任务失败", exp);
     }
 }
        /// <summary>
        /// 任务的关闭
        /// </summary>
        /// <param name="taskid"></param>
        /// <returns></returns>
        public bool Stop(int taskid)
        {
            var taskruntimeinfo = TaskPoolManager.CreateInstance().Get(taskid.ToString());
            if (taskruntimeinfo == null)
            {
                throw new Exception("任务不在运行中");
            }

            var r= DisposeTask(taskid, taskruntimeinfo,false);

            SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) =>
            {
                tb_task_dal taskdal = new tb_task_dal();
                taskdal.UpdateTaskState(c, taskid, (int)EnumTaskState.Stop);
            });
            LogHelper.AddTaskLog("节点关闭任务成功", taskid);
            return r;
        }
        /// <summary>
        /// 任务的开启
        /// </summary>
        /// <param name="taskid"></param>
        /// <returns></returns>
        public bool Start(int taskid)
        {
            var taskruntimeinfo = TaskPoolManager.CreateInstance().Get(taskid.ToString());
            if (taskruntimeinfo != null)
            {
                throw new Exception("任务已在运行中");
            }

            taskruntimeinfo = new NodeTaskRuntimeInfo();
            taskruntimeinfo.TaskLock = new TaskLock();
            SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) =>
                {
                    tb_task_dal taskdal = new tb_task_dal();
                    taskruntimeinfo.TaskModel = taskdal.Get(c, taskid);
                    tb_version_dal versiondal = new tb_version_dal();
                    taskruntimeinfo.TaskVersionModel = versiondal.GetCurrentVersion(c, taskid, taskruntimeinfo.TaskModel.taskversion);
                });
            string filelocalcachepath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\"+GlobalConfig.TaskDllCompressFileCacheDir + @"\" + taskruntimeinfo.TaskModel.id + @"\" + taskruntimeinfo.TaskModel.taskversion + @"\" +
                taskruntimeinfo.TaskVersionModel.zipfilename;
            string fileinstallpath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\"+GlobalConfig.TaskDllDir + @"\" +  taskruntimeinfo.TaskModel.id ;
            string fileinstallmainclassdllpath = fileinstallpath + @"\" + taskruntimeinfo.TaskModel.taskmainclassdllfilename;
            string taskshareddlldir = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\" + GlobalConfig.TaskSharedDllsDir;
             
            XXF.Common.IOHelper.CreateDirectory(filelocalcachepath);
            XXF.Common.IOHelper.CreateDirectory(fileinstallpath);
            System.IO.File.WriteAllBytes(filelocalcachepath, taskruntimeinfo.TaskVersionModel.zipfile);

            CompressHelper.UnCompress(filelocalcachepath, fileinstallpath);
            //拷贝共享程序集
            XXF.Common.IOHelper.CopyDirectory(taskshareddlldir, fileinstallpath);
            try
            {
                var dlltask = new AppDomainLoader<BaseDllTask>().Load(fileinstallmainclassdllpath, taskruntimeinfo.TaskModel.taskmainclassnamespace, out taskruntimeinfo.Domain);
                var sdktaskmodel = new XXF.BaseService.TaskManager.model.tb_task_model();
                PropertyHelper.Copy(taskruntimeinfo.TaskModel, sdktaskmodel);
                dlltask.SystemRuntimeInfo = new TaskSystemRuntimeInfo()
                {
                    TaskConnectString = GlobalConfig.TaskDataBaseConnectString,
                    TaskModel = sdktaskmodel
                };

                dlltask.AppConfig = new TaskAppConfigInfo();
                if (!string.IsNullOrEmpty(taskruntimeinfo.TaskModel.taskappconfigjson))
                {
                    dlltask.AppConfig = new XXF.Serialization.JsonHelper().Deserialize<TaskAppConfigInfo>(taskruntimeinfo.TaskModel.taskappconfigjson);
                }
                taskruntimeinfo.DllTask = dlltask;
                bool r = TaskPoolManager.CreateInstance().Add(taskid.ToString(), taskruntimeinfo);
                SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) =>
                {
                    tb_task_dal taskdal = new tb_task_dal();
                    taskdal.UpdateTaskState(c, taskid, (int)EnumTaskState.Running);
                });
                LogHelper.AddTaskLog("节点开启任务成功", taskid);
                return r;
            }
            catch (Exception exp)
            {
                DisposeTask(taskid, taskruntimeinfo,true);
                throw exp;
            }
        }