Lycoris.Quartz.Extensions 6.0.3

There is a newer version of this package available.
See the version list below for details.
dotnet add package Lycoris.Quartz.Extensions --version 6.0.3
                    
NuGet\Install-Package Lycoris.Quartz.Extensions -Version 6.0.3
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Lycoris.Quartz.Extensions" Version="6.0.3" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Lycoris.Quartz.Extensions" Version="6.0.3" />
                    
Directory.Packages.props
<PackageReference Include="Lycoris.Quartz.Extensions" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Lycoris.Quartz.Extensions --version 6.0.3
                    
#r "nuget: Lycoris.Quartz.Extensions, 6.0.3"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Lycoris.Quartz.Extensions@6.0.3
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Lycoris.Quartz.Extensions&version=6.0.3
                    
Install as a Cake Addin
#tool nuget:?package=Lycoris.Quartz.Extensions&version=6.0.3
                    
Install as a Cake Tool

Quartz简单封装,开箱即用,使用方便

基于Quartz做了一层简单封装,支持Scoped生命周期直接在构造函数注入,简化了使用成本。

一、注册Quartz调度中心

builder.Services.AddQuartzSchedulerCenter();

二、创建调度任务

创建调度任务的两种方式

基类会根据设置的任务运行截至事件自动停止任务,并且基类中也做了异常捕捉,除了你业务中必要的业务捕捉要,其他未知异常基类都能帮你及时记录

1. 继承扩展的基类

基类中包含以下两个可读属性

  • JobTraceId:当前执行的任务TraceId
  • JobName:当前执行的任务名称
[QuartzJob("测试任务", Trigger = QuartzTriggerEnum.SIMPLE, IntervalSecond = 5)]
public class TestJob : BaseQuartzJob
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    protected override Task DoWork(IJobExecutionContext context)
    {
        Console.WriteLine($"TestJob => {DateTime.Now:yyyy-MM-dd HH:mm:ss}");
        return Task.CompletedTask;
    }
}

创建好调度任务后,还需要设置任务的配置,设置很简单,在任务类上加上特性[QuartzJob("测试任务", Trigger = QuartzTriggerEnum.SIMPLE, IntervalSecond = 5)]

QuartzJobAttribute使用指南:

  • 构造函数入参为定时任务名称
  • Trigger为触发器类型,分为普通定时器SIMPLE和Cron定时器CRON
  • IntervalSecond:定时秒数,该配置仅对普通定时器有效
  • Cron:如果是Corn定时器,需要配置Cron表达式
  • RunTimes:执行次数,默认是无线循环

  • 2. 继承来自QuartzIJob接口
public class TestJob : IJob
{
    public Task Execute(IJobExecutionContext context)
    {
        return Task.CompletedTask;
    }
}

三、注册调度任务

注册调度任务(以下ServiceBuilder类为演示自行编写的扩展类部分,不包含在当前扩展类库之内,仅做使用示例使用)

public static class ServiceBuilder
{
    // 推荐使用
    public static void AddQuartzJobBuilder(this IServiceCollection services)
    {
        services.AddQuartzJob<TestJob>()
                .AddQuartzJob<TestJob2>()
                .QuartzJobBuild();
    }

    // 推荐使用
    public static void AddQuartzJobBuilder(this IServiceCollection services)
    {
        services.AddQuartzJob(typeof(TestJob), typeof(TestJob2)).QuartzJobBuild();
    }

    // 请优先使用前两种注册方式
    public static void AddQuartzJobBuilder(this IServiceCollection services)
    {
        services.AddQuartzJobAssembly(MethodBase.GetCurrentMethod().ReflectedType.Assembly).QuartzJobBuild();
    }
}

注册任务的另一种方式:注册调度中心时同时注册调度任务

 builder.Services.AddQuartzSchedulerCenter(buider =>
 {
     buider.AddJob<TestJob>();
     buider.AddJob<TestJob2>();
 });

四、调度任务也支持原生的两个特性

  • PersistJobDataAfterExecutionAttribute: 这一次的结果作为值传给下一次
[PersistJobDataAfterExecution]
public class TestJob : BaseQuartzJob
{
    public TestJob(ILycorisLoggerFactory factory) : base(factory.CreateLogger<TestJob>())
    {

    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="context"></param>
    /// <returns></returns>
    protected override Task DoWork(IJobExecutionContext context)
    {
        // 取出的是上一次保存的值
        var val = context.GetJobDataMap("Key");

        _logger.Info($"TestJob => {DateTime.Now:yyyy-MM-dd HH:mm:ss}");

        // 保存这一次的值
        context.AddJobDataMap("Key", "这一次的传值");

        return Task.CompletedTask;
    }
}
  • DisallowConcurrentExecutionAttribute: 只有上一个任务完成才会执行下一次任务
    [DisallowConcurrentExecution]
    public class TestJob : BaseQuartzJob
    

五、数据库支持

扩展不做数据库支持,但是Quartz原生带有各种监听服务,需要使用到数据库做数据持久化的,请自行开发,可实现的接口如下:

  • ISchedulerListener:调度器执行监听
  • ITriggerListener:调度器执行监听
  • IJobListener:调度任务执行监听

由于原生的接口有些有很多方法需要实现,如果想偷懒的小伙伴可以继承我处理好的基类:

  • SchedulerListener
  • TriggerListener
  • JobListener

仅需重写你需要使用到的监听方法即可

注册监听

 builder.Services.AddQuartzSchedulerCenter(buider =>
 {
     buider.AddSchedulerListener<CustomeSchedulerListener>();
     buider.AddTriggerListener<CustomeTriggerListener>();
     buider.AddJobListener<CustomeJobListener>();
 });

三、自定义日志记录

  • ILycorisLoggerFactory:扩展的自定义日志工厂,在开发的时候,使用别人的扩展,但是由于自己的日志需要按格式,ES才能进行切割关键词分片等,很多不支持,所以自己开发的时候额外增加了这部分。
  • 注意:使用自定义日志工厂时候,还需要自己实现ILycorisLogger来配合日志工厂实现自定义日志功能
builder.Services.AddQuartzSchedulerCenter(builder =>
{
    // 替换扩展中默认的日志工厂
    builder.AddLycorisLoggerFactory<CustomeLoggerFactory>();
});

PS:如果你使用了多个Lycoris系列扩展,那你可以在注册这些扩展之前使用builder.Serovces.AddLycorisLoggerFactory<CustomeLoggerFactory>()进行替换,就不需要在每个扩展中使用AddLycorisLoggerFactory进行逐一替换了

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.