/// <summary> /// 增加或更新一个任务 /// </summary> /// <param name="taskInfo"></param> /// <returns></returns> public ResultModel AddOrUpdateTask(TaskInfo taskInfo) { ResultModel result = new ResultModel(); try { if (string.IsNullOrEmpty(taskInfo.TaskId)) { taskInfo.TaskId = Guid.NewGuid().ToString("N"); //如果没有ID自动生成 } taskInfo.NextRunTime = TaskHelper.CalNextRunTime(taskInfo); //如果时间数据定义不合理会抛出异常并返回消息 if (string.IsNullOrWhiteSpace(taskInfo.Command)) //检查命令是否存在 { result.Code = 404; result.Msg = "The task command is null!"; } else { _cicadaDbContext.TaskInfos.AddOrUpdate(taskInfo); if (taskInfo.Notices != null && taskInfo.Notices.Count > 0) { foreach (Notice notice in taskInfo.Notices) { notice.TaskInfo = taskInfo; if (string.IsNullOrEmpty(notice.NoticeId)) { notice.NoticeId = Guid.NewGuid().ToString("N"); } _cicadaDbContext.Notices.AddOrUpdate(notice); } } _cicadaDbContext.SaveChanges(); result.Code = 200; result.Msg = taskInfo.TaskId; } } catch (Exception ex) { result.Code = 500; result.Msg = ex.ToString(); } return(result); }
public bool AddTask(TaskInfo task) { if (task.NextRunTime == null) { task.NextRunTime = TaskHelper.CalNextRunTime(task); } _tasks[task.TaskId] = task; lock (_runTaskLock) { int removeCount = _needRunTasks.RemoveWhere(f => f.TaskId == task.TaskId); NextRunTask taskStatus = new NextRunTask(); taskStatus.TaskId = task.TaskId; taskStatus.NextRunTime = task.NextRunTime ?? DateTime.Now; _needRunTasks.Add(taskStatus); } return(true); }
/// <summary> /// 启动一个任务 /// </summary> /// <param name="taskId"></param> /// <param name="nextRunTime"></param> /// <returns></returns> public ResultModel StartTask(string taskId, long nextRunTime = 0) { ResultModel result = new ResultModel(); try { var _data = _cicadaDbContext.TaskInfos.Where(i => i.TaskId == taskId).FirstOrDefault(); if (_data == null) { result.Code = 404; result.Msg = "No Record"; } else if (_data.Status == 0) { result.Code = 501; result.Msg = "This task is already start."; } else { result.Code = 200; if (nextRunTime != 0) { _data.NextRunTime = DateTimeHelper.GetTime(nextRunTime); } else { _data.NextRunTime = TaskHelper.CalNextRunTime(_data); } _data.Status = 0; _cicadaDbContext.SaveChanges(); } } catch (Exception ex) { result.Code = 500; result.Msg = ex.Message; } return(result); }
public bool AddTasks(IEnumerable <TaskInfo> tasks) { lock (_runTaskLock) { foreach (var task in tasks) { _tasks[task.TaskId] = task; if (task.NextRunTime == null) { task.NextRunTime = TaskHelper.CalNextRunTime(task); } int removeCount = _needRunTasks.RemoveWhere(f => f.TaskId == task.TaskId); NextRunTask taskStatus = new NextRunTask(); taskStatus.TaskId = task.TaskId; taskStatus.NextRunTime = task.NextRunTime ?? DateTime.Now; _needRunTasks.Add(taskStatus); } } return(true); }