public void AddTask(LibBusinessTask task, bool needAddDB = false) { int curDate = LibDateUtils.GetCurrentDate(); if (string.IsNullOrEmpty(task.ProgId) || string.IsNullOrEmpty(task.BusinessTaskId)) { return; } //如果指定的日期大于当前日期,无需执行 if (task.TaskType == LibTaskType.None && task.ExecDate != 0 && task.ExecDate > curDate) { return; } //未指定有效执行日期则跳过 if (task.ExecDate == 0 && task.RepeatDateMark == 0) { return; } //无设置执行时间点则跳过 if (task.IntervalTime == 0 && task.ExecTime.Count == 0) { return; } //初始化Timer LibTaskParam param = new LibTaskParam(task); string key = Guid.NewGuid().ToString(); param.Task = new LibTask(new Timer(ExecBusinessTask, param, param.GetTaskDueTime(), Timeout.InfiniteTimeSpan)); TaskList.TryAdd(key, param.Task); TaskMap.TryAdd(task.TaskId, key); if (task.TaskType == LibTaskType.TempTask && needAddDB) { LibDataAccess dataAccess = new LibDataAccess(); dataAccess.ExecuteNonQuery(string.Format("insert into AXPBUSINESSTEMPTASK(TASKID,PROGID,BUSINESSTASKID,EXECDATE,EXECTIME,EXECCONDITION,INTERNALID) values({0},{1},{2},{3},{4},{5},{6})", LibStringBuilder.GetQuotString(task.TaskId), LibStringBuilder.GetQuotString(task.ProgId), LibStringBuilder.GetQuotString(task.BusinessTaskId), task.ExecDate, task.ExecTime[0], LibStringBuilder.GetQuotString(task.ExecCondition), LibStringBuilder.GetQuotString(task.InternalId)), false); } }
private void ExecBusinessTask(object obj) { LibTaskParam param = (LibTaskParam)obj; LibBusinessTask taskDefine = param.TaskDefine; //系统当天是否可以执行 if (IsExecOfDay(taskDefine)) { try { param.Task.TaskState = TaskRunState.Running; LibBcfBase bcf = LibBcfSystem.Default.GetBcfInstance(taskDefine.ProgId); bcf.Handle = LibHandleCache.Default.GetSystemHandle(); Type type = bcf.GetType(); object[] destParam = RestoreParamFormat(type, taskDefine.BusinessTaskId, new string[] { taskDefine.ExecCondition }); object result = bcf.GetType().InvokeMember(taskDefine.BusinessTaskId, BindingFlags.InvokeMethod, null, bcf, destParam); switch (bcf.Template.BillType) { case AxCRL.Template.BillType.Master: case AxCRL.Template.BillType.Bill: break; case AxCRL.Template.BillType.Grid: break; case AxCRL.Template.BillType.DataFunc: break; case AxCRL.Template.BillType.Rpt: case AxCRL.Template.BillType.DailyRpt: DataSet dataSet = result as DataSet; if (dataSet != null) { LibSysNews news = new LibSysNews(); news.Content = taskDefine.MainContent; news.Data = LibBillDataSerializeHelper.Serialize(dataSet); news.PersonId = "SYSTEM"; news.ProgId = taskDefine.ProgId; news.Title = taskDefine.Title; foreach (LibBusinessTaskLiaison item in taskDefine.Liaison) { news.UserList.Add(item.UserId); } LibSysNewsHelper.SendNews(news, false); } break; default: break; } } catch (Exception ex) { //将错误输出 string path = System.IO.Path.Combine(AxCRL.Comm.Runtime.EnvProvider.Default.MainPath, "Output", "Error", "ScheduleTask", string.Format("{0}.txt", DateTime.Now.Ticks)); OutputInfo(path, ex.ToString()); } finally { param.Task.TaskState = TaskRunState.Wait; } } bool canNextExec = true; if (taskDefine.ExecDate != 0) { int curDate = LibDateUtils.GetCurrentDate(); if (taskDefine.ExecDate > curDate) { param.Task.Timer.Dispose(); canNextExec = false; param.Task.TaskState = TaskRunState.Stop; } else if (taskDefine.ExecDate == curDate && taskDefine.ExecTime.Count > 0) { int curTime = LibDateUtils.GetLibTimePart(LibDateUtils.GetCurrentDateTime(), LibDateTimePartEnum.Time); if (taskDefine.ExecTime[taskDefine.ExecTime.Count - 1] < curTime) { param.Task.Timer.Dispose(); canNextExec = false; param.Task.TaskState = TaskRunState.Stop; } } } if (canNextExec) { param.Task.Timer.Change(param.GetTaskDueTime(), Timeout.InfiniteTimeSpan); } else if (taskDefine.TaskType == LibTaskType.TempTask) { //删除临时任务 LibDataAccess dataAccess = new LibDataAccess(); dataAccess.ExecuteNonQuery(string.Format("delete AXPBUSINESSTEMPTASK where TASKID={0}", LibStringBuilder.GetQuotString(taskDefine.TaskId)), false); } }