public void ExecuteTaskOnce(string Id) { var taskPool = _tasks.Where(x => x.Id == Id).FirstOrDefault(); if (taskPool == null) { return; } taskPool.ExecuteOnce((t) => { var _taskLogic = new TaskBusinessLogic(this); var task = _taskLogic.GetAllTasks(true).Where(x => x.Type == t.Type).FirstOrDefault(); var start = DateTime.Now; //new instance each time. t.Instance = _resolve(task.Type); task.LastStart = start; task.NextRunTime = start.AddMilliseconds(taskPool.Interval); _taskLogic.UpdateTask(task); }, (t) => { var _taskLogic = new TaskBusinessLogic(this); var task = _taskLogic.GetAllTasks(true).Where(x => x.Type == t.Type).FirstOrDefault(); var endTime = DateTime.Now; task.LastSuccess = t.Status.Successful ? endTime : task.LastSuccess; task.LastEnd = endTime; if (task.LastSuccess != task.LastEnd) { task.LastException = t.Status.Exception.ToString(); } taskPool.StartImmediate = false; _taskLogic.UpdateTask(task); }); }
public virtual void Start(string Id) { try { var tasks = Id == null ? _tasks : _tasks.Where(x => x.Id == Id); foreach (var taskThread in tasks) { if (taskThread.IsPoolsTaskCurrentlyRunning) { continue; } if (Id != null) { taskThread.StartImmediate = true; } if (taskThread.Task.Name == "TaskHealthChecker") { taskThread.SetTimer((t) => { var _taskLogic = new TaskBusinessLogic(this); var task = _taskLogic.GetAllTasks(true).Where(x => x.Type == t.Type).FirstOrDefault(); var start = DateTime.Now; task.LastStart = start; task.NextRunTime = start.AddMilliseconds(taskThread.Interval); _taskLogic.UpdateTask(task); }, (t) => { var _taskLogic = new TaskBusinessLogic(this); var task = _taskLogic.GetAllTasks(true).Where(x => x.Type == t.Type).FirstOrDefault(); var endTime = DateTime.Now; task.LastSuccess = t.Status.Successful ? endTime : task.LastSuccess; task.LastEnd = endTime; if (task.LastSuccess != task.LastEnd) { task.LastException = t.Status.Exception.ToString(); } taskThread.StartImmediate = false; _taskLogic.UpdateTask(task); }); } } } catch (Exception ex) { var e = ex; } }