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
<PackageReference Include="Pixsys.Library.ScheduledTasks.Quartz" Version="1.2.0" />
<PackageVersion Include="Pixsys.Library.ScheduledTasks.Quartz" Version="1.2.0" />
<PackageReference Include="Pixsys.Library.ScheduledTasks.Quartz" />
paket add Pixsys.Library.ScheduledTasks.Quartz --version 1.2.0
#r "nuget: Pixsys.Library.ScheduledTasks.Quartz, 1.2.0"
#:package Pixsys.Library.ScheduledTasks.Quartz@1.2.0
#addin nuget:?package=Pixsys.Library.ScheduledTasks.Quartz&version=1.2.0
#tool nuget:?package=Pixsys.Library.ScheduledTasks.Quartz&version=1.2.0
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 | Versions 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. |
-
net8.0
- Quartz.Extensions.DependencyInjection (>= 3.13.1)
- Quartz.Extensions.Hosting (>= 3.13.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.