| | |
| | | using WIDESEAWCS_Core.Helper; |
| | | using WIDESEAWCS_QuartzJob.DTO; |
| | | using WIDESEAWCS_QuartzJob.CustomException; |
| | | using Quartz.Impl.Matchers; |
| | | |
| | | namespace WIDESEAWCS_QuartzJob |
| | | { |
| | | public class SchedulerCenterServer : ISchedulerCenter |
| | | { |
| | | private Task<IScheduler> _scheduler; |
| | | private IScheduler _scheduler; |
| | | private readonly IJobFactory _iocjobFactory; |
| | | public SchedulerCenterServer(IJobFactory jobFactory) |
| | | { |
| | | _iocjobFactory = jobFactory; |
| | | _scheduler = GetSchedulerAsync(); |
| | | } |
| | | private Task<IScheduler> GetSchedulerAsync() |
| | | private IScheduler GetSchedulerAsync() |
| | | { |
| | | if (_scheduler != null) |
| | | return this._scheduler; |
| | |
| | | { |
| | | { "quartz.serializer.type", "binary" }, |
| | | }; |
| | | StdSchedulerFactory factory = new StdSchedulerFactory(collection); |
| | | return _scheduler = factory.GetScheduler(); |
| | | //StdSchedulerFactory factory = new StdSchedulerFactory(collection); |
| | | StdSchedulerFactory factory = new StdSchedulerFactory(); |
| | | return _scheduler = factory.GetScheduler().Result; |
| | | } |
| | | catch (Exception ex) |
| | | { |
| | | throw new QuartzJobException(string.Format(QuartzJobExceptionMessage.JobFactoryInstanceException, ex.Message), innerException: ex); |
| | | } |
| | | |
| | | } |
| | | } |
| | | |
| | |
| | | WebResponseContent result = new WebResponseContent(); |
| | | try |
| | | { |
| | | this._scheduler.Result.JobFactory = this._iocjobFactory; |
| | | if (!this._scheduler.Result.IsStarted) |
| | | if (_scheduler.IsShutdown && _scheduler.IsStarted) |
| | | { |
| | | // 从Factory中获取Scheduler实例 |
| | | NameValueCollection collection = new NameValueCollection |
| | | { |
| | | { "quartz.serializer.type", "binary" }, |
| | | }; |
| | | //StdSchedulerFactory factory = new StdSchedulerFactory(collection); |
| | | StdSchedulerFactory factory = new StdSchedulerFactory(); |
| | | _scheduler = factory.GetScheduler().Result; |
| | | } |
| | | |
| | | this._scheduler.JobFactory = this._iocjobFactory; |
| | | |
| | | if (!this._scheduler.IsStarted) |
| | | { |
| | | //等待任务运行完成 |
| | | await this._scheduler.Result.Start(); |
| | | await this._scheduler.Start(); |
| | | await Console.Out.WriteLineAsync(QuartzJobInfoMessage.StartJobSuccess); |
| | | result = WebResponseContent.Instance.OK(QuartzJobInfoMessage.StartJobSuccess); |
| | | return result; |
| | | } |
| | | else |
| | | { |
| | | await _scheduler.Shutdown(); |
| | | result = WebResponseContent.Instance.Error(QuartzJobInfoMessage.JobHasStart); |
| | | return result; |
| | | } |
| | |
| | | WebResponseContent result = new WebResponseContent(); |
| | | try |
| | | { |
| | | if (!this._scheduler.Result.IsShutdown) |
| | | if (!_scheduler.IsShutdown) |
| | | { |
| | | //等待任务运行完成 |
| | | await this._scheduler.Result.Shutdown(); |
| | | await _scheduler.Shutdown(false); |
| | | |
| | | await Console.Out.WriteLineAsync(QuartzJobInfoMessage.StopJobSuccess); |
| | | result = WebResponseContent.Instance.OK(QuartzJobInfoMessage.StopJobSuccess); |
| | | return result; |
| | | } |
| | | else |
| | | { |
| | | IReadOnlyCollection<string> jobGroupNames = await _scheduler.GetJobGroupNames(); |
| | | |
| | | await _scheduler.PauseAll(); |
| | | |
| | | result = WebResponseContent.Instance.Error(QuartzJobInfoMessage.JobHasStop); |
| | | return result; |
| | | } |
| | |
| | | { |
| | | try |
| | | { |
| | | if (_scheduler.IsShutdown && _scheduler.IsStarted) |
| | | { |
| | | // 从Factory中获取Scheduler实例 |
| | | NameValueCollection collection = new NameValueCollection |
| | | { |
| | | { "quartz.serializer.type", "binary" }, |
| | | }; |
| | | //StdSchedulerFactory factory = new StdSchedulerFactory(collection); |
| | | StdSchedulerFactory factory = new StdSchedulerFactory(); |
| | | _scheduler = factory.GetScheduler().Result; |
| | | } |
| | | |
| | | |
| | | JobKey jobKey = new JobKey(tasksQz.Id.ToString(), tasksQz.JobGroup); |
| | | if (await _scheduler.Result.CheckExists(jobKey)) |
| | | if (await _scheduler.CheckExists(jobKey)) |
| | | { |
| | | result = WebResponseContent.Instance.Error(string.Format(QuartzJobInfoMessage.JobHasAdd, tasksQz.Name)); |
| | | return result; |
| | |
| | | { |
| | | tasksQz.EndTime = DateTime.MaxValue.AddDays(-1); |
| | | } |
| | | DateTimeOffset endRunTime = DateBuilder.NextGivenSecondDate(tasksQz.EndTime, 1);//设置暂停时间 |
| | | |
| | | #endregion |
| | | |
| | |
| | | } |
| | | |
| | | Assembly assembly = Assembly.Load(new AssemblyName(tasksQz.AssemblyName)); |
| | | Type jobType = assembly.GetType(tasksQz.AssemblyName + "." + tasksQz.ClassName); |
| | | Type jobType = assembly.GetType(tasksQz.AssemblyName + "." + tasksQz.ClassName) ?? throw new Exception($"未找到程序集里面该类型,【{tasksQz.AssemblyName}. {tasksQz.ClassName}】"); |
| | | |
| | | #endregion |
| | | |
| | | //判断任务调度是否开启 |
| | | if (!_scheduler.IsStarted) |
| | | { |
| | | await StartScheduleAsync(); |
| | | } |
| | | |
| | | //传入反射出来的执行程序集 |
| | | IJobDetail job = new JobDetailImpl(tasksQz.Id.ToString(), tasksQz.JobGroup, jobType); |
| | |
| | | ITrigger trigger = CreateSimpleTrigger(tasksQz); |
| | | |
| | | // 告诉Quartz使用我们的触发器来安排作业 |
| | | await _scheduler.Result.ScheduleJob(job, trigger); |
| | | await _scheduler.ScheduleJob(job, trigger); |
| | | |
| | | result = WebResponseContent.Instance.OK(string.Format(QuartzJobInfoMessage.JobAddSuccess, tasksQz.Name)); |
| | | return result; |
| | |
| | | public async Task<bool> IsExistScheduleJobAsync(DispatchInfoDTO sysSchedule) |
| | | { |
| | | JobKey jobKey = new JobKey(sysSchedule.Id.ToString(), sysSchedule.JobGroup); |
| | | if (await _scheduler.Result.CheckExists(jobKey)) |
| | | if (await _scheduler.CheckExists(jobKey)) |
| | | { |
| | | return true; |
| | | } |
| | |
| | | try |
| | | { |
| | | JobKey jobKey = new JobKey(sysSchedule.Id.ToString(), sysSchedule.JobGroup); |
| | | if (!await _scheduler.Result.CheckExists(jobKey)) |
| | | if (!await _scheduler.CheckExists(jobKey)) |
| | | { |
| | | result = WebResponseContent.Instance.Error(string.Format(QuartzJobInfoMessage.JobNotExist, sysSchedule.Name)); |
| | | return result; |
| | | } |
| | | else |
| | | { |
| | | await this._scheduler.Result.DeleteJob(jobKey); |
| | | await this._scheduler.DeleteJob(jobKey); |
| | | result = WebResponseContent.Instance.OK(string.Format(QuartzJobInfoMessage.StopAJobSuccess, sysSchedule.Name)); |
| | | return result; |
| | | } |
| | |
| | | try |
| | | { |
| | | JobKey jobKey = new JobKey(sysSchedule.Id.ToString(), sysSchedule.JobGroup); |
| | | if (!await _scheduler.Result.CheckExists(jobKey)) |
| | | if (!await _scheduler.CheckExists(jobKey)) |
| | | { |
| | | result = WebResponseContent.Instance.Error(string.Format(QuartzJobInfoMessage.ResumeJobNotExist, sysSchedule.Name)); |
| | | return result; |
| | | } |
| | | await this._scheduler.Result.ResumeJob(jobKey); |
| | | await this._scheduler.ResumeJob(jobKey); |
| | | result = WebResponseContent.Instance.OK(string.Format(QuartzJobInfoMessage.ResumeJobSuccess, sysSchedule.Name)); |
| | | return result; |
| | | } |
| | |
| | | throw new QuartzJobException(string.Format(QuartzJobExceptionMessage.ResumeJobException, sysSchedule.Name, ex.Message), innerException: ex); |
| | | } |
| | | } |
| | | |
| | | //public async Task<WebResponseContent> PauseAllJobAsync() |
| | | //{ |
| | | // WebResponseContent result = new WebResponseContent(); |
| | | // if (_scheduler.IsStarted && !_scheduler.IsShutdown) |
| | | // { |
| | | // await _scheduler.PauseAll(); |
| | | // } |
| | | // return WebResponseContent.Instance.OK(); |
| | | //} |
| | | |
| | | //public async Task<WebResponseContent> ResumeAllJobAsync() |
| | | //{ |
| | | // WebResponseContent result = new WebResponseContent(); |
| | | // if (_scheduler.IsStarted && !_scheduler.IsShutdown) |
| | | // await _scheduler.ResumeAll(); |
| | | // return WebResponseContent.Instance.OK(); |
| | | //} |
| | | |
| | | /// <summary> |
| | | /// 暂停指定的计划任务 |
| | |
| | | try |
| | | { |
| | | JobKey jobKey = new JobKey(sysSchedule.Id.ToString(), sysSchedule.JobGroup); |
| | | if (!await _scheduler.Result.CheckExists(jobKey)) |
| | | if (!await _scheduler.CheckExists(jobKey)) |
| | | { |
| | | result = WebResponseContent.Instance.Error(string.Format(QuartzJobInfoMessage.PauseJobNotExist, sysSchedule.Name)); |
| | | return result; |
| | | } |
| | | await this._scheduler.Result.PauseJob(jobKey); |
| | | await this._scheduler.PauseJob(jobKey); |
| | | result = WebResponseContent.Instance.OK(string.Format(QuartzJobInfoMessage.PauseJobSuccess, sysSchedule.Name)); |
| | | return result; |
| | | } |
| | |
| | | JobKey jobKey = new JobKey(tasksQz.Id.ToString(), tasksQz.JobGroup); |
| | | |
| | | //判断任务是否存在,存在则 触发一次,不存在则先添加一个任务,触发以后再 停止任务 |
| | | if (!await _scheduler.Result.CheckExists(jobKey)) |
| | | if (!await _scheduler.CheckExists(jobKey)) |
| | | { |
| | | //不存在 则 添加一个计划任务 |
| | | await AddScheduleJobAsync(tasksQz); |
| | | |
| | | //触发执行一次 |
| | | await _scheduler.Result.TriggerJob(jobKey); |
| | | await _scheduler.TriggerJob(jobKey); |
| | | |
| | | //停止任务 |
| | | await StopScheduleJobAsync(tasksQz); |
| | |
| | | } |
| | | else |
| | | { |
| | | await _scheduler.Result.TriggerJob(jobKey); |
| | | await _scheduler.TriggerJob(jobKey); |
| | | result = WebResponseContent.Instance.OK(string.Format(QuartzJobInfoMessage.ExecuteJobSuccess, tasksQz.Name)); |
| | | } |
| | | } |