From d955885f929cbfdee535ffe213eb5d7b97db22a3 Mon Sep 17 00:00:00 2001 From: wenjy Date: Fri, 27 Oct 2023 16:41:45 +0800 Subject: [PATCH] Add - Quartz --- .../Durkee.Mes.Api.Quartz.csproj | 12 +++++ Durkee.Mes.Api.Quartz/Job/Job2.cs | 25 ++++++++++ Durkee.Mes.Api.Quartz/Job/MyJob.cs | 26 ++++++++++ Durkee.Mes.Api.Quartz/QuartzSetUp.cs | 36 ++++++++++++++ Durkee.Mes.Api.sln | 14 ++++-- .../Controllers/BaseUserController.cs | 5 +- Durkee.Mes.Api/Controllers/JobController.cs | 48 +++++++++++++++++++ Durkee.Mes.Api/Durkee.Mes.Api.csproj | 1 + Durkee.Mes.Api/Startup.cs | 5 ++ 9 files changed, 167 insertions(+), 5 deletions(-) create mode 100644 Durkee.Mes.Api.Quartz/Durkee.Mes.Api.Quartz.csproj create mode 100644 Durkee.Mes.Api.Quartz/Job/Job2.cs create mode 100644 Durkee.Mes.Api.Quartz/Job/MyJob.cs create mode 100644 Durkee.Mes.Api.Quartz/QuartzSetUp.cs create mode 100644 Durkee.Mes.Api/Controllers/JobController.cs diff --git a/Durkee.Mes.Api.Quartz/Durkee.Mes.Api.Quartz.csproj b/Durkee.Mes.Api.Quartz/Durkee.Mes.Api.Quartz.csproj new file mode 100644 index 0000000..1a9fd7a --- /dev/null +++ b/Durkee.Mes.Api.Quartz/Durkee.Mes.Api.Quartz.csproj @@ -0,0 +1,12 @@ + + + + netcoreapp3.1 + + + + + + + + diff --git a/Durkee.Mes.Api.Quartz/Job/Job2.cs b/Durkee.Mes.Api.Quartz/Job/Job2.cs new file mode 100644 index 0000000..85962c1 --- /dev/null +++ b/Durkee.Mes.Api.Quartz/Job/Job2.cs @@ -0,0 +1,25 @@ +using Microsoft.Extensions.Logging; +using Quartz; +using System; +using System.Collections.Generic; +using System.Text; +using System.Threading.Tasks; + +namespace Durkee.Mes.Api.Quartz.Job +{ + internal class Job2 : IJob + { + private readonly ILogger _logger; + + public Job2(ILogger logger) + { + _logger = logger; + } + + public Task Execute(IJobExecutionContext context) + { + _logger.LogInformation($"执行Job2:{DateTime.Now.ToString("HH:mm:ss")}"); + return Task.CompletedTask; + } + } +} diff --git a/Durkee.Mes.Api.Quartz/Job/MyJob.cs b/Durkee.Mes.Api.Quartz/Job/MyJob.cs new file mode 100644 index 0000000..2a5fd09 --- /dev/null +++ b/Durkee.Mes.Api.Quartz/Job/MyJob.cs @@ -0,0 +1,26 @@ +using Microsoft.Extensions.Hosting; +using Microsoft.Extensions.Logging; +using Quartz; +using Quartz.Logging; +using System; +using System.Threading; +using System.Threading.Tasks; + +namespace Durkee.Mes.Api.Quartz.Job +{ + public class MyJob : IJob + { + private readonly ILogger _logger; + + public MyJob(ILogger logger) + { + _logger = logger; + } + + public Task Execute(IJobExecutionContext context) + { + _logger.LogInformation($"执行MyJob:{DateTime.Now.ToString("HH:mm:ss")}"); + return Task.CompletedTask; + } + } +} diff --git a/Durkee.Mes.Api.Quartz/QuartzSetUp.cs b/Durkee.Mes.Api.Quartz/QuartzSetUp.cs new file mode 100644 index 0000000..78f8c3a --- /dev/null +++ b/Durkee.Mes.Api.Quartz/QuartzSetUp.cs @@ -0,0 +1,36 @@ +using Durkee.Mes.Api.Quartz.Job; +using Microsoft.Extensions.DependencyInjection; +using Quartz; +using Quartz.Impl; +using Quartz.Spi; +using System; +using System.Collections.Generic; +using System.Text; + +namespace Durkee.Mes.Api.Quartz +{ + public static class QuartzSetUp + { + [Obsolete] + public static void AddQuartzSetUp(this IServiceCollection services) + { + services.AddQuartz(q => + { + q.UseMicrosoftDependencyInjectionJobFactory(); + + q.ScheduleJob(trigger => + trigger.WithCronSchedule("*/3 * * * * ?").WithIdentity("MyJob","MyJobGroup") // 示例:每3s执行一次 + ); + + q.ScheduleJob(trigger => + trigger.WithCronSchedule("*/5 * * * * ?").WithIdentity("Job2", "Job2Group") // 示例:每5s执行一次 + ); + }); + + services.AddQuartzHostedService(options => options.WaitForJobsToComplete = true); + + services.AddSingleton(provider=>provider.GetRequiredService().GetScheduler().Result); + + } + } +} diff --git a/Durkee.Mes.Api.sln b/Durkee.Mes.Api.sln index 3fb184f..e5acf60 100644 --- a/Durkee.Mes.Api.sln +++ b/Durkee.Mes.Api.sln @@ -3,13 +3,15 @@ Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio Version 17 VisualStudioVersion = 17.5.33502.453 MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Durkee.Mes.Api", "Durkee.Mes.Api\Durkee.Mes.Api.csproj", "{30BF9893-8860-4267-BDDC-C1D220DF702C}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Durkee.Mes.Api", "Durkee.Mes.Api\Durkee.Mes.Api.csproj", "{30BF9893-8860-4267-BDDC-C1D220DF702C}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Durkee.Mes.Api.Config", "Durkee.Mes.Api.Config\Durkee.Mes.Api.Config.csproj", "{10B17840-AFCA-4547-AC77-B0F9B2B89C2A}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Durkee.Mes.Api.Config", "Durkee.Mes.Api.Config\Durkee.Mes.Api.Config.csproj", "{10B17840-AFCA-4547-AC77-B0F9B2B89C2A}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Durkee.Mes.Api.Repository", "Durkee.Mes.Api.Repository\Durkee.Mes.Api.Repository.csproj", "{A6857806-816A-40A3-AFEC-F05F876899BF}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Durkee.Mes.Api.Repository", "Durkee.Mes.Api.Repository\Durkee.Mes.Api.Repository.csproj", "{A6857806-816A-40A3-AFEC-F05F876899BF}" EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Durkee.Mes.Api.Model", "Durkee.Mes.Api.Model\Durkee.Mes.Api.Model.csproj", "{28400313-5B3D-412C-94F4-3DCCF5FDAEE3}" +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Durkee.Mes.Api.Model", "Durkee.Mes.Api.Model\Durkee.Mes.Api.Model.csproj", "{28400313-5B3D-412C-94F4-3DCCF5FDAEE3}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Durkee.Mes.Api.Quartz", "Durkee.Mes.Api.Quartz\Durkee.Mes.Api.Quartz.csproj", "{0AC0188D-548A-428C-8FFC-164F53A5607F}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution @@ -33,6 +35,10 @@ Global {28400313-5B3D-412C-94F4-3DCCF5FDAEE3}.Debug|Any CPU.Build.0 = Debug|Any CPU {28400313-5B3D-412C-94F4-3DCCF5FDAEE3}.Release|Any CPU.ActiveCfg = Release|Any CPU {28400313-5B3D-412C-94F4-3DCCF5FDAEE3}.Release|Any CPU.Build.0 = Release|Any CPU + {0AC0188D-548A-428C-8FFC-164F53A5607F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {0AC0188D-548A-428C-8FFC-164F53A5607F}.Debug|Any CPU.Build.0 = Debug|Any CPU + {0AC0188D-548A-428C-8FFC-164F53A5607F}.Release|Any CPU.ActiveCfg = Release|Any CPU + {0AC0188D-548A-428C-8FFC-164F53A5607F}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/Durkee.Mes.Api/Controllers/BaseUserController.cs b/Durkee.Mes.Api/Controllers/BaseUserController.cs index 063bbcc..f1cea39 100644 --- a/Durkee.Mes.Api/Controllers/BaseUserController.cs +++ b/Durkee.Mes.Api/Controllers/BaseUserController.cs @@ -1,4 +1,5 @@ using Durkee.Mes.Api.Model.domain; +using Durkee.Mes.Api.Quartz; using Durkee.Mes.Api.Repository.service; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; @@ -6,6 +7,7 @@ using Microsoft.Extensions.Logging; using SqlSugar; using System; using System.Collections.Generic; +using System.Threading.Tasks; namespace Durkee.Mes.Api.Controllers { @@ -20,6 +22,7 @@ namespace Durkee.Mes.Api.Controllers private readonly IBaseUserService _service; + public BaseUserController(ILogger logger, IBaseUserService service) { _logger = logger; @@ -35,7 +38,7 @@ namespace Durkee.Mes.Api.Controllers { IEnumerable users = null; try - { + { users = _service.GetUsers(); } catch(Exception ex) diff --git a/Durkee.Mes.Api/Controllers/JobController.cs b/Durkee.Mes.Api/Controllers/JobController.cs new file mode 100644 index 0000000..1cb2518 --- /dev/null +++ b/Durkee.Mes.Api/Controllers/JobController.cs @@ -0,0 +1,48 @@ +using Durkee.Mes.Api.Quartz; +using Microsoft.AspNetCore.Mvc; +using Quartz; +using Quartz.Impl.Matchers; +using SqlSugar; +using System.Threading.Tasks; + +namespace Durkee.Mes.Api.Controllers +{ + /// + /// 任务调度 + /// + [ApiController] + [Route("api/[controller]")] + public class JobController + { + private readonly IScheduler _scheduler; + + public JobController(IScheduler scheduler) + { + _scheduler = scheduler; + } + + [HttpPost("start")] + public async Task StartJob(string jobName,string groupName) + { + // 检查调度器是否已经启动 + if (!_scheduler.IsStarted) + { + await _scheduler.Start(); + } + + var myJobKey = new JobKey(jobName, groupName); + await _scheduler.ResumeJob(myJobKey); + + return "Job started successfully."; + } + + [HttpPost("stop")] + public async Task StopJob(string jobName, string groupName) + { + var myJobKey = new JobKey(jobName, groupName); + await _scheduler.PauseJob(myJobKey); + + return "Job stopped successfully."; + } + } +} diff --git a/Durkee.Mes.Api/Durkee.Mes.Api.csproj b/Durkee.Mes.Api/Durkee.Mes.Api.csproj index 212a551..d82bd02 100644 --- a/Durkee.Mes.Api/Durkee.Mes.Api.csproj +++ b/Durkee.Mes.Api/Durkee.Mes.Api.csproj @@ -16,6 +16,7 @@ + diff --git a/Durkee.Mes.Api/Startup.cs b/Durkee.Mes.Api/Startup.cs index 79cbc81..84d58bd 100644 --- a/Durkee.Mes.Api/Startup.cs +++ b/Durkee.Mes.Api/Startup.cs @@ -1,5 +1,6 @@ using Durkee.Mes.Api.Config; using Durkee.Mes.Api.Repository; +using Durkee.Mes.Api.Quartz; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; @@ -25,6 +26,7 @@ namespace Durkee.Mes.Api public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. + [Obsolete] public void ConfigureServices(IServiceCollection services) { services.AddControllers(); @@ -67,6 +69,9 @@ namespace Durkee.Mes.Api //ע services.AddServices(); + + //ע + services.AddQuartzSetUp(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.