/// <summary> /// 删除计划任务的执行 /// </summary> /// <param name="scheduleTask">计划任务</param> public void RemoveSchedule(IScheduleTask scheduleTask) { if (scheduleTask == null) throw new ArgumentNullException("scheduleTask"); RemoveSchedule(scheduleTask.Id); }
public static void UnscheduleTask(IScheduleTask task) { lock (_lock) { var entry = _pendingTasks.FirstOrDefault(p => p.Value == task); if (entry.IsNotDefault()) { _pendingTasks.Remove(entry.Key); ProcessTasks(null); } } }
public static void ScheduleTask(IScheduleTask task) { lock (_lock) { DateTime occursAt = task.GetNextReoccurrence(); while (_pendingTasks.ContainsKey(occursAt)) { occursAt = occursAt.AddMilliseconds(1); } _pendingTasks.Add(occursAt, task); ProcessTasks(null); } }
public void AddTask(IScheduleTask task) { lock (_locker) { if (task.Name == COATImportTask.ImportTaskName) { // ReSharper disable SuspiciousTypeConversion.Global _db.ImportTasks.AddObject((ImportTask) task); // ReSharper restore SuspiciousTypeConversion.Global _db.SaveChanges(); } _tasks.Enqueue(task); } TryStart(); }
public bool CheckTaskAvailable(IScheduleTask task) { var op = operations.Any(o => o.Name == task.OperationName); if (!op) return false; var tnames = task.Triggers.Select(o => o.TriggerName).ToArray(); foreach (var tn in tnames) { var tt = task.Triggers.First(o => o.TriggerName == tn); var t = triggers.Any(o => o.Name == tn); if (!t || !tt.Enabled || (tt.BeginTime.HasValue && DateTime.Today < tt.BeginTime.Value) || (tt.EndTime.HasValue && DateTime.Today > tt.EndTime.Value)) { task.Triggers.Remove(tt); } } return task.Triggers.Count > 0; }
/// <summary> /// 添加计划任务 /// </summary> /// <param name="scheduleTask">计划任务</param> public void Add(IScheduleTask scheduleTask) { if (scheduleTask == null) throw new ArgumentNullException("scheduleTask"); lock (_lock) { IScheduleTask find = null; if (this.TryFind(scheduleTask.Id, out find)) { throw new ArgumentException( string.Format(CultureInfo.InvariantCulture, "ScheduleTask already exists"), "scheduleTask"); } } lock (_lock) { _collection.Add(scheduleTask); } }
/// <summary> /// Determines whether [is scheduled task] [the specified task]. /// </summary> /// <param name="task"> /// The task. /// </param> /// <returns> /// <c>true</c> if [is scheduled task] [the specified task]; otherwise, <c>false</c>. /// </returns> public static bool IsScheduledTask(IScheduleTask task) { return(s_PendingTasks.ContainsValue(task)); }
/// <summary> /// 尝试根据计划任务ID查找指定的计划任务 /// </summary> /// <param name="scheduleTaskId">计划任务ID</param> /// <param name="scheduleTask">指定的计划任务</param> /// <returns>指定的计划任务是否存在</returns> public bool TryFind(string scheduleTaskId, out IScheduleTask scheduleTask) { bool result = false; scheduleTask = null; lock (_lock) { foreach (var item in _collection) { if (item.Id == scheduleTaskId) { scheduleTask = item; result = true; break; } } } return result; }
public static bool IsScheduledTask(IScheduleTask task) { return _pendingTasks.ContainsValue(task); }
public void scheduleTask(IScheduleTask scheduleTask) { }
private static void ExecuteTask(IScheduleTask scheduleTask, bool throwException = false, bool dispose = true, bool ensureRunOnOneWebFarmInstance = true) { //var scope = EngineContextExperimental.Current.ContainerManager.Scope(); try { //task is run on one farm node at a time? if (ensureRunOnOneWebFarmInstance) //ten kod odpowiada za odpalaanie tylko na 1 serwerze { ////is web farm enabled (multiple instances)? //var grandConfig = EngineContextExperimental.Current.ContainerManager.Resolve<GrandConfig>("", scope); //if (grandConfig.MultipleInstancesEnabled) //{ // var machineNameProvider = EngineContextExperimental.Current.ContainerManager.Resolve<IMachineNameProvider>("", scope); // var machineName = machineNameProvider.GetMachineName(); // if (String.IsNullOrEmpty(machineName)) // { // throw new Exception("Machine name cannot be detected. You cannot run in web farm."); // //actually in this case we can generate some unique string (e.g. Guid) and store it in some "static" (!!!) variable // //then it can be used as a machine name // } // //lease can't be aquired only if for a different machine and it has not expired // if (scheduleTask.LeasedUntilUtc.HasValue && // scheduleTask.LeasedUntilUtc.Value >= DateTime.UtcNow && // scheduleTask.LeasedByMachineName != machineName) // return; // //lease the task. so it's run on one farm node at a time // scheduleTask.LeasedByMachineName = machineName; // scheduleTask.LeasedUntilUtc = DateTime.UtcNow.AddMinutes(30); //} } scheduleTask.LastStartUtc = DateTime.UtcNow; scheduleTask.Execute(); scheduleTask.LastSuccessUtc = DateTime.UtcNow; scheduleTask.LastNonSuccessEndUtc = null; } catch (Exception exc) { scheduleTask.Enabled = !scheduleTask.StopOnError; scheduleTask.LastNonSuccessEndUtc = DateTime.UtcNow; scheduleTask.LastSuccessUtc = null; //log error //var logger = EngineContextExperimental.Current.ContainerManager.Resolve<ILogger>("", scope); //logger.Error(string.Format("Error while running the '{0}' schedule task. {1}", scheduleTask.ScheduleTaskName, exc.Message), exc); //if (throwException) // throw; } finally { var scheduleTaskService = EngineContextExperimental.Current.Resolve <IScheduleTaskService>(); var taskToUpdate = scheduleTaskService.GetTaskByType(scheduleTask.Type); taskToUpdate.Enabled = scheduleTask.Enabled; taskToUpdate.LastStartUtc = scheduleTask.LastStartUtc; taskToUpdate.LastNonSuccessEndUtc = scheduleTask.LastNonSuccessEndUtc; taskToUpdate.LastSuccessUtc = scheduleTask.LastSuccessUtc; taskToUpdate.LeasedByMachineName = scheduleTask.LeasedByMachineName; taskToUpdate.LeasedUntilUtc = scheduleTask.LeasedUntilUtc; scheduleTaskService.UpdateTask(taskToUpdate); //dispose all resources if (dispose) { //scope.Dispose(); } } }
public void schedule(IScheduleTask scheduleTask, TimeSpan timeSpan) { _eventLoop.Schedule(scheduleTask, timeSpan); }
/// <summary> /// 删除计划任务 /// </summary> /// <param name="scheduleTask">计划任务</param> public void Remove(IScheduleTask scheduleTask) { Remove(scheduleTask.Id); }
private static void ExecuteTask(IScheduleTask scheduleTask, bool manualstart = false) { try { if (!scheduleTask.Enabled && !manualstart) { return; } bool runTask = true; //is web farm enabled (multiple instances)? var grandConfig = EngineContext.Current.Resolve <GrandConfig>(); if (grandConfig.MultipleInstancesEnabled && !manualstart) { var machineNameProvider = EngineContext.Current.Resolve <IMachineNameProvider>(); var machineName = machineNameProvider.GetMachineName(); if (String.IsNullOrEmpty(machineName)) { throw new Exception("Machine name cannot be detected. You cannot run in web farm."); //actually in this case we can generate some unique string (e.g. Guid) and store it in some "static" (!!!) variable //then it can be used as a machine name } if (!string.IsNullOrEmpty(scheduleTask.LeasedByMachineName) && (machineName != scheduleTask.LeasedByMachineName)) { runTask = false; scheduleTask.LastStartUtc = DateTime.UtcNow; scheduleTask.LastNonSuccessEndUtc = DateTime.UtcNow; } } if (runTask == true) { scheduleTask.LastStartUtc = DateTime.UtcNow; scheduleTask.Execute(); scheduleTask.LastSuccessUtc = DateTime.UtcNow; scheduleTask.LastNonSuccessEndUtc = null; } } catch (Exception exc) { scheduleTask.Enabled = !scheduleTask.StopOnError; scheduleTask.LastNonSuccessEndUtc = DateTime.UtcNow; scheduleTask.LastSuccessUtc = null; //log error var logger = EngineContext.Current.Resolve <ILogger>(); logger.Error(string.Format("Error while running the '{0}' schedule task. {1}", scheduleTask.ScheduleTaskName, exc.Message), exc); } finally { var scheduleTaskService = EngineContext.Current.Resolve <IScheduleTaskService>(); var taskToUpdate = scheduleTaskService.GetTaskByType(scheduleTask.Type); taskToUpdate.Enabled = scheduleTask.Enabled; taskToUpdate.LastStartUtc = scheduleTask.LastStartUtc; taskToUpdate.LastNonSuccessEndUtc = scheduleTask.LastNonSuccessEndUtc; taskToUpdate.LastSuccessUtc = scheduleTask.LastSuccessUtc; taskToUpdate.LeasedUntilUtc = scheduleTask.LeasedUntilUtc; scheduleTaskService.UpdateTask(taskToUpdate); } }
/// <summary> /// 计划任务预执行 /// </summary> /// <param name="state">计划任务</param> private void PreSchedule(IScheduleTask state) { if (state == null) return; PerformSchedule(state); }
/// <summary> /// 执行计划任务 /// </summary> /// <param name="scheduleTask">计划任务</param> private void PerformSchedule(IScheduleTask scheduleTask) { // 如果之前已经执行该任务则删除 PerformRemoveSchedule(scheduleTask.Id); // 将任务缓存至集合中 ScheduleTaskCollection.Instance.Add(scheduleTask); // 订阅终结事件 scheduleTask.ScheduleTaskTerminatedEvent += new EventHandler<ScheduleTaskTerminatedEventArgs>(OnScheduleTaskTerminatedEvent); // 为Trigger分配线程执行 scheduleTask.Start(); }
/// <summary> /// 执行计划任务 /// </summary> /// <param name="scheduleTask">计划任务</param> public void Schedule(IScheduleTask scheduleTask) { if (scheduleTask == null) throw new ArgumentNullException("scheduleTask"); _queue.Enqueue(scheduleTask); }
public void schedule(IScheduleTask scheduleTask, TimeSpan timeSpan) { _hashedWheelTimer.NewTimeout(scheduleTask, timeSpan); }