public Int64 AddTask(TaskEntity paraTask) { try { TaskDataSet.PL_TimerTaskRow taskRow = _DataSet.PL_TimerTask.NewPL_TimerTaskRow(); Mapper.DataMapper.ReseveMappingTaskEntity(paraTask, ref taskRow); _DataSet.PL_TimerTask.AddPL_TimerTaskRow(taskRow); this.Save2DB(); paraTask.SetKeyID(taskRow.ID); return taskRow.ID; } catch { throw; } }
/// <summary> /// 添加定时任务 /// </summary> /// <param name="paraName"></param> /// <param name="paraDateStart"></param> /// <param name="paraDateEnd"></param> /// <param name="paraAppName"></param> /// <param name="paraRunSpaceTimeSecs">周期数(秒)</param> /// <param name="paraRunSpaceType">周期类型(便于存储和下次查看)</param> /// <param name="paraExtraStr"></param> /// <param name="paraRunTimeOutSecs">执行超时时间,如果不限定,则给-1,如果限定了,在指定时间内未执行完成,则强制结束(exe直接结束进程,dll通过接口通知结束)</param> /// <returns></returns> public TaskEntity AddTask(string paraName, DateTime paraDateStart, DateTime paraDateEnd, string paraAppName, Int64 paraRunSpaceTimeSecs, TaskFrequence paraRunSpaceType, string paraExtraStr, Int64 paraRunTimeOutSecs) { try { TaskAssembly assembly = BLLFactory.GetBllRegistApp().GetRegestedApp(paraAppName); if (assembly == null) { throw new Exception(string.Format("添加的计划的程序(RegestesAppName){0}尚未在配置文件(RegistedApps.xml)中注册", paraAppName)); } else { TaskEntity entity = new TaskEntity(); entity.DateEnd = paraDateEnd; entity.DateStart = paraDateStart; entity.Enable = true; entity.ExtraParaStr = paraExtraStr; entity.Name = paraName; entity.RunSpaceTime = paraRunSpaceTimeSecs; entity.RunSpaceType = paraRunSpaceType; entity.RunTimeOutSecs = paraRunTimeOutSecs; entity.RegestesAppName = paraAppName; //先校验,后保存 //输入校验 MessageParser.CheckAndSetTaskFrequence(ref entity); //保存到数据库 Int64 id = _DataAccess.AddTask(entity); if (id >= 0) {//保存成功 entity.SetKeyID(id); try { //发送消息同步到任务管理器中 string message = MessageParser.BuildMessage(new List<TaskEntity>() { entity }, null, null, null, null, null); this.SendXMLSocket2Server(message); return entity; } catch { this.DelTaskComplet(id); throw; } } else {//未保存成功 this.WriteLog(-1, entity.Name, "计划保存到数据库没有成功", LogType.TaskAdd2DBError); throw new Exception("计划保存到数据库没有成功"); } } } catch { throw; } }
/// <summary> /// 翻译一条消息 /// </summary> /// <param name="paraMessage">The para message.</param> /// <param name="paraAddedTasks">The para added tasks.</param> /// <param name="paraDeletedTasks">The para deleted tasks.</param> /// <param name="paraUpdateTasks">The para update tasks.</param> /// <param name="paraRunImmediateTask">The para run immediate task.</param> /// <param name="paraRunType">Type of the para run.</param> /// <param name="paraStopTask">The para stop task.</param> public static void ParseMessage(string paraMessage, out List<TaskEntity> paraAddedTasks, out List<Int64> paraDeletedTasks, out List<TaskEntity> paraUpdateTasks, out List<Int64> paraRunImmediateTask, out List<RunTaskType> paraRunType, out List<Int64> paraStopTask) { paraAddedTasks = new List<TaskEntity>(); paraDeletedTasks = new List<long>(); paraUpdateTasks = new List<TaskEntity>(); paraRunImmediateTask = new List<long>(); paraRunType = new List<RunTaskType>(); paraStopTask = new List<long>(); XmlDocument doc = new XmlDocument(); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(paraMessage)); doc.Load(ms); XmlNode iNode = doc.SelectSingleNode("Root"); XmlNode addNode = iNode.SelectSingleNode("AddList"); foreach (XmlElement ele in addNode.ChildNodes) { TaskEntity entity = new TaskEntity(); entity.SetKeyID(long.Parse(ele.GetAttribute("ID"))); entity.Name = ele.GetAttribute("Name"); entity.Enable = bool.Parse(ele.GetAttribute("Enable")); entity.DateStart = DateTime.Parse(ele.GetAttribute("DateStart")); entity.DateEnd = DateTime.Parse(ele.GetAttribute("DateEnd")); entity.RunSpaceTime = Int64.Parse(ele.GetAttribute("RunSpaceTime")); entity.RunSpaceType = (TaskFrequence)Enum.Parse(typeof(TaskFrequence), ele.GetAttribute("RunSpaceType")); entity.ExtraParaStr = ele.GetAttribute("ExtraParaStr"); entity.RunTimeOutSecs = Int64.Parse(ele.GetAttribute("RunTimeOutSecs")); entity.RegestesAppName = ele.GetAttribute("RegestesAppName"); CheckAndSetTaskFrequence(ref entity); paraAddedTasks.Add(entity); } XmlNode mdfNode = iNode.SelectSingleNode("UpdateList"); foreach (XmlElement ele in mdfNode.ChildNodes) { TaskEntity entity = new TaskEntity(); entity.Name = ele.GetAttribute("Name"); entity.Enable = bool.Parse(ele.GetAttribute("Enable")); entity.DateStart = DateTime.Parse(ele.GetAttribute("DateStart")); entity.DateEnd = DateTime.Parse(ele.GetAttribute("DateEnd")); entity.RunSpaceTime = Int64.Parse(ele.GetAttribute("RunSpaceTime")); entity.RunSpaceType = (TaskFrequence)Enum.Parse(typeof(TaskFrequence), ele.GetAttribute("RunSpaceType")); entity.ExtraParaStr = ele.GetAttribute("ExtraParaStr"); entity.RunTimeOutSecs = Int64.Parse(ele.GetAttribute("RunTimeOutSecs")); entity.RegestesAppName = ele.GetAttribute("RegestesAppName"); entity.SetKeyID(Int64.Parse(ele.GetAttribute("ID"))); CheckAndSetTaskFrequence(ref entity); paraUpdateTasks.Add(entity); } XmlNode delNode = iNode.SelectSingleNode("DeleteList"); foreach (XmlElement ele in delNode.ChildNodes) { paraDeletedTasks.Add( Int64.Parse(ele.GetAttribute("ID"))); } XmlNode runNode = iNode.SelectSingleNode("RunList"); foreach (XmlElement ele in runNode.ChildNodes) { paraRunImmediateTask.Add( Int64.Parse(ele.GetAttribute("ID"))); paraRunType.Add((RunTaskType)Enum.Parse(typeof(RunTaskType),ele.GetAttribute("Type"))); } //释放内存流 ms.Close(); ms.Dispose(); }