/// <summary> /// 启用任务调度 /// 启动调度时会把任务表中状态为“执行中”的任务加入到任务调度队列中 /// </summary> public void StartScheduler() { try { if (_scheduler.IsStarted) { return; } //添加全局监听 _scheduler.ListenerManager.AddTriggerListener(new CustomTriggerListener(), GroupMatcher <TriggerKey> .AnyGroup()); _scheduler.Start(); //获取所有执行中的任务 foreach (var taskConfig in _taskConfigs) { try { ScheduleJob(taskConfig); } catch (Exception ex) { ZilLionLoger.WriteErrLog(ex); } } ZilLionLoger.WriteTraceLog($"scheduler:{_schedulerKey}成功启动!"); } catch (Exception ex) { ZilLionLoger.WriteErrLog(ex); } }
/// <summary> /// 初始化任务调度对象 /// </summary> public void InitScheduler(string schedulerKey, IList <TaskConfig> taskConfigs ) { try { var properties = new NameValueCollection { ["quartz.scheduler.instanceName"] = schedulerKey, ["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz", ["quartz.threadPool.threadCount"] = "10", ["quartz.threadPool.threadPriority"] = "Normal", ["quartz.jobStore.misfireThreshold"] = "60000", ["quartz.jobStore.type"] = "Quartz.Simpl.RAMJobStore, Quartz" }; ISchedulerFactory factory = new StdSchedulerFactory(properties); _schedulerKey = schedulerKey; _taskConfigs = taskConfigs; _scheduler = factory.GetScheduler(); _scheduler.Clear(); } catch (Exception ex) { ZilLionLoger.WriteErrLog(ex); } }
private static void WriteLogTofile() { try { var path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\TaskLog\"; #if DEBUG if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } #endif if (!Directory.Exists(path)) { return; } var logName = $@"ZiLion_Log_{DateTime.Now:yyyyMMddHH}.TXT"; var stream = new StreamWriter(path + logName, true, Encoding.Default); stream.WriteLine(AppDomain.CurrentDomain.FriendlyName + "*********" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")); stream.Flush(); stream.Close(); } catch (Exception ex) { Debug.WriteLine(ex.Message); ZilLionLoger.WriteErrLog(ex); } }
/// <summary> /// 初始化任务调度对象 /// </summary> public void InitScheduler(string schedulerKey, IList <TaskConfig> taskConfigs, TaskRunnerConfig runnerConfig ) { try { if (_hasinited) { throw new Exception("SchedulerContainer 只能初始化一次"); } var properties = new NameValueCollection { ["quartz.scheduler.instanceName"] = schedulerKey, ["quartz.threadPool.type"] = "Quartz.Simpl.SimpleThreadPool, Quartz", ["quartz.threadPool.threadCount"] = "10", ["quartz.threadPool.threadPriority"] = "Normal", ["quartz.jobStore.misfireThreshold"] = "60000", ["quartz.jobStore.type"] = "Quartz.Simpl.RAMJobStore, Quartz" }; ISchedulerFactory factory = new StdSchedulerFactory(properties); _schedulerKey = schedulerKey; _taskConfigs = taskConfigs; _runnerConfig = runnerConfig; _taskRunLogRespository = new TaskRunLogRespository(_runnerConfig); _scheduler = factory.GetScheduler(); _scheduler.Clear(); _hasinited = true; } catch (Exception ex) { ZilLionLoger.WriteErrLog(ex); } }
/// <summary> /// 停止任务调度 /// </summary> public void StopSchedule() { try { //判断调度是否已经关闭 if (!_scheduler.IsShutdown) { _scheduler.Shutdown(true); } } catch (Exception ex) { ZilLionLoger.WriteErrLog(ex); } }
/// <summary> /// 立即运行一次任务 /// </summary> /// <param name="jobKey">任务key</param> public void RunOnceTask(string jobKey) { var jk = new JobKey(jobKey); if (!_scheduler.CheckExists(jk)) { return; } var jobDetail = _scheduler.GetJobDetail(jk); var type = jobDetail.JobType; var instance = type.FastNew(); var method = type.GetMethod("Execute"); method.Invoke(instance, new object[] { null }); ZilLionLoger.WriteTraceLog($"任务“{jobKey}”立即运行"); }
/// <summary> /// 暂停任务 /// </summary> /// <param name="jobKey"></param> public void PauseJob(string jobKey) { var jk = new JobKey(jobKey); if (_scheduler.CheckExists(jk)) { //任务已经存在则暂停任务 _scheduler.PauseJob(jk); var jobDetail = _scheduler.GetJobDetail(jk); if (jobDetail.JobType.GetInterface("IInterruptableJob") != null) { _scheduler.Interrupt(jk); } ZilLionLoger.WriteWarnLog($"任务“{jobKey}”已经暂停"); } }