Skip to content

Commit 0f8dc8b

Browse files
committed
Allow adding a job without scheduling it
1 parent b3d0dc9 commit 0f8dc8b

File tree

5 files changed

+80
-3
lines changed

5 files changed

+80
-3
lines changed

examples/Job1.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System.Threading.Tasks;
2+
using Quartz;
3+
using Serilog;
4+
5+
namespace Hosting.Extensions.Quartz.Example
6+
{
7+
public class Job1 : IJob
8+
{
9+
public Task Execute(IJobExecutionContext context)
10+
{
11+
Log.Information("Job1");
12+
return Task.CompletedTask;
13+
}
14+
}
15+
}

examples/Program.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
using Microsoft.Extensions.Configuration;
22
using Microsoft.Extensions.Hosting;
33
using Quartz;
4+
using Quartz.Impl.Matchers;
5+
using Quartz.Listener;
46
using Serilog;
57
using System;
68
using System.Threading.Tasks;
@@ -27,6 +29,10 @@ public static async Task Main(string[] args)
2729
job.WithIdentity("LogWriteJob").WithDescription("Simple job");
2830
trigger.StartNow().WithSimpleSchedule((x) => x.WithIntervalInSeconds(2).RepeatForever());
2931
});
32+
services.AddJobService<Job1>((job) =>
33+
{
34+
job.WithIdentity("Job1");
35+
});
3036
})
3137
// You can use the other `UserQuartz` 2 methods
3238
// if you only want to configure either the scheduler factory
@@ -43,6 +49,11 @@ public static async Task Main(string[] args)
4349
// add job listeners, trigger listeners, etc.
4450
// DO NOT call the Start method here as it will be automatically
4551
// invoked by the hosted service once it is started.
52+
// Cria um novo listener para escutar a execução do job2
53+
var listener = new JobChainingJobListener("Chain");
54+
var firstJob = new JobKey("ConsolePrintJob");
55+
listener.AddJobChainLink(firstJob, new JobKey("Job1"));
56+
scheduler.ListenerManager.AddJobListener(listener, KeyMatcher<JobKey>.KeyEquals(firstJob));
4657
}
4758
)
4859
.UseConsoleLifetime()

src/HostBuilderExtensions.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ namespace Hosting.Extensions.Quartz
1010
{
1111
public static class HostBuilderExtensions
1212
{
13+
/// <summary>
14+
/// Registers the Scheduler, JobFactory, QuartzConfigCollection and the QuartzHostedService on the service collection
15+
/// </summary>
16+
/// <returns>The HostBuilder itselft</returns>
17+
public static IHostBuilder UseQuartz(this IHostBuilder builder)
18+
{
19+
return UseQuartz(builder, null, null);
20+
}
21+
1322
/// <summary>
1423
/// Registers the Scheduler, JobFactory, QuartzConfigCollection and the QuartzHostedService on the service collection
1524
/// </summary>

src/QuartzHostedService.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,14 @@ public async Task StartAsync(CancellationToken cancellationToken)
3333
_logger.LogInformation("Scheduling {count} job(s)", _jobs.Count());
3434
foreach (var item in _jobs)
3535
{
36-
await _scheduler.ScheduleJob(item.JobDetail, item.Trigger, cancellationToken);
36+
if (item.Trigger == null)
37+
{
38+
await _scheduler.AddJob(item.JobDetail, true, cancellationToken);
39+
}
40+
else
41+
{
42+
await _scheduler.ScheduleJob(item.JobDetail, item.Trigger, cancellationToken);
43+
}
3744
}
3845
_logger.LogInformation("Starting quartz scheduler");
3946
await _scheduler.Start(cancellationToken);

src/ServiceCollectionExtensions.cs

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ public static class ServiceCollectionExtensions
1313
/// <typeparam name="TImplementation">Service implementation</typeparam>
1414
/// <param name="collection">The NET Core dependency injection service</param>
1515
/// <param name="builder">A function the will receive the JobBuilder and TriggerBuilder to customize the job's behavior</param>
16-
/// <returns></returns>
1716
public static IServiceCollection AddJobService<TService, TImplementation>(this IServiceCollection collection, Action<JobBuilder, TriggerBuilder> builder)
1817
where TService : class
1918
where TImplementation : class, TService, IJob
@@ -28,7 +27,6 @@ public static IServiceCollection AddJobService<TService, TImplementation>(this I
2827
/// </summary>
2928
/// <typeparam name="TService">Service implementation</typeparam>
3029
/// <param name="collection">The NET Core dependency injection service</param>
31-
/// <param name="builder"></param>
3230
/// <param name="builder">A function the will receive the JobBuilder and TriggerBuilder to customize the job's behavior</param>
3331
public static IServiceCollection AddJobService<TService>(this IServiceCollection collection, Action<JobBuilder, TriggerBuilder> builder)
3432
where TService : class, IJob
@@ -38,6 +36,43 @@ public static IServiceCollection AddJobService<TService>(this IServiceCollection
3836
return collection;
3937
}
4038

39+
/// <summary>
40+
/// Adds a job to the scheduler, without scheduling it
41+
/// </summary>
42+
/// <typeparam name="TService">Service implementation</typeparam>
43+
/// <param name="collection">The NET Core dependency injection service</param>
44+
/// <param name="builder">A function the will receive the JobBuilder to customize the job's behavior</param>
45+
public static IServiceCollection AddJobService<TService>(this IServiceCollection collection, Action<JobBuilder> builder)
46+
where TService : class, IJob
47+
{
48+
collection.AddSingleton<TService>();
49+
RegisterJob<TService>(collection, builder);
50+
return collection;
51+
}
52+
53+
/// <summary>
54+
/// Adds a job to the scheduler, without scheduling it
55+
/// </summary>
56+
/// <typeparam name="TService">Abstract service</typeparam>
57+
/// <typeparam name="TImplementation">Service implementation</typeparam>
58+
/// <param name="collection">The NET Core dependency injection service</param>
59+
/// <param name="builder">A function the will receive the JobBuilder to customize the job's behavior</param>
60+
public static IServiceCollection AddJobService<TService, TImplementation>(this IServiceCollection collection, Action<JobBuilder> builder)
61+
where TService : class
62+
where TImplementation : class, TService, IJob
63+
{
64+
collection.AddSingleton<TService, TImplementation>();
65+
RegisterJob<TService>(collection, builder);
66+
return collection;
67+
}
68+
69+
private static void RegisterJob<TService>(IServiceCollection collection, Action<JobBuilder> builder)
70+
{
71+
var jobBuilder = JobBuilder.Create(typeof(TService)).StoreDurably();
72+
builder(jobBuilder);
73+
collection.AddSingleton<IJobSchedule>(new JobSchedule(jobBuilder.Build(), null));
74+
}
75+
4176
private static void RegisterJob<TService>(IServiceCollection collection, Action<JobBuilder, TriggerBuilder> builder) where TService : class
4277
{
4378
var jobBuilder = JobBuilder.Create(typeof(TService));

0 commit comments

Comments
 (0)