Skip to content

Commit 629be49

Browse files
author
Thiago
authored
Merge pull request #1 from skiptirengu/develop
Allow customization to the scheduler instance
2 parents 7fba68a + b3d0dc9 commit 629be49

File tree

4 files changed

+90
-14
lines changed

4 files changed

+90
-14
lines changed

README.md

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,24 @@ Easily integrate .NET Core's Host with Quartz
66

77
## Usage
88

9-
To enable the Quartz integration, simply call the extension method `UseQuartz()` on your HostBuilder.
9+
Check out the [working example](./examples).
10+
11+
To enable the Quartz integration, simply call one of the three `UseQuartz()` extension methods on your HostBuilder.
1012

1113
```c#
12-
new HostBuilder().UseQuartz((context, config) =>
14+
new HostBuilder().UseQuartz((context, provider, scheduler) =>
1315
{
14-
config.Set("quartz.threadPool.threadCount", "2");
16+
// You can further configure the scheduler instance here, like
17+
// add job listeners, trigger listeners, etc.
18+
// DO NOT call the Start method here as it will be automatically
19+
// invoked by the hosted service once it has started.
20+
scheduler.ListenerManager.AddJobListener(
21+
new JobChainingJobListener("TestListener"), KeyMatcher<JobKey>.KeyEquals(new JobKey("DummyJob"))
22+
);
1523
})
1624
```
1725

18-
The callback is opitional and can be used to customize Quartz default options. You can also set the options in your `appsettings.json` file.
26+
To change Quartz options, add a section to your `appsettings.json` file, or use the `UserQuartz()` method.
1927

2028
```json
2129
{
@@ -29,7 +37,16 @@ The callback is opitional and can be used to customize Quartz default options. Y
2937
}
3038
```
3139

32-
To schedule a new job, call the extension method `AddJobService()` from an `IServiceCollection` instance.
40+
or
41+
42+
```c#
43+
new HostBuilder().UseQuartz((context, config) =>
44+
{
45+
config.Set("quartz.threadPool.threadCount", "2");
46+
})
47+
```
48+
49+
To schedule a new job, call the extension method `AddJobService()` on a `IServiceCollection` instance.
3350

