Add - Quartz

master
wenjy 1 year ago
parent b37d649e28
commit d955885f92

@ -0,0 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="7.0.0" />
<PackageReference Include="Quartz.AspNetCore" Version="3.7.0" />
</ItemGroup>
</Project>

@ -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<Job2> _logger;
public Job2(ILogger<Job2> logger)
{
_logger = logger;
}
public Task Execute(IJobExecutionContext context)
{
_logger.LogInformation($"执行Job2{DateTime.Now.ToString("HH:mm:ss")}");
return Task.CompletedTask;
}
}
}

@ -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<MyJob> _logger;
public MyJob(ILogger<MyJob> logger)
{
_logger = logger;
}
public Task Execute(IJobExecutionContext context)
{
_logger.LogInformation($"执行MyJob{DateTime.Now.ToString("HH:mm:ss")}");
return Task.CompletedTask;
}
}
}

@ -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<MyJob>(trigger =>
trigger.WithCronSchedule("*/3 * * * * ?").WithIdentity("MyJob","MyJobGroup") // 示例每3s执行一次
);
q.ScheduleJob<Job2>(trigger =>
trigger.WithCronSchedule("*/5 * * * * ?").WithIdentity("Job2", "Job2Group") // 示例每5s执行一次
);
});
services.AddQuartzHostedService(options => options.WaitForJobsToComplete = true);
services.AddSingleton<IScheduler>(provider=>provider.GetRequiredService<ISchedulerFactory>().GetScheduler().Result);
}
}
}

@ -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

@ -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<BaseUserController> logger, IBaseUserService service)
{
_logger = logger;

@ -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
{
/// <summary>
/// 任务调度
/// </summary>
[ApiController]
[Route("api/[controller]")]
public class JobController
{
private readonly IScheduler _scheduler;
public JobController(IScheduler scheduler)
{
_scheduler = scheduler;
}
[HttpPost("start")]
public async Task<string> 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<string> StopJob(string jobName, string groupName)
{
var myJobKey = new JobKey(jobName, groupName);
await _scheduler.PauseJob(myJobKey);
return "Job stopped successfully.";
}
}
}

@ -16,6 +16,7 @@
<ItemGroup>
<ProjectReference Include="..\Durkee.Mes.Api.Config\Durkee.Mes.Api.Config.csproj" />
<ProjectReference Include="..\Durkee.Mes.Api.Quartz\Durkee.Mes.Api.Quartz.csproj" />
<ProjectReference Include="..\Durkee.Mes.Api.Repository\Durkee.Mes.Api.Repository.csproj" />
</ItemGroup>

@ -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.

Loading…
Cancel
Save