/// <summary> /// 获取状态的描述 /// </summary> /// <param name="state"></param> /// <returns></returns> public static string GetTriggerState(this TriggerState state) { switch (state) { case TriggerState.Blocked: return("阻塞"); case TriggerState.Complete: return("完成"); case TriggerState.Error: return("错误"); case TriggerState.None: return("无状态"); case TriggerState.Normal: return("正常"); case TriggerState.Paused: return("暂停"); default: return(state.ToString()); } }
public void InitTrigger(ListViewItem Item, ITrigger Trigger) { Item.SubItems.Clear(); TriggerState ts = scheduler.GetScheduler().GetTriggerState(Trigger.Key); Item.Text = ts.ToString(); Item.SubItems.Add(Trigger.Key.Name); Item.SubItems.Add(Trigger.Key.Group); string express = ""; if (Trigger is SimpleTriggerImpl) { express = string.Format("repeate {0} times;repeat-interval {1}ms", (Trigger as ISimpleTrigger).RepeatCount, (Trigger as ISimpleTrigger).RepeatInterval); } else { express = (Trigger as ICronTrigger).CronExpressionString; } Item.SubItems.Add(express); Item.SubItems.Add(FormatTime(Trigger.StartTimeUtc)); Item.SubItems.Add(FormatTime2(Trigger.EndTimeUtc)); Item.SubItems.Add(FormatTime2(Trigger.GetPreviousFireTimeUtc())); Item.SubItems.Add(FormatTime2(Trigger.GetNextFireTimeUtc())); }
/// <summary> /// 根据组名获取任务 /// </summary> /// <param name="GroupName"></param> /// <returns></returns> public static List <TaskEntity> GetTasks(string GroupName) { List <TaskEntity> jobs = new List <TaskEntity>(); foreach (JobKey job in _scheduler.GetJobKeys(GroupMatcher <JobKey> .GroupEquals(GroupName))) { IJobDetail jobDetail = _scheduler.GetJobDetail(job); if (jobDetail != null) { IList <ITrigger> triggers = GetTriggersOfJob(jobDetail.Key); TaskEntity task = new TaskEntity(); TriggerState State = _scheduler.GetTriggerState(triggers[0].Key); task.Name = jobDetail.Key.Name; task.Group = jobDetail.Key.Group; //task.Description = jobDetail.Description; task.AssemblyName = jobDetail.JobType.Namespace.ToString(); task.ClassName = jobDetail.JobType.FullName.ToString(); task.BeginTime = triggers[0].StartTimeUtc; if (triggers[0].EndTimeUtc != null) { task.EndTime = triggers[0].EndTimeUtc; } else { task.EndTime = null; } if (triggers[0] is SimpleTriggerImpl) { task.Type = 0; task.RepeatCount = ((SimpleTriggerImpl)triggers[0]).RepeatCount; task.RepeatInterval = ((SimpleTriggerImpl)triggers[0]).RepeatInterval; task.FiredCount = ((SimpleTriggerImpl)triggers[0]).TimesTriggered; task.Description = ((SimpleTriggerImpl)triggers[0]).Description; } else if (triggers[0] is CronTriggerImpl) { task.Type = 1; task.CronExpression = ((CronTriggerImpl)triggers[0]).CronExpressionString.ToString(); task.Description = ((CronTriggerImpl)triggers[0]).Description; } if (triggers[0].GetNextFireTimeUtc() != null) { task.NextFireTime = triggers[0].GetNextFireTimeUtc(); } else { task.NextFireTime = null; } task.State = State.ToString(); jobs.Add(task); } } return(jobs); }
/// <summary> /// 获取任务 /// </summary> /// <param name="name"></param> /// <param name="group"></param> /// <returns></returns> public static TaskEntity GetTask(string name, string group) { JobKey jobkey = new JobKey(name, group); IJobDetail jobDetail = _scheduler.GetJobDetail(jobkey); if (jobDetail != null) { TaskEntity task = new TaskEntity(); IList <ITrigger> triggers = GetTriggersOfJob(jobDetail.Key); TriggerState State = _scheduler.GetTriggerState(triggers[0].Key); task.Name = jobDetail.Key.Name; task.Group = jobDetail.Key.Group; //task.Description = jobDetail.Description; task.AssemblyName = jobDetail.JobType.Namespace.ToString(); task.ClassName = jobDetail.JobType.FullName.ToString(); task.BeginTime = triggers[0].StartTimeUtc; if (triggers[0].EndTimeUtc != null) { task.EndTime = triggers[0].EndTimeUtc; } else { task.EndTime = null; } if (triggers[0] is SimpleTriggerImpl) { task.Type = 0; task.RepeatCount = ((SimpleTriggerImpl)triggers[0]).RepeatCount; task.RepeatInterval = ((SimpleTriggerImpl)triggers[0]).RepeatInterval; task.FiredCount = ((SimpleTriggerImpl)triggers[0]).TimesTriggered; task.Description = ((SimpleTriggerImpl)triggers[0]).Description; } else if (triggers[0] is CronTriggerImpl) { task.Type = 1; task.CronExpression = ((CronTriggerImpl)triggers[0]).CronExpressionString.ToString(); task.Description = ((CronTriggerImpl)triggers[0]).Description; } if (triggers[0].GetNextFireTimeUtc() != null) { task.NextFireTime = triggers[0].GetNextFireTimeUtc(); } else { task.NextFireTime = null; } task.State = State.ToString(); return(task); } return(null); }
/// <summary> /// 更新任务通过盘点计划(已有同样的任务的时候) /// </summary> /// <typeparam name="T"></typeparam> /// <param name="trigger"></param> /// <param name="groupName"></param> /// <param name="jobDetailName"></param> public async Task SchedulerUpdateByPlan(InventoryPlan inventoryPlan) { if (Scheduler == null) { CreateScheduler(); } var triggerKey = GetTriggerKeyByPlan(inventoryPlan); var trigger = (ICronTrigger)await Scheduler.GetTrigger(triggerKey); var currentCron = QuartzUtils.GetQuartzCron(inventoryPlan); //处理是否更新任务的定时时间 if (trigger.CronExpressionString != currentCron) { var triggerNew = TriggerBuilder.Create() .WithIdentity(triggerKey.Name, triggerKey.Group) .ForJob(trigger.JobKey) .WithCronSchedule(currentCron) .Build(); await Scheduler.RescheduleJob(trigger.Key, triggerNew); } //处理是否更新任务的执行状态 TriggerState triggerState = await Scheduler.GetTriggerState(triggerKey); switch (triggerState) { case TriggerState.Normal: if (!inventoryPlan.Enabled) { await Scheduler.PauseTrigger(triggerKey); } break; case TriggerState.Paused: if (inventoryPlan.Enabled) { await Scheduler.ResumeTrigger(triggerKey); } break; //如果处于其他状态,暂不进行任何处理 default: LogUtils.Error($"{triggerKey.Name}定时任务更新时,原定时任务出现其他状态{triggerState.ToString()}"); break; } }
private void BindData() { listView1.Items.Clear(); listView1.Groups.Clear(); string expression, nextTime, previousTime; if (scheduler != null && !scheduler.IsShutdown) { IList <string> groupNames = scheduler.GetTriggerGroupNames(); foreach (string groupName in groupNames) { List <ITrigger> triggers = GetTriggers(GroupMatcher <TriggerKey> .GroupEquals(groupName)); ListViewGroup group = new ListViewGroup(groupName); foreach (ITrigger trigger in triggers) { expression = GetTriggerExpression(trigger); nextTime = trigger.GetNextFireTimeUtc().HasValue ? trigger.GetNextFireTimeUtc().Value.LocalDateTime.ToString("HH:mm:ss") : ""; previousTime = trigger.GetPreviousFireTimeUtc().HasValue ? trigger.GetPreviousFireTimeUtc().Value.LocalDateTime.ToString("HH:mm:ss") : ""; if (trigger.GetPreviousFireTimeUtc().HasValue&& trigger.GetPreviousFireTimeUtc().Value.LocalDateTime.Date < DateTime.Now.Date) { previousTime = "- " + previousTime; } if (trigger.GetNextFireTimeUtc().HasValue&& trigger.GetNextFireTimeUtc().Value.LocalDateTime.Date > DateTime.Now.Date) { nextTime = "+ " + nextTime; } TriggerState state = scheduler.GetTriggerState(trigger.Key); ListViewItem item = new ListViewItem( new string[] { trigger.JobKey.Name, scheduler.GetJobDetail(trigger.JobKey).Description, state.ToString(), previousTime, nextTime, expression }); item.Group = group; item.Tag = trigger; item.BackColor = state == TriggerState.Normal ? Color.Transparent : Color.FromArgb(255, 204, 204); listView1.Groups.Add(group); listView1.Items.Add(item); } } } }
public static string GetTriggerState(TriggerState state) { return(state.ToString()); }
public static IList <TriggerDetails> GetTriggerDetails(IEnumerable <KeyValuePair <ITrigger, Guid> > quartzTriggers) { IList <TriggerDetails> triggerDetails = new List <TriggerDetails>(); foreach (KeyValuePair <ITrigger, Guid> trigger in quartzTriggers) { ITrigger quartzTrigger = trigger.Key; TriggerState triggerState = Scheduler.Instance().GetTriggerState(quartzTrigger.Key); var triggerType = "InstructionNotSet"; var misfireInstruction = string.Empty; var additionalDetails = string.Empty; if (quartzTrigger is ICronTrigger) { triggerType = "Cron"; switch (quartzTrigger.MisfireInstruction) { case 0: misfireInstruction = "SmartPolicy"; break; case 1: misfireInstruction = "FireOnceNow"; break; case 2: misfireInstruction = "DoNothing"; break; case -1: misfireInstruction = "IgnoreMisfirePolicy"; break; } additionalDetails = string.Format("Cron Expression: {0}", ((ICronTrigger)quartzTrigger).CronExpressionString); } if (quartzTrigger is ISimpleTrigger) { triggerType = "Simple"; switch (quartzTrigger.MisfireInstruction) { case 0: misfireInstruction = "SmartPolicy"; break; case 1: misfireInstruction = "FireNow"; break; case 2: misfireInstruction = "RescheduleNowWithExistingRepeatCount"; break; case 3: misfireInstruction = "RescheduleNowWithRemainingRepeatCount"; break; case 4: misfireInstruction = "RescheduleNextWithRemainingCount"; break; case 5: misfireInstruction = "RescheduleNextWithExistingCount"; break; case -1: misfireInstruction = "IgnoreMisfirePolicy"; break; } additionalDetails = string.Format("Repeat Interval: {0}. Repeat Count: {1}", ((ISimpleTrigger)quartzTrigger).RepeatInterval, ((ISimpleTrigger)quartzTrigger).RepeatCount); } var nextFireTimeUtc = quartzTrigger.GetNextFireTimeUtc(); var previousFireTimeUtc = quartzTrigger.GetPreviousFireTimeUtc(); var jobDataMap = new Dictionary <string, object>(); if (null != quartzTrigger.JobDataMap) { foreach (var jobData in quartzTrigger.JobDataMap) { jobDataMap.Add(jobData.Key, jobData.Value); } } triggerDetails.Add(new TriggerDetails { Id = trigger.Value, Name = quartzTrigger.Key.Name, Group = quartzTrigger.Key.Group, JobName = quartzTrigger.JobKey.Name, JobGroup = quartzTrigger.JobKey.Group, Description = quartzTrigger.Description, CalendarName = quartzTrigger.CalendarName, StartTimeUtc = quartzTrigger.StartTimeUtc.UtcDateTime, EndTimeUtc = (quartzTrigger.EndTimeUtc.HasValue) ? quartzTrigger.EndTimeUtc.Value.UtcDateTime : (DateTime?)null, NextFireTimeUtc = (nextFireTimeUtc.HasValue) ? nextFireTimeUtc.Value.UtcDateTime : (DateTime?)null, PreviousFireTimeUtc = (previousFireTimeUtc.HasValue) ? previousFireTimeUtc.Value.UtcDateTime : (DateTime?)null, FinalFireTimeUtc = (quartzTrigger.FinalFireTimeUtc.HasValue) ? quartzTrigger.FinalFireTimeUtc.Value.UtcDateTime : (DateTime?)null, Type = triggerType, MisfireInstruction = misfireInstruction, AdditionalDetails = additionalDetails, JobDataMap = (jobDataMap.Count > 0) ? jobDataMap : null, State = triggerState.ToString() }); } return(triggerDetails); }