From 70233af5426b0d1c343ebe87183303a34a9aaa58 Mon Sep 17 00:00:00 2001
From: hutongqing <hutongqing@hnkhzn.com>
Date: 星期三, 20 十一月 2024 09:05:38 +0800
Subject: [PATCH] WIDESEAWCS_QuartzJob更新版本

---
 WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/SchedulerCenterServer.cs |  104 ++++++++++++++++++++++++++++++++++++++++------------
 1 files changed, 80 insertions(+), 24 deletions(-)

diff --git a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/SchedulerCenterServer.cs b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/SchedulerCenterServer.cs
index febec47..a176c07 100644
--- a/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/SchedulerCenterServer.cs
+++ b/WIDESEAWCS_Server/WIDESEAWCS_QuartzJob/QuartzNet/SchedulerCenterServer.cs
@@ -30,19 +30,20 @@
 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;
@@ -55,14 +56,14 @@
                     {
                         { "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);
                 }
-
             }
         }
 
@@ -75,17 +76,31 @@
             WebResponseContent result = new WebResponseContent();
             try
             {
-                this._scheduler.Result.JobFactory = this._iocjobFactory;
-                if (!this._scheduler.Result.IsStarted)
+                if (_scheduler.IsShutdown && _scheduler.IsStarted)
+                {
+                    // 浠嶧actory涓幏鍙朣cheduler瀹炰緥
+                    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;
                 }
@@ -105,16 +120,21 @@
             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;
                 }
@@ -139,8 +159,21 @@
             {
                 try
                 {
+                    if (_scheduler.IsShutdown && _scheduler.IsStarted)
+                    {
+                        // 浠嶧actory涓幏鍙朣cheduler瀹炰緥
+                        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;
@@ -156,7 +189,6 @@
                     {
                         tasksQz.EndTime = DateTime.MaxValue.AddDays(-1);
                     }
-                    DateTimeOffset endRunTime = DateBuilder.NextGivenSecondDate(tasksQz.EndTime, 1);//璁剧疆鏆傚仠鏃堕棿
 
                     #endregion
 
@@ -173,9 +205,15 @@
                     }
 
                     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);
@@ -184,7 +222,7 @@
                     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;
@@ -208,7 +246,7 @@
         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;
             }
@@ -228,14 +266,14 @@
             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;
                 }
@@ -257,12 +295,12 @@
             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;
             }
@@ -271,6 +309,24 @@
                 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>
         /// 鏆傚仠鎸囧畾鐨勮鍒掍换鍔�
@@ -283,12 +339,12 @@
             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;
             }
@@ -422,13 +478,13 @@
                 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);
@@ -437,7 +493,7 @@
                 }
                 else
                 {
-                    await _scheduler.Result.TriggerJob(jobKey);
+                    await _scheduler.TriggerJob(jobKey);
                     result = WebResponseContent.Instance.OK(string.Format(QuartzJobInfoMessage.ExecuteJobSuccess, tasksQz.Name));
                 }
             }

--
Gitblit v1.9.3