Pixsys.Library.ScheduledTasks.Quartz 1.2.0

dotnet add package Pixsys.Library.ScheduledTasks.Quartz --version 1.2.0
                    
NuGet\Install-Package Pixsys.Library.ScheduledTasks.Quartz -Version 1.2.0
                    
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="Pixsys.Library.ScheduledTasks.Quartz" Version="1.2.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pixsys.Library.ScheduledTasks.Quartz" Version="1.2.0" />
                    
Directory.Packages.props
<PackageReference Include="Pixsys.Library.ScheduledTasks.Quartz" />
                    
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 Pixsys.Library.ScheduledTasks.Quartz --version 1.2.0
                    
#r "nuget: Pixsys.Library.ScheduledTasks.Quartz, 1.2.0"
                    
#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 Pixsys.Library.ScheduledTasks.Quartz@1.2.0
                    
#: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=Pixsys.Library.ScheduledTasks.Quartz&version=1.2.0
                    
Install as a Cake Addin
#tool nuget:?package=Pixsys.Library.ScheduledTasks.Quartz&version=1.2.0
                    
Install as a Cake Tool

Pixsys.Library.ScheduledTasks.Quartz

A set of helpers to create one time job and recurrent jobs using Quartz Scheduler.

1. Installation

1.1 Register the service in Program.cs

using Pixsys.Library.ScheduledTasks.Quartz;

WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
builder.Services.AddQuartzScheduler();

If you have recurrent jobs, you can also register them. This function will look for classes inheriting RecurringJob located in the provided assembly and schedule jobs if triggers are found (manual or in the appsettings). Just remember to make the Main method async

public static async Task Main(string[] args)
{
...
    var app = builder.Build();

    await app.ScheduleRecurringJobsAsync(typeof(Program).Assembly);
}

2. Usage

You can then create your one-time job :

using Pixsys.Library.ScheduledTasks.Quartz;
using Pixsys.Library.ScheduledTasks.Quartz.Base;
using Quartz;
using Pixsys.Library.ScheduledTasks.Quartz.Extensions;

public class HelloJob(ILogger<BaseJob> logger, ISchedulerFactory schedulerFactory, IServiceProvider serviceProvider) : OneTimeJob(logger, schedulerFactory)
{
    protected override JobKey Key => new JobKey("hello", "global");

    public override async Task ExecuteTask(IJobExecutionContext context)
    {
        logger.LogJobInformation("Hello World!");
        // Testing job exception
        throw new JobExecutionException("Error while executing the job");
        await Task.CompletedTask;
    }

    public override List<IJobListener> GetJobListeners()
    {
        return [ActivatorUtilities.CreateInstance<HelloJobListener>(serviceProvider)];
    }
}

A job listener can be attached to the job

using Pixsys.Library.ScheduledTasks.Quartz.Base;
using Pixsys.Library.ScheduledTasks.Quartz.Extensions;
using Quartz;

public class HelloJobListener(ILogger<BaseJob> logger) : BaseJobListener(logger)
{
    private readonly ILogger<BaseJob> logger = logger;

    public override async Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default)
    {
        this.logger.LogJobInformation("job will be executed");
        await Task.CompletedTask;
    }

    public override async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException? jobException, CancellationToken cancellationToken = default)
    {
        if (jobException != null)
        {
            this.logger.LogJobInformation("A job exception has been raised");
        }
        this.logger.LogJobInformation("job was executed");
        await Task.CompletedTask;
    }
}

Here is an example of a recurring job, where you can add additional manual triggers

using Pixsys.Library.ScheduledTasks.Quartz;
using Pixsys.Library.ScheduledTasks.Quartz.Base;
using Pixsys.Library.ScheduledTasks.Quartz.Extensions;
using Quartz;

[DisallowConcurrentExecution]
public class RecurringHello(ILogger<BaseJob> logger, ISchedulerFactory schedulerFactory, IConfiguration config, IServiceProvider serviceProvider) : RecurringJob(logger, schedulerFactory, config)
{
    protected override JobKey Key => new JobKey("hello", "recurring");

    public override Task ExecuteTask(IJobExecutionContext context)
    {
        //var var1 = context.MergedJobDataMap.GetIntValue("var1");
        //var var2 = context.MergedJobDataMap.GetString("var2");

        logger.LogJobInformation($"Hello world! {DateTime.Now.ToShortTimeString()}");
        return Task.CompletedTask;
    }

    public override List<IJobListener> GetJobListeners()
    {
        return [ActivatorUtilities.CreateInstance<HelloJobListener>(serviceProvider)];
    }

    protected override List<ITrigger> GetManualTriggers()
    {
        var trigger = TriggerBuilder.Create()
       .ForJob(this.Key)
       .StartNow()
       .Build();

        return new List<ITrigger> { trigger };
    }
}

You can also register your crontab schedules for your recurrent jobs in appsettings.json

  "ScheduledTasks": {
    "Quartz": {
      "job group": {
        "job name": [ "0 0/1 * 1/1 * ? *" ]
      }
    }
  }     
Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.

Version Downloads Last Updated
1.2.0 272 1/23/2025
1.1.0 156 1/22/2025
1.0.1 298 1/3/2025
1.0.0 214 11/28/2024