3451
```c#
3552
new HostBuilder().ConfigureServices((services) =>

examples/Program.cs

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,23 @@ public static async Task Main(string[] args)
2828
trigger.StartNow().WithSimpleSchedule((x) => x.WithIntervalInSeconds(2).RepeatForever());
2929
});
3030
})
31-
.UseQuartz((context, config) =>
32-
{
33-
config.Set("quartz.threadPool.threadCount", Environment.ProcessorCount.ToString());
34-
})
31+
// You can use the other `UserQuartz` 2 methods
32+
// if you only want to configure either the scheduler factory
33+
// or the scheduler instance
34+
.UseQuartz(
35+
(context, config) =>
36+
{
37+
// Here you can further customize options passed down to the StdSchedulerFactory
38+
config.Set("quartz.threadPool.threadCount", Environment.ProcessorCount.ToString());
39+
},
40+
(context, provider, scheduler) =>
41+
{
42+
// You can further configure the scheduler instance here, like
43+
// add job listeners, trigger listeners, etc.
44+
// DO NOT call the Start method here as it will be automatically
45+
// invoked by the hosted service once it is started.
46+
}
47+
)
3548
.UseConsoleLifetime()
3649
.UseSerilog()
3750
.Build();

src/HostBuilderExtensions.cs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using Microsoft.Extensions.DependencyInjection;
22
using Microsoft.Extensions.Hosting;
3+
using Quartz;
34
using Quartz.Impl;
45
using Quartz.Spi;
56
using System;
@@ -15,7 +16,37 @@ public static class HostBuilderExtensions
1516
/// <param name="builder">The NET Core HostBuilder</param>
1617
/// <param name="configure">A function that will receive the HostBuilderContext and the StdSchedulerFactory config collection</param>
1718
/// <returns>The HostBuilder itselft</returns>
18-
public static IHostBuilder UseQuartz(this IHostBuilder builder, Action<HostBuilderContext, QuartzConfigCollection> configure = null)
19+
public static IHostBuilder UseQuartz(
20+
this IHostBuilder builder,
21+
Action<HostBuilderContext, QuartzConfigCollection> configure = null)
22+
{
23+
return UseQuartz(builder, configure);
24+
}
25+
26+
/// <summary>
27+
/// Registers the Scheduler, JobFactory, QuartzConfigCollection and the QuartzHostedService on the service collection
28+
/// </summary>
29+
/// <param name="builder">The NET Core HostBuilder</param>
30+
/// <param name="configure">A function that will receive the HostBuilderContext, the ServiceProvider and the IScheduler instance</param>
31+
/// <returns>The HostBuilder itselft</returns>
32+
public static IHostBuilder UseQuartz(
33+
this IHostBuilder builder,
34+
Action<HostBuilderContext, IServiceProvider, IScheduler> configure)
35+
{
36+
return UseQuartz(builder, null, configure);
37+
}
38+
39+
/// <summary>
40+
/// Registers the Scheduler, JobFactory, QuartzConfigCollection and the QuartzHostedService on the service collection
41+
/// </summary>
42+
/// <param name="builder">The NET Core HostBuilder</param>
43+
/// <param name="configureFactory">A function that will receive the HostBuilderContext and the StdSchedulerFactory config collection</param>
44+
/// <param name="configureScheduler">A function that will receive the HostBuilderContext, the ServiceProvider and the IScheduler instance</param>
45+
/// <returns>The HostBuilder itselft</returns>
46+
public static IHostBuilder UseQuartz(
47+
this IHostBuilder builder,
48+
Action<HostBuilderContext, QuartzConfigCollection> configureFactory = null,
49+
Action<HostBuilderContext, IServiceProvider, IScheduler> configureScheduler = null)
1950
{
2051
builder.ConfigureServices((context, collection) =>
2152
{
@@ -26,11 +57,11 @@ public static IHostBuilder UseQuartz(this IHostBuilder builder, Action<HostBuild
2657
collection.AddSingleton<IJobFactory, JobFactory>();
2758
collection.AddSingleton((provider) =>
2859
{
29-
configure?.Invoke(context, config);
60+
configureFactory?.Invoke(context, config);
3061
var factory = new StdSchedulerFactory(config);
31-
3262
var scheduler = factory.GetScheduler().Result;
3363
scheduler.JobFactory = provider.GetRequiredService<IJobFactory>();
64+
configureScheduler?.Invoke(context, provider, scheduler);
3465
return scheduler;
3566
});
3667
});

tests/QuartzTest.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
using Microsoft.Extensions.DependencyInjection;
33
using Microsoft.Extensions.Hosting;
44
using Quartz;
5+
using Quartz.Impl.Matchers;
6+
using Quartz.Listener;
57
using Quartz.Spi;
68
using System.Collections.Generic;
79
using System.Linq;
@@ -22,13 +24,22 @@ public void HostBuilderExtension()
2224

2325
var host = new HostBuilder()
2426
.ConfigureAppConfiguration(x => x.AddInMemoryCollection(dictionary))
25-
.UseQuartz((ctx, c) => c.Set("quartz.threadPool.threadCount", "4"))
27+
.UseQuartz(
28+
(ctx, c) =>
29+
{
30+
c.Set("quartz.threadPool.threadCount", "4");
31+
},
32+
(context, provider, sched) =>
33+
{
34+
sched.ListenerManager.AddJobListener(new JobChainingJobListener("TestListener"), KeyMatcher<JobKey>.KeyEquals(new JobKey("DummyJob")));
35+
}
36+
)
2637
.ConfigureServices((context, services) =>
2738
{
2839
services
2940
.AddJobService<IDummyJob, DummyJob>((job, trigger) =>
3041
{
31-
job.WithDescription("IDummyJob_Job");
42+
job.WithIdentity("DummyJob").WithDescription("IDummyJob_Job");
3243
trigger.WithDescription("IDummyJob_Trigger");
3344
})
3445
.AddJobService<EvenDummierJob>((job, trigger) =>
@@ -58,6 +69,10 @@ public void HostBuilderExtension()
5869
Assert.True(jobs[1].JobDetail.JobType.IsAssignableFrom(typeof(EvenDummierJob)));
5970
Assert.Equal("EvenDummierJob_Job", jobs[1].JobDetail.Description);
6071
Assert.Equal("EvenDummierJob_Trigger", jobs[1].Trigger.Description);
72+
73+
var matchers = scheduler.ListenerManager.GetJobListeners();
74+
Assert.Single(matchers);
75+
Assert.Equal("TestListener", matchers.First().Name);
6176
}
6277
}
6378
}

0 commit comments

Comments
 (0)