public JsonResult Uninstall(int id)
        {
            return(this.Visit(Core.EnumUserRole.Admin, () =>
            {
                try
                {
                    tb_command_dal commanddal = new tb_command_dal();
                    tb_task_dal dal = new tb_task_dal();
                    using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
                    {
                        PubConn.Open();
                        var taskmodel = dal.Get(PubConn, id);
                        dal.UpdateTaskState(PubConn, id, (int)Core.EnumTaskState.Stop);

                        tb_command_model m = new tb_command_model()
                        {
                            command = "",
                            commandcreatetime = DateTime.Now,
                            commandname = EnumTaskCommandName.UninstallTask.ToString(),
                            taskid = id,
                            nodeid = taskmodel.nodeid,
                            commandstate = (int)EnumTaskCommandState.None
                        };
                        commanddal.Add(PubConn, m);

                        return Json(new { code = 1 });
                    }
                }
                catch (Exception ex)
                {
                    return Json(new { code = -1, msg = ex.Message });
                }
            }));
        }
 public JsonResult ChangeTaskState(int id, int nodeid, int state)
 {
     return(this.Visit(Core.EnumUserRole.Admin, () =>
     {
         tb_command_dal dal = new tb_command_dal();
         tb_task_dal taskDal = new tb_task_dal();
         using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
         {
             PubConn.Open();
             if (taskDal.CheckTaskState(PubConn, id) == state)
             {
                 string msg = state == 1 ? "已开启" : "已关闭";
                 return Json(new { code = -1, msg = msg });
             }
             else
             {
                 tb_command_model m = new tb_command_model()
                 {
                     command = "",
                     commandcreatetime = DateTime.Now,
                     commandname = state == (int)EnumTaskCommandName.StartTask ? EnumTaskCommandName.StartTask.ToString() : EnumTaskCommandName.StopTask.ToString(),
                     taskid = id,
                     nodeid = nodeid,
                     commandstate = (int)EnumTaskCommandState.None
                 };
                 dal.Add(PubConn, m);
             }
             return Json(new { code = 1, msg = "Success" });
         }
     }));
 }
 public JsonResult Run(int id)
 {
     return(this.Visit(Core.EnumUserRole.Admin, () =>
     {
         try
         {
             tb_task_dal dal = new tb_task_dal();
             tb_command_dal cmdDal = new tb_command_dal();
             using (DbConn PubConn =
                        DbConfig.CreateConn(Config.TaskConnectString))
             {
                 PubConn.Open();
                 tb_task_model task = dal.GetOneTask(PubConn, id);
                 tb_command_model c = new tb_command_model()
                 {
                     command = "",
                     commandcreatetime = DateTime.Now,
                     commandname = EnumTaskCommandName.RunTask.ToString(),
                     taskid = id,
                     nodeid = task.nodeid,
                     commandstate = (int)EnumTaskCommandState.None
                 };
                 cmdDal.Add(PubConn, c);
                 return Json(new { code = 1, msg = "Success" });
             }
         }
         catch (Exception ex)
         {
             return Json(new { code = -1, msg = ex.Message });
         }
     }));
 }
Ejemplo n.º 4
0
 static void RunCommond()
 {
     lock (_lockRunLoop)
     {
         try
         {
             List <tb_command_model> commands = new List <tb_command_model>();
             try
             {
                 SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (c) =>
                 {
                     tb_command_dal commanddal = new tb_command_dal();
                     if (lastMaxID < 0)
                     {
                         lastMaxID = commanddal.GetMaxCommandID(c);
                     }
                     commands = commanddal.GetNodeCommands(c, GlobalConfig.NodeID, lastMaxID);
                 });
             }
             catch (Exception exp2)
             {
                 LogHelper.AddNodeError("获取当前节点命令集错误", exp2);
             }
             if (commands.Count > 0)
             {
                 LogHelper.AddNodeLog("当前节点扫描到" + commands.Count + "条命令,并执行中....");
             }
             foreach (var c in commands)
             {
                 try
                 {
                     CommandFactory.Execute(c);
                     SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (conn) =>
                     {
                         new tb_command_dal().UpdateCommandState(conn, c.id, (int)EnumTaskCommandState.Success);
                     });
                     LogHelper.AddNodeLog(string.Format("当前节点执行命令成功! id:{0},命令名:{1},命令内容:{2}", c.id, c.commandname, c.command));
                 }
                 catch (Exception exp1)
                 {
                     try
                     {
                         SqlHelper.ExcuteSql(GlobalConfig.TaskDataBaseConnectString, (conn) =>
                         {
                             new tb_command_dal().UpdateCommandState(conn, c.id, (int)EnumTaskCommandState.Error);
                         });
                     }
                     catch { }
                     LogHelper.AddTaskError("执行节点命令失败", c.taskid, exp1);
                 }
                 lastMaxID = Math.Max(lastMaxID, c.id);
             }
         }
         catch (Exception exp)
         {
             LogHelper.AddNodeError("系统级不可恢复严重错误", exp);
         }
     }
 }
        public static bool AddTaskCommand(tb_command_model model)
        {
            tb_command_dal dal = new tb_command_dal();

            using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
            {
                PubConn.Open();
                return(dal.Add(PubConn, model));
            }
        }
 public JsonResult Delete(int id)
 {
     return(this.Visit(Core.EnumUserRole.Admin, () =>
     {
         using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
         {
             PubConn.Open();
             tb_command_dal dal = new tb_command_dal();
             dal.Delete(PubConn, id);
             return Json(new { code = 1, msg = "Success" });
         }
     }));
 }
 public ActionResult Update(tb_command_model model)
 {
     return(this.Visit(Core.EnumUserRole.Admin, () =>
     {
         tb_command_dal dal = new tb_command_dal();
         using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
         {
             PubConn.Open();
             model.commandcreatetime = DateTime.Now;
             dal.UpdateCommand(PubConn, model);
         }
         return RedirectToAction("index");
     }));
 }
 public ActionResult Update(int id)
 {
     return(this.Visit(Core.EnumUserRole.Admin, () =>
     {
         using (DbConn PubConn = DbConfig.CreateConn(Config.TaskConnectString))
         {
             PubConn.Open();
             tb_command_dal dal = new tb_command_dal();
             tb_command_model_Ex model = dal.GetOneCommand(PubConn, id);
             List <tb_node_model> Node = new tb_node_dal().GetListAll(PubConn);
             ViewBag.Node = Node;
             return View(model);
         }
     }));
 }
