public JsonEntity SkipDispatchExecute(string ClientId, int dispatchId) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { var nodemodel = nodedal.Detail(dbconn, ClientId); dbconn.BeginTransaction(); try { var dispatchmodel = dispatchdal.GetDetail(dbconn, dispatchId); if (dispatchmodel == null || dispatchmodel.DispatchState == -1) { throw new MException("调度不存在!"); } if (dispatchmodel.DispatchState != 2) { throw new MException("调度不是执行中状态!"); } dispatchdal.SkipExec(dbconn, dispatchmodel.DispatchId); dbconn.Commit(); return(new JsonEntity() { code = 1 }); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } }
public JsonEntity CmdEndExecute(string ClientId, int cmdid, bool success, string msg) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { var nodemodel = nodedal.Detail(dbconn, ClientId); dbconn.BeginTransaction(); try { var cmddetail = cmddal.Detail(dbconn, cmdid); if (cmddetail == null || cmddetail.CmdState == -1) { throw new MException("命令不存在!"); } if (cmddetail.CmdState != 1) { throw new MException("命令不是执行中状态!"); } if (cmddetail.NodeId != nodemodel.NodeId) { throw new MException("无权限操作该命令!"); } cmddal.EndCmd(dbconn, cmdid, success, msg); dbconn.Commit(); return(new JsonEntity() { code = 1 }); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } }
public int TaskSetVersion(TaskVersion model) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { var task = taskdal.GetDetail(dbconn, model.TaskId); if (task == null) { throw new MException("没有任务!"); } dbconn.BeginTransaction(); try { model.TaskId = task.TaskId; var versions = taskdal.AddVersion(dbconn, new Model.TaskVersion() { CreateTime = DateTime.Now, FilePath = model.FilePath ?? "", FileSize = model.FileSize, Remark = model.Remark ?? "", TaskId = model.TaskId, VersionId = 0, VersionNO = DateTime.Now.ToString("yyyyMMddHHmmssfff" + "-" + model.TaskId), Vstate = 0 }); taskdal.SetVersion(dbconn, task.TaskId, versions.VersionId); dbconn.Commit(); return(model.TaskId); } catch (Exception ex) { dbconn.Rollback(); throw; } } }
public JsonEntity AutoEndDispatchExecute(string ClientId, int dispatchId) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { dbconn.BeginTransaction(); try { var dispatchmodel = dispatchdal.GetDetail(dbconn, dispatchId); if (dispatchmodel == null || dispatchmodel.DispatchState == -1) { throw new MException("调度不存在!"); } if (dispatchmodel.DispatchState != 2) { throw new MException("调度不是执行中状态!"); } dispatchdal.EndExec(dbconn, dispatchmodel.DispatchId, false, "自动结束执行" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff")); //System.Diagnostics.Trace.WriteLine(string.Format("{0} 自动结束执行 {1}", dispatchmodel.DispatchId, DateTime.Now.ToString("HH:mm:ss.fff"))); dbconn.Commit(); return(new JsonEntity() { code = 1 }); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } }
public bool SetDispatchState(int taskId, int nodeId, int dispatchState) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { List <int> nodeids = new List <int>(); if (nodeId == 0) { nodeids.AddRange(taskdal.GetTaskBindings(dbconn, taskId).Select(x => x.NodeId)); } else { nodeids.Add(nodeId); } dbconn.BeginTransaction(); try { foreach (var a in nodeids) { taskdal.SetDispatchState(dbconn, taskId, a, dispatchState); } dbconn.Commit(); return(true); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } }
public bool AddTaskBinding(int taskId, string nodeIds) { var nodes = RLib.Utils.StringHelper.SplitToIntList(nodeIds ?? "", new char[] { ',', ' ' }); if (nodes.Count == 0) { throw new MException("请选择节点!"); } using (RLib.DB.DbConn dbconn = Pub.GetConn()) { dbconn.BeginTransaction(); try { foreach (var n in nodes) { taskdal.AddBinding(dbconn, new TaskBinding() { NodeId = n, ServerState = 0, TaskId = taskId }); } dbconn.Commit(); return(true); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } }
/// <summary> /// 批量上传版本变指定为当前版本 /// </summary> /// <param name="model"></param> /// <param name="tag"></param> public void BatchTaskVersion(TaskVersion model, int tag) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { var tasks = taskdal.GetAllTask(dbconn, tag); if (tasks.Count == 0) { throw new MException("没有任务!"); } dbconn.BeginTransaction(); try { foreach (var a in tasks) { model.TaskId = a.TaskId; var versions = taskdal.AddVersion(dbconn, model); taskdal.SetVersion(dbconn, a.TaskId, versions.VersionId); } dbconn.Commit(); } catch (Exception ex) { dbconn.Rollback(); throw; } } }
public JsonEntity EndDispatchExecute(string ClientId, int dispatchId, bool success, string msg) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { var nodemodel = nodedal.Detail(dbconn, ClientId); dbconn.BeginTransaction(); try { var dispatchmodel = dispatchdal.GetDetail(dbconn, dispatchId); if (dispatchmodel == null || dispatchmodel.DispatchState == -1) { throw new MException("调度不存在!"); } if (success) { dispatchdal.SuccessEndExec(dbconn, dispatchmodel.DispatchId); } else { if (dispatchmodel.DispatchState != 2) { throw new MException("调度不是执行中状态!"); } dispatchdal.EndExec(dbconn, dispatchmodel.DispatchId, success, msg); } var runningdispatchid = dispatchdal.CheckTaskDispatchKeyIsRunning(dbconn, dispatchmodel.TaskId, dispatchmodel.RunKey); var canupdateStop = runningdispatchid == dispatchmodel.DispatchId; if (!canupdateStop) { bool isnormal = Math.Abs(runningdispatchid - dispatchmodel.DispatchId) < (int.MaxValue / 2); canupdateStop = isnormal ? dispatchmodel.DispatchId <= runningdispatchid : dispatchmodel.DispatchId >= runningdispatchid; } if (canupdateStop) { dispatchdal.SetTaskDispatchKeyStopped(dbconn, dispatchmodel.TaskId, dispatchmodel.RunKey); } else { // System.Diagnostics.Trace.WriteLine(string.Format("{0} 不能设置未运行 {1}", dispatchmodel.DispatchId, DateTime.Now.ToString("HH:mm:ss.fff"))); } dbconn.Commit(); return(new JsonEntity() { code = 1 }); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } }
public int TaskAdd(Task model) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { if (string.IsNullOrWhiteSpace(model.Title)) { throw new MException("标题不能为空!"); } SetDefaultNodeBind(model); Model.Task taskmodel = new Model.Task() { CreateTime = DateTime.Now, CurrVersionId = 0, DispatchClass = model.DispatchClass, EnterClass = model.EnterClass, EnterDll = model.EnterDll, ExpireMins = 1000, Remark = model.Remark, RunCron = model.RunCron, State = model.State, TaskConfig = model.TaskConfig, TaskId = model.TaskId, TaskTags = 0, Title = model.Title, TaskType = model.TaskType, UpdateTime = null, }; dbconn.BeginTransaction(); try { taskmodel = taskdal.Add(dbconn, taskmodel); foreach (var a in model.TaskBindings) { taskdal.AddBinding(dbconn, new Model.TaskBinding() { TaskId = taskmodel.TaskId, NodeId = a.NodeId }); } dbconn.Commit(); return(taskmodel.TaskId); } catch (Exception ex) { dbconn.Rollback(); throw; } } }
public bool StartTask(int taskId, List <int> nodeids) { var taskandbinds = TaskBll.ToRightTaskNodes(taskId, nodeids, true); if (taskandbinds.Item2.Count() == 0) { throw new MException("没有节点,无法启动!"); } if (taskandbinds.Item1.CurrVersionId == 0) { throw new MException("请上传任务版本文件并设置当前运行版本!"); } using (RLib.DB.DbConn dbconn = Pub.GetConn()) { dbconn.BeginTransaction(); try { var cmdmodel = new Model.Cmd() { CmdArgs = RLib.Utils.DataSerialize.SerializeJson(new Ruanal.Core.CmdTaskArg() { TaskId = taskId }), CmdState = 0, CmdType = Ruanal.Core.ConfigConst.CmdType_StartTask, NodeId = 0 }; List <string> clientIds = new List <string>(); foreach (var a in taskandbinds.Item2) { cmdmodel.NodeId = a.NodeId; cmddal.AddCmd(dbconn, cmdmodel); taskdal.UpdateBindLocalState(dbconn, a.BindId, 1); clientIds.Add(a.Node.ClientId); } dbconn.Commit(); NotifyNewCmd(clientIds); return(true); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } }
public bool SetDispatchState(int nodeId, int dispatchState) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { dbconn.BeginTransaction(); try { nodedal.SetDispatchState(dbconn, nodeId, dispatchState); dbconn.Commit(); return(true); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } }
public bool StopDispatch(int dispatchid) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { dbconn.BeginTransaction(); try { var dismodel = dispatchdal.GetDetail(dbconn, dispatchid); if (dismodel.NodeId <= 0) { throw new MException("任务未分配到节点,停止执行失败!"); } var cmdmodel = new Model.Cmd() { CmdState = 0, CmdType = Ruanal.Core.ConfigConst.CmdType_StopDispatchJob, NodeId = dismodel.NodeId, CmdArgs = RLib.Utils.DataSerialize.SerializeJson( new Ruanal.Core.CmdDispatchArg() { TaskId = dismodel.TaskId, DispatchId = dismodel.DispatchId, InvokeId = dismodel.InvokeId }) }; cmddal.AddCmd(dbconn, cmdmodel); dbconn.Commit(); var nodemodel = nodedal.Detail(dbconn, dismodel.NodeId); if (nodemodel != null) { NotifyNewCmd(new List <string>() { nodemodel.ClientId }); } return(true); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } }
public bool DeleteNode(int nodeId) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { dbconn.BeginTransaction(); try { var taskdal = new DAL.TaskDal(); var tasks = taskdal.GetNodeTasks(dbconn, nodeId, false); if (tasks.Count > 0) { throw new MException("节点有任务,不能删除节点!"); } nodedal.Delete(dbconn, nodeId); dbconn.Commit(); return(true); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } }
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; } } }
private List <Model.Dispatch> DispatchNodeTasks(int taskid, int nodeId, int count, int nodeStopDispatch, int bindStopDispath) { if (count <= 0) { return(new List <Model.Dispatch>()); } object tasklocker = null; if (taskdisclock.ContainsKey(taskid)) { tasklocker = taskdisclock[taskid]; } else { lock (dispatchlocker) { if (!taskdisclock.ContainsKey(taskid)) { taskdisclock[taskid] = new object(); } tasklocker = taskdisclock[taskid]; } } using (RLib.DB.DbConn dbconn = Pub.GetConn()) { var disitems = dispatchdal.GetWatchExecDispatchs(dbconn, nodeId, taskid, count, true); //节点或节点任务停止分配 或 当前已满足 if (disitems.Count >= count || nodeStopDispatch == 1 || bindStopDispath == 1) { return(disitems); } count -= disitems.Count; int todispatchCount = Math.Max(1, count); var dispaths = dispatchdal.GetUnDispatchs(dbconn, taskid, todispatchCount, true); if (dispaths.Count == 0) { return(disitems); } //分任务单路分配 lock (tasklocker) { dbconn.BeginTransaction(); try { dispaths = dispatchdal.GetUnDispatchs(dbconn, taskid, todispatchCount, false); if (dispaths.Count > 0) { foreach (var a in dispaths) { a.NodeId = nodeId; dispatchdal.SetDispatch(dbconn, a.DispatchId, nodeId); } dbconn.Commit(); disitems.AddRange(dispaths); } return(disitems); } catch (Exception ex) { dbconn.Rollback(); throw ex; } } } }
public int TaskEdit(Task model) { using (RLib.DB.DbConn dbconn = Pub.GetConn()) { if (string.IsNullOrWhiteSpace(model.Title)) { throw new MException("标题不能为空!"); } var taskmodel = taskdal.GetDetail(dbconn, model.TaskId); if (taskmodel == null || taskmodel.State == -1) { throw new MException("任务不存在!"); } SetDefaultNodeBind(model); taskmodel.DispatchClass = model.DispatchClass; taskmodel.EnterClass = model.EnterClass; taskmodel.EnterDll = model.EnterDll; taskmodel.Remark = model.Remark; taskmodel.RunCron = model.RunCron; taskmodel.State = model.State; taskmodel.TaskConfig = model.TaskConfig; taskmodel.Title = model.Title; taskmodel.TaskType = model.TaskType; taskmodel.UpdateTime = DateTime.Now; taskmodel.TaskBindings = taskdal.GetTaskBindings(dbconn, model.TaskId); var delnodes = new List <int>(); foreach (var t in taskmodel.TaskBindings) { if (!model.TaskBindings.Exists(x => t.NodeId == x.NodeId)) { if (t.LocalState == 1) { throw new Exception("节点" + t.Node + "本在状态为运行中,不能删除!"); } } } taskmodel.TaskBindings.Select(x => x.NodeId).Except(model.TaskBindings.Select(x => x.NodeId)).ToList(); var addnodes = model.TaskBindings.Select(x => x.NodeId).Except(taskmodel.TaskBindings.Select(x => x.NodeId)).ToList(); dbconn.BeginTransaction(); try { taskdal.Update(dbconn, taskmodel); foreach (var a in delnodes) { taskdal.DeleteBind(dbconn, model.TaskId, a); } foreach (var a in addnodes) { taskdal.AddBinding(dbconn, new Model.TaskBinding() { TaskId = taskmodel.TaskId, NodeId = a }); } dbconn.Commit(); return(taskmodel.TaskId); } catch (Exception ex) { dbconn.Rollback(); throw; } } }