void ShowSelectedTaskTypeOption() { PanelOnce.Visible = false; PanelRepeat.Visible = false; PanelDayOfWeek.Visible = false; ScheduledTaskType type = ScheduledTaskType.Once; try { type = (ScheduledTaskType)(int.Parse(DropDownListScheduledTaskType.SelectedValue)); } catch (Exception) { } switch (type) { case ScheduledTaskType.Once: PanelOnce.Visible = true; break; case ScheduledTaskType.Repeat: PanelRepeat.Visible = true; break; case ScheduledTaskType.DayOfWeek: PanelDayOfWeek.Visible = true; break; } }
private static ScheduledTaskBase GetScheduledTask(string cacheItemKey) { ScheduledTaskType taskType = GetScheduledTaskType(cacheItemKey); ScheduledTaskBase scheduledTask = _scheduledTasks[taskType]; return(scheduledTask); }
private static void RunTask(string cacheItemKey) { System.Diagnostics.Debug.WriteLine(string.Format("{0} removed from cache", cacheItemKey)); int startIndex = ScheduledtaskStringPrefix.Length; string substring = cacheItemKey.Substring(startIndex, cacheItemKey.Length - startIndex); ScheduledTaskType taskType = (ScheduledTaskType)Enum.Parse(typeof(ScheduledTaskType), substring); ScheduledTaskBase scheduledTask = _scheduledTasks[taskType]; //Don't run any data retreivals between 4 and 5 in the morning, just re-queue in one hour if (DateTime.Now.Hour == 4) { const int cacheSeconds = 3600; System.Diagnostics.Debug.WriteLine(string.Format("{0} is re-queued in {1}", cacheItemKey, cacheSeconds)); QueueTask(scheduledTask, cacheSeconds); } else { try { System.Diagnostics.Debug.WriteLine(string.Format("{0} is executed", cacheItemKey)); scheduledTask.Execute(); } catch (Exception) { } System.Diagnostics.Debug.WriteLine(string.Format("{0} is re-queued in {1} seconds", cacheItemKey, scheduledTask.Interval.TotalSeconds)); QueueTask(scheduledTask); } }
private static Mock <IScheduledTask> CreateMockScheduledTask(ScheduledTaskType scheduledTaskType) { var mockScheduledTask = new Mock <IScheduledTask>(); mockScheduledTask.SetupGet(s => s.ScheduledTaskType).Returns(scheduledTaskType); return(mockScheduledTask); }
private static ScheduledTaskType GetScheduledTaskType(string cacheItemKey) { int startIndex = ScheduledtaskStringPrefix.Length; string substring = cacheItemKey.Substring(startIndex, cacheItemKey.Length - startIndex); ScheduledTaskType taskType = (ScheduledTaskType)Enum.Parse(typeof(ScheduledTaskType), substring); return(taskType); }
private static void FinishTask(string cacheItemKey) { ScheduledTaskType scheduledTaskType = GetScheduledTaskType(cacheItemKey); if (_runningTasks.ContainsKey(scheduledTaskType)) { _runningTasks.Remove(scheduledTaskType); } }
private static DataScheduledTask CreateScheduledTask(Instant currentInstant, ScheduledTaskType scheduledTaskType, bool isDue) { var nextRunTimeOffset = isDue ? -1.Seconds() : 1.Seconds(); return(new DataScheduledTask { NextRunTime = currentInstant.Plus(nextRunTimeOffset), ScheduledTaskType = scheduledTaskType }); }
public static void ExecuteTaskNow(ScheduledTaskType scheduledTaskType) { string name = string.Format("{0}{1}", ScheduledtaskStringPrefix, scheduledTaskType); ScheduledTaskBase scheduledTask = GetScheduledTask(name); if (HttpRuntime.Cache[name] == null) { // Task doesn't exist in cache. Probably running. Task task; if (_runningTasks.TryGetValue(scheduledTaskType, out task)) { if (!task.IsCompleted) { task.ContinueWith((t) => { scheduledTask.Execute(); }); } else { scheduledTask.Execute(); } //if (!task.IsCompleted) //{ // task.Wait(); //} //Thread.Sleep(100); //if (HttpRuntime.Cache[name] != null) //{ // HttpRuntime.Cache.Remove(name); //} //else //{ // scheduledTask.Execute(); //} //RunTask(name); //task.ContinueWith((t) => RunTask(name)); } else { scheduledTask.Execute(); //RunTask(name); } } else { HttpRuntime.Cache.Remove(name); } }
void SetScheduledTaskType(ScheduledTaskType type) { for (int i = 0; i < DropDownListScheduledTaskType.Items.Count; i++) { ListItem item = DropDownListScheduledTaskType.Items[i]; if (int.Parse(item.Value) == (int)type) { DropDownListScheduledTaskType.SelectedIndex = i; ShowSelectedTaskTypeOption(); break; } } }
/// <summary> /// Triggered when the cache item is removed from cache. /// The task is executed and then queued again. /// </summary> private static void CacheItemRemoved(string cacheItemKey, object seconds, CacheItemRemovedReason removedReason) { //Task.Factory.StartNew() Task task = new Task(() => RunTask(cacheItemKey)); Task continuationTask = task.ContinueWith((t) => FinishTask(cacheItemKey)); ScheduledTaskType scheduledTaskType = GetScheduledTaskType(cacheItemKey); if (!_runningTasks.ContainsKey(scheduledTaskType)) { _runningTasks.Add(scheduledTaskType, task); } task.Start(); }
private IScheduledTask GetScheduledTask(ScheduledTaskType scheduledTaskType) => this.scheduledTasks.Single(t => t.ScheduledTaskType == scheduledTaskType);
void SetScheduledTaskType(ScheduledTaskType type) { for (int i = 0; i < DropDownListScheduledTaskType.Items.Count; i++) { ListItem item = DropDownListScheduledTaskType.Items[i]; if (int.Parse(item.Value) == (int)type) { DropDownListScheduledTaskType.SelectedIndex = i; ShowSelectedTaskTypeOption(); break; } } }
protected void FinishButton_Click(object sender, EventArgs e) { try { IAutomation automation = AutomationEditor1.Automation; if (automation != null) { string name = TextBoxName.Text.Trim(); string comment = TextBoxComment.Text.Trim(); foreach (ScheduledTaskUnit unit in TheAdminServer.ScheduledTaskManager.ScheduledTaskList) { if (unit.Task.SecurityObject.Name == name) { //重名的判定: //1.添加时与任何一个已有的计划任务名称相同 //2.修改时与除本身外的任何一个已有的计划任务名称相同 if ((_opType == PageOpType.Add) || ((_opType == PageOpType.Modify) && (unit.Task.SecurityObject.Id != _taskUnit.Task.SecurityObject.Id))) { OperateResult.Text = "该计划任务名称已存在" + StringDef.Colon + "计划任务ID - " + unit.Task.SecurityObject.Id.ToString(); return; } } } ScheduledTaskType type = (ScheduledTaskType)(int.Parse(DropDownListScheduledTaskType.SelectedValue)); if (_opType == PageOpType.Add) { IScheduledTask task = null; switch (type) { case ScheduledTaskType.Once: { OnceScheduledTask onceTask = new OnceScheduledTask(); onceTask.Automation = automation; onceTask.ExecuteTime = DateTime.Parse(TextBoxExecuteTime.Text); task = onceTask; } break; case ScheduledTaskType.Repeat: { RepeatScheduledTask repeatTask = new RepeatScheduledTask(); repeatTask.Automation = automation; repeatTask.StartTime = TimeSpan.Parse(TextBoxStartTime.Text); repeatTask.EndTime = TimeSpan.Parse(TextBoxEndTime.Text); repeatTask.Interval = TimeSpan.Parse(TextBoxInterval.Text); task = repeatTask; } break; case ScheduledTaskType.DayOfWeek: { DayOfWeekScheduledTask dayOfWeekTask = new DayOfWeekScheduledTask(); dayOfWeekTask.Automation = automation; dayOfWeekTask.TimeOfDay = TimeSpan.Parse(TextBoxDayOfWeekExecuteTime.Text); int count = 0; foreach (ListItem item in CheckBoxListDayOfWeek.Items) { if (item.Selected) { ++count; } } dayOfWeekTask.DayOfWeek = new DayOfWeek[count]; count = 0; foreach (ListItem item in CheckBoxListDayOfWeek.Items) { if (item.Selected) { dayOfWeekTask.DayOfWeek[count] = (DayOfWeek)int.Parse(item.Value); ++count; } } task = dayOfWeekTask; } break; } TheAdminServer.ScheduledTaskManager.Add(task, name, comment); } else if (_opType == PageOpType.Modify) { IScheduledTask task = null; //如果类型没有改变,则不需要生成新的对象;否则,需要根据类型生成新的对象 if (_taskUnit.Task.Type == type) { task = _taskUnit.Task; switch (type) { case ScheduledTaskType.Once: { OnceScheduledTask onceTask = (OnceScheduledTask)task; onceTask.Automation = automation; onceTask.ExecuteTime = DateTime.Parse(TextBoxExecuteTime.Text); } break; case ScheduledTaskType.Repeat: { RepeatScheduledTask repeatTask = (RepeatScheduledTask)task; repeatTask.Automation = automation; repeatTask.StartTime = TimeSpan.Parse(TextBoxStartTime.Text); repeatTask.EndTime = TimeSpan.Parse(TextBoxEndTime.Text); repeatTask.Interval = TimeSpan.Parse(TextBoxInterval.Text); } break; case ScheduledTaskType.DayOfWeek: { DayOfWeekScheduledTask dayOfWeekTask = task as DayOfWeekScheduledTask; dayOfWeekTask.Automation = automation; dayOfWeekTask.TimeOfDay = TimeSpan.Parse(TextBoxDayOfWeekExecuteTime.Text); int count = 0; foreach (ListItem item in CheckBoxListDayOfWeek.Items) { if (item.Selected) { ++count; } } dayOfWeekTask.DayOfWeek = new DayOfWeek[count]; count = 0; foreach (ListItem item in CheckBoxListDayOfWeek.Items) { if (item.Selected) { dayOfWeekTask.DayOfWeek[count] = (DayOfWeek)int.Parse(item.Value); ++count; } } } break; } } else { switch (type) { case ScheduledTaskType.Once: { OnceScheduledTask onceTask = new OnceScheduledTask(); onceTask.Automation = automation; onceTask.ExecuteTime = DateTime.Parse(TextBoxExecuteTime.Text); task = onceTask; } break; case ScheduledTaskType.Repeat: { RepeatScheduledTask repeatTask = new RepeatScheduledTask(); repeatTask.Automation = automation; repeatTask.StartTime = TimeSpan.Parse(TextBoxStartTime.Text); repeatTask.EndTime = TimeSpan.Parse(TextBoxEndTime.Text); repeatTask.Interval = TimeSpan.Parse(TextBoxInterval.Text); task = repeatTask; } break; case ScheduledTaskType.DayOfWeek: { DayOfWeekScheduledTask dayOfWeekTask = new DayOfWeekScheduledTask(); dayOfWeekTask.Automation = automation; dayOfWeekTask.TimeOfDay = TimeSpan.Parse(TextBoxDayOfWeekExecuteTime.Text); int count = 0; foreach (ListItem item in CheckBoxListDayOfWeek.Items) { if (item.Selected) { ++count; } } dayOfWeekTask.DayOfWeek = new DayOfWeek[count]; count = 0; foreach (ListItem item in CheckBoxListDayOfWeek.Items) { if (item.Selected) { dayOfWeekTask.DayOfWeek[count] = (DayOfWeek)int.Parse(item.Value); ++count; } } task = dayOfWeekTask; } break; } } TheAdminServer.ScheduledTaskManager.Update(_taskUnit.Task.SecurityObject.Id, task, name, comment); } Response.Redirect("ScheduledTaskList.aspx"); } else { OperateResult.Text = "未指定要执行的自动化任务(需要在第一步中指定并点击'应用'按钮)"; return; } } catch (Exception ex) { OperateResult.Text = StringDef.ParameterInputError + StringDef.Colon + ex.Message; //Response.Write("<script language='JavaScript'>alert('参数有误!');</script>"); //ShowMessage("参数有误", MessageType.Failure); } }
private bool SummaryTaskIsDue(IReadOnlyList <ScheduledTask> scheduledTasks, ScheduledTaskType summaryTaskType) { var summaryTask = scheduledTasks.Single(t => t.ScheduledTaskType == summaryTaskType); return(summaryTask.NextRunTime <= this.dateCalculator.CurrentInstant); }
public void Update(int ScheduledTaskTypeKey,string ScheduledTaskTypeDescription) { ScheduledTaskType item = new ScheduledTaskType(); item.MarkOld(); item.IsLoaded = true; item.ScheduledTaskTypeKey = ScheduledTaskTypeKey; item.ScheduledTaskTypeDescription = ScheduledTaskTypeDescription; item.Save(UserName); }
public void Insert(int ScheduledTaskTypeKey,string ScheduledTaskTypeDescription) { ScheduledTaskType item = new ScheduledTaskType(); item.ScheduledTaskTypeKey = ScheduledTaskTypeKey; item.ScheduledTaskTypeDescription = ScheduledTaskTypeDescription; item.Save(UserName); }
public Schedule(ScheduledTaskType scheduledTaskType, Instant nextRunTime) { ScheduledTaskType = scheduledTaskType; NextRunTime = nextRunTime; }