public Model.Task GetDetail(int taskId) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { var r = taskdal.GetDetail(dbconn, taskId); return(r); } }
public int TaskDelete(int taskid) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { var taskdetail = taskdal.GetDetail(dbconn, taskid); if (taskdetail == null || taskdetail.State == -1) { throw new MException("任务不存在!"); } var taskbind = taskdal.GetTaskBindings(dbconn, taskid); if (taskbind.Count(x => x.LocalState == 1) > 0) { throw new MException("请先停止!"); } if (taskbind.Count(x => x.ServerState == 1) > 0) { throw new MException("请等待节点任务停止或重启节点!"); } taskdal.Delete(dbconn, taskid); return(taskid); } }
public JsonEntity BuildDispatchs(string clientId, int taskId, string groupId, List <Ruanal.Job.DispatcherItem> configs) { string noRunKeyRegName = "_NoRunKeyReg"; string onlyRunKeyRegName = "_OnlyRunKeyReg"; using (RLib.DB.DbConn dbconn = Pub.GetConn()) { var nodedetail = nodedal.Detail(dbconn, clientId); var taskmodel = taskdal.GetDetail(dbconn, taskId); if (taskmodel == null || taskmodel.State == -1) { return(new JsonEntity() { code = -1, msg = "任务不存在!" }); } List <string> norunkey = new List <string>(); List <string> onlyrunkey1 = new List <string>(); List <string> onlyrunkey2 = new List <string>(); bool isnorunkey = false; #region 得到 排除和仅运行配置 nodedetail.NodeConfig = (nodedetail.NodeConfig ?? "").Trim(); taskmodel.TaskConfig = (taskmodel.TaskConfig ?? "").Trim(); if (nodedetail.NodeConfig.StartsWith("{") && nodedetail.NodeConfig.EndsWith("}")) { var configjobj = Newtonsoft.Json.Linq.JObject.Parse(nodedetail.NodeConfig); if (configjobj[noRunKeyRegName] != null) { norunkey.AddRange(configjobj[noRunKeyRegName].ToString().Replace("||", "■").Split(new char[] { '■' }, StringSplitOptions.RemoveEmptyEntries)); } if (configjobj[onlyRunKeyRegName] != null) { onlyrunkey1.AddRange(configjobj[onlyRunKeyRegName].ToString().Replace("||", "■").Split(new char[] { '■' }, StringSplitOptions.RemoveEmptyEntries)); } } if (taskmodel.TaskConfig.StartsWith("{") && taskmodel.TaskConfig.EndsWith("}")) { var configjobj = Newtonsoft.Json.Linq.JObject.Parse(taskmodel.TaskConfig); if (configjobj[noRunKeyRegName] != null) { //排除的key添加到列表 norunkey.AddRange(configjobj[noRunKeyRegName].ToString().Replace("||", "■").Split(new char[] { '■' }, StringSplitOptions.RemoveEmptyEntries)); } if (configjobj[onlyRunKeyRegName] != null) { //var taskonlyrunkey = configjobj[onlyRunKeyRegName].ToString().Replace("||", "■").Split(new char[] { '■' }, StringSplitOptions.RemoveEmptyEntries); onlyrunkey2.AddRange(configjobj[onlyRunKeyRegName].ToString().Replace("||", "■").Split(new char[] { '■' }, StringSplitOptions.RemoveEmptyEntries)); } } norunkey = norunkey.Distinct().ToList(); onlyrunkey1 = onlyrunkey1.Distinct().ToList(); onlyrunkey2 = onlyrunkey2.Distinct().ToList(); if (isnorunkey == true) { return(new JsonEntity() { code = 1, data = groupId }); } #endregion //排除 if (norunkey.Count() > 0) { foreach (var key in norunkey) { System.Text.RegularExpressions.Regex regx = new System.Text.RegularExpressions.Regex(key); configs.RemoveAll((x) => { return(regx.IsMatch(x.RunKey)); }); } } //仅包含 if (onlyrunkey1.Count() > 0 || onlyrunkey2.Count > 0) { List <System.Text.RegularExpressions.Regex> regs1 = onlyrunkey1.Select(x => new System.Text.RegularExpressions.Regex(x)).ToList(); List <System.Text.RegularExpressions.Regex> regs2 = onlyrunkey2.Select(x => new System.Text.RegularExpressions.Regex(x)).ToList(); configs = configs.Where(x => (regs1.Count == 0 || regs1.Exists(y => y.IsMatch(x.RunKey))) //匹配 仅节点包含 && (regs2.Count == 0 || regs2.Exists(y => y.IsMatch(x.RunKey))) //且匹配 仅任务包含 ).ToList(); } var nodetasks = taskdal.NodeTaskSum(dbconn, nodedetail.NodeId); var nodetask = nodetasks.FirstOrDefault(x => x.TaskId == taskId); if (nodetask == null || nodetask.LocalState != 1) { return(new JsonEntity() { code = -1, msg = "任务不存在!" }); } if (nodedetail.StopDispatch == 1 || nodetask.StopDispatch == 1) { return(new JsonEntity() { code = -1, msg = "任务分配调度已停止," + (nodedetail.StopDispatch == 1 ? "节点停止调度" : "绑定停止调度") }); } dbconn.BeginTransaction(); try { if (string.IsNullOrEmpty(groupId)) { return(new JsonEntity() { code = -1, msg = "没有分组ID!" }); } int rcount = dispatchdal.ExistGroupId(dbconn, groupId); if (rcount > 0) { return(new JsonEntity() { code = -1, msg = "分组ID已存在!" }); } //var tbinds = taskdal.GetTaskBindsNodes(dbconn, taskId, true); //List<string> clientIds = tbinds.Select(x => x.ClientId).ToList(); List <Model.Dispatch> dises = new List <Model.Dispatch>(); DateTime st = dbconn.GetServerDate(); DateTime expiretime = st; if (taskmodel.ExpireMins <= 0) { expiretime = st.AddMinutes(Ruanal.Core.ConfigConst.DispatchDefaultExpireMins); } else { expiretime = st.AddMinutes((double)taskmodel.ExpireMins); } foreach (var a in configs) { dises.Add(new Model.Dispatch() { DispatchState = 0, GroupId = groupId, NickName = a.NickName, RunKey = a.RunKey, RunArgs = a.RunArgs, TaskId = taskId, InvokeId = Guid.NewGuid().ToString().Replace("-", ""), ExpireTime = expiretime }); } dispatchdal.AddBatch(dbconn, dises); dbconn.Commit(); CmdHelper.Instance.NotifyNewDispatch(taskId.ToString()); return(new JsonEntity() { code = 1, data = groupId }); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } }
/// <summary> /// 任务自动绑定或删除主节点 返回任务详情和当前所有绑定 /// </summary> /// <param name="taskId"></param> /// <returns></returns> public static Tuple <Model.Task, List <Model.TaskBinding> > ToRightTaskNodes(int taskId, List <int> filterNodeIdds, bool checkThrow) { var taskdal = new DAL.TaskDal(); var nodedal = new DAL.NodeDal(); using (RLib.DB.DbConn dbconn = Pub.GetConn()) { var taskmodel = taskdal.GetDetail(dbconn, taskId); if (taskmodel == null || taskmodel.State != 0) { throw new MException("任务不存在或已删除!"); } var taskbinds = taskdal.GetTaskBindings(dbconn, taskId); foreach (var a in taskbinds) { a.Node = nodedal.Detail(dbconn, a.NodeId); } Exception throex = null; //添加控制节点 if (taskmodel.TaskType == 1 && taskbinds.Count(x => x.Node.NodeType == 1) == 0) { var mainnodes = nodedal.GetMainNode(dbconn); if (mainnodes.Count == 0) { throex = new MException("没有控制节点,不能运行调度任务!"); } else if (mainnodes.Count > 1) { throex = new MException("有多个控制节点,请手动添加控制节点!"); } else { var binding = taskdal.AddBinding(dbconn, new Model.TaskBinding() { BindId = 0, LastRunTime = null, LocalState = 0, ServerState = 0, NodeId = mainnodes.First().NodeId, RunVersion = 0, TaskId = taskmodel.TaskId }); binding.Node = mainnodes.First(); taskbinds.Add(binding); } } if (checkThrow == true && throex != null) { throw throex; } //删除控制节点 if (taskmodel.TaskType == 0 && taskbinds.Count(x => x.Node.NodeType == 1) > 0) { var mainnodes = taskbinds.Where(x => x.Node.NodeType == 1).ToList(); foreach (var a in mainnodes) { taskdal.DeleteBind(dbconn, a.BindId); taskbinds.Remove(a); } } if (filterNodeIdds != null && filterNodeIdds.Count > 0) { taskbinds.RemoveAll(x => !filterNodeIdds.Contains(x.NodeId)); } taskbinds = taskbinds.OrderByDescending(x => x.Node.NodeType).ToList(); return(new Tuple <Model.Task, List <Model.TaskBinding> >(taskmodel, taskbinds)); } }