Ejemplo n.º 9
0
 public ActionResult Add(tb_command_model model)
 {
     return(this.Visit(EnumUserRole.Admin, () =>
     {
         tb_command_dal dal = new tb_command_dal();
         using (DbConn PubConn = DbConn.CreateConn(Config.TaskConnectString))
         {
             PubConn.Open();
             model.commandcreatetime = DateTime.Now;
             dal.Add(PubConn, model);
             RedisHelper.SendMessage(new Core.Redis.RedisCommondInfo()
             {
                 CommondType = Core.Redis.EnumCommondType.TaskCommand, NodeId = model.nodeid
             });
         }
         return RedirectToAction("index");
     }));
 }
Ejemplo n.º 10
0
 public JsonResult ChangeMoreTaskState(string poststr)
 {
     return(this.Visit(EnumUserRole.Admin, () =>
     {
         // System.Web.Script.Serialization.JavaScriptSerializer jss = new System.Web.Script.Serialization.JavaScriptSerializer();
         List <PostChangeModel> post = new List <PostChangeModel>();
         //   post = jss.Deserialize<List<PostChangeModel>>(poststr);
         post = JsonConvert.DeserializeObject <List <PostChangeModel> >(poststr);
         tb_command_dal dal = new tb_command_dal();
         tb_task_dal taskDal = new tb_task_dal();
         using (DbConn PubConn = DbConn.CreateConn(Config.TaskConnectString))
         {
             PubConn.Open();
             foreach (PostChangeModel m in post)
             {
                 m.state = m.state == 0 ? 1 : 0;
                 if (taskDal.CheckTaskState(PubConn, m.id) == m.state)
                 {
                     string msg = m.state == 1 ? "已开启" : "已关闭";
                     return Json(new { code = -1, msg = msg });
                 }
                 else
                 {
                     tb_command_model c = new tb_command_model()
                     {
                         command = "",
                         commandcreatetime = DateTime.Now,
                         commandname = m.state == (int)EnumTaskCommandName.StartTask ? EnumTaskCommandName.StartTask.ToString() : EnumTaskCommandName.StopTask.ToString(),
                         taskid = m.id,
                         nodeid = m.nodeid,
                         commandstate = (int)EnumTaskCommandState.None
                     };
                     dal.Add(PubConn, c);
                     RedisHelper.SendMessage(new Core.Redis.RedisCommondInfo()
                     {
                         CommondType = Core.Redis.EnumCommondType.TaskCommand, NodeId = m.nodeid
                     });
                 }
             }
             return Json(new { code = 1, data = post });
         }
     }));
 }
        //
        // 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);
            }));
        }
        static void RunCommond()
        {
            lock (_lockRunLoop)
            {
                try
                {
                    List <tb_command_model> commands = new List <tb_command_model>();
                    try
                    {
                        SqlHelper.ExcuteSql(GlobalConfig.ConnectionString, (conn) =>
                        {
                            tb_command_dal commanddal = new tb_command_dal();
                            if (lastMaxID < 0)
                            {
                                lastMaxID = commanddal.GetMaxCommandID(conn);
                            }
                            commands = commanddal.GetNodeCommands(conn, GlobalConfig.NodeID, lastMaxID);
                        });
                    }
                    catch (Exception ex)
                    {
                        LogHelper.AddNodeError("获取当前节点命令集错误", ex);
                    }
                    if (commands.Count > 0)
                    {
                        LogHelper.AddNodeLog("当前节点扫描到" + commands.Count + "条命令,并执行中....");
                    }
                    foreach (var c in commands)
                    {
                        EnumTaskCommandState enumTaskCommandState = EnumTaskCommandState.Error;
                        EnumTaskState        enumTaskState        = EnumTaskState.UnInstall;
                        try
                        {
                            SqlHelper.ExcuteSql(GlobalConfig.ConnectionString, (conn) =>
                            {
                                tb_task_dal taskDAL = new tb_task_dal();
                                var task            = taskDAL.GetOneTask(conn, c.taskid);

                                tb_version_dal versionDAL = new tb_version_dal();
                                var version = versionDAL.GetVersionByTaskID(conn, c.taskid);

                                string path = $"{AppDomain.CurrentDomain.BaseDirectory}{task.taskname}";
                                if (!Directory.Exists(path))
                                {
                                    Directory.CreateDirectory(path);
                                    if (version != null)
                                    {
                                        string zipFilePath = $"{path}\\{version.zipfilename}";
                                        ///数据库二进制转压缩文件
                                        CompressHelper.ConvertToFile(version.zipfile, zipFilePath);
                                        CompressHelper.UnCompress(zipFilePath, path);
                                        ///删除压缩文件
                                        File.Delete(zipFilePath);
                                        ///初始化shell脚本
                                        InitScript.InstallScript(task.taskname, task.taskmainclassdllfilename);

                                        InitScript.UninstallScript(task.taskname);

                                        InitScript.StartScript(task.taskname);

                                        InitScript.StopScript(task.taskname);
                                    }
                                    else
                                    {
                                        throw new Exception($"在tb_version表中未查到taskid:{c.taskid}数据");
                                    }
                                }
                                if (c.commandname == EnumTaskCommandName.StartTask.Tostring())
                                {
                                    if (ServiceHelper.ServiceState(task.taskname) == EnumTaskState.UnInstall)
                                    {
                                        CommandFactory.Execute(path + "\\install.bat");
                                    }

                                    ///线程睡眠5s,等待服务安装完成
                                    Thread.Sleep(5000);
                                    CommandFactory.Execute(path + "\\start.bat");

                                    ///线程睡眠2s,等到脚本执行完成
                                    Thread.Sleep(2000);
                                    if (ServiceHelper.ServiceState(task.taskname) == EnumTaskState.Running)
                                    {
                                        enumTaskCommandState = EnumTaskCommandState.Success;
                                        enumTaskState        = EnumTaskState.Running;
                                    }
                                }
                                else if (c.commandname == EnumTaskCommandName.ReStartTask.Tostring())
                                {
                                    CommandFactory.Execute(path + "\\start.bat");
                                    ///线程睡眠2s,等到脚本执行完成
                                    Thread.Sleep(2000);
                                    if (ServiceHelper.ServiceState(task.taskname) == EnumTaskState.Running)
                                    {
                                        enumTaskCommandState = EnumTaskCommandState.Success;
                                        enumTaskState        = EnumTaskState.Running;
                                    }
                                }
                                else if (c.commandname == EnumTaskCommandName.StopTask.Tostring())
                                {
                                    CommandFactory.Execute(path + "\\stop.bat");

                                    ///线程睡眠2s,等到脚本执行完成
                                    Thread.Sleep(2000);
                                    if (ServiceHelper.ServiceState(task.taskname) == EnumTaskState.Stop)
                                    {
                                        enumTaskCommandState = EnumTaskCommandState.Success;
                                        enumTaskState        = EnumTaskState.Stop;
                                    }
                                }
                                else if (c.commandname == EnumTaskCommandName.UninstallTask.Tostring())
                                {
                                    if (ServiceHelper.ServiceState(task.taskname) == EnumTaskState.Running)
                                    {
                                        CommandFactory.Execute(path + "\\stop.bat");
                                        Thread.Sleep(2000);
                                    }
                                    CommandFactory.Execute(path + "\\uninstall.bat");

                                    ///线程睡眠2s,等到脚本执行完成
                                    Thread.Sleep(2000);
                                    if (ServiceHelper.ServiceState(task.taskname) == EnumTaskState.UnInstall)
                                    {
                                        enumTaskCommandState = EnumTaskCommandState.Success;
                                    }
                                }
                                ///更新命令状态
                                new tb_command_dal().UpdateCommandState(conn, c.id, (int)enumTaskCommandState);
                                ///更新服务状态
                                new tb_task_dal().UpdateTaskState(conn, c.taskid, (int)enumTaskState);

                                if (enumTaskCommandState == EnumTaskCommandState.Success)
                                {
                                    LogHelper.AddNodeLog($"节点:{c.nodeid}成功执行任务:{c.taskid}……");
                                }
                            });
                        }
                        catch (Exception ex)
                        {
                            LogHelper.AddTaskError("任务执行错误", c.taskid, ex);
                        }
                    }
                }
                catch (Exception ex)
                {
                    LogHelper.AddNodeLog($"节点执行错误,错误原因:{ex.Message}");
                }
            }
        }