Last active
September 18, 2021 20:19
-
-
Save seangwright/84159025f6ca8d009397a6596f08ed3a to your computer and use it in GitHub Desktop.
.NET 6 Console App w/ global usings
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// See https://docs.microsoft.com/en-us/dotnet/core/extensions/dependency-injection-usage | |
await Host | |
.CreateDefaultBuilder(args) | |
.ConfigureServices((context, services) => | |
services | |
.AddLogging() | |
.AddQuartz(quartz => | |
{ | |
quartz.SchedulerId = "Quartz-App"; | |
quartz.SchedulerName = "Quartz-App"; | |
quartz.UseMicrosoftDependencyInjectionJobFactory(); | |
quartz.ScheduleJob<HelloJob>(trigger => | |
{ | |
trigger.WithIdentity("trigger1", "group1") | |
.StartNow() | |
.UsingJobData(new JobDataMap()) | |
.WithSimpleSchedule(x => x | |
.WithIntervalInSeconds(2) | |
.WithRepeatCount(5)) | |
.WithDescription("Hello job trigger"); | |
}); | |
quartz.AddSchedulerListener<SchedulerListener>(); | |
quartz.AddJobListener<JobListener>(); | |
}) | |
.AddQuartzHostedService(options => | |
{ | |
options.WaitForJobsToComplete = true; | |
})) | |
.RunConsoleAsync(); | |
public class HelloJob : IJob | |
{ | |
public async Task Execute(IJobExecutionContext context) | |
{ | |
await Console.Out.WriteLineAsync("Greetings from HelloJob!"); | |
context.Result = "Success!"; | |
} | |
} | |
public class SchedulerListener : SchedulerListenerSupport | |
{ | |
private readonly ILogger<SchedulerListener> logger; | |
private readonly ISchedulerFactory factory; | |
public SchedulerListener(ILogger<SchedulerListener> logger, ISchedulerFactory factory) | |
{ | |
this.factory = factory; | |
this.logger = logger; | |
} | |
public override async Task TriggerFinalized(ITrigger trigger, CancellationToken cancellationToken = default) | |
{ | |
await Log(trigger, cancellationToken); | |
await base.TriggerFinalized(trigger, cancellationToken); | |
} | |
private async Task Log(ITrigger trigger, CancellationToken cancellationToken) | |
{ | |
var scheduler = await factory.GetScheduler("Quartz-App", cancellationToken); | |
if (scheduler is null) | |
{ | |
return; | |
} | |
var jobDetail = await scheduler.GetJobDetail(trigger.JobKey, cancellationToken); | |
if (jobDetail is null) | |
{ | |
return; | |
} | |
logger.LogInformation( | |
"Trigger {trigger} {description} for Job {job} finished", | |
trigger.Key, | |
trigger.Description, | |
jobDetail.Key); | |
} | |
} | |
public class JobListener : JobListenerSupport | |
{ | |
private readonly ILogger<JobListener> logger; | |
public override string Name => nameof(JobListener); | |
public JobListener(ILogger<JobListener> logger) | |
{ | |
this.logger = logger; | |
} | |
public override Task JobWasExecuted(IJobExecutionContext context, JobExecutionException? jobException, CancellationToken cancellationToken = default) | |
{ | |
logger.LogInformation("{Job} completed with {result}", context.JobDetail.Key, context.Result); | |
return base.JobWasExecuted(context, jobException, cancellationToken); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<Project Sdk="Microsoft.NET.Sdk"> | |
<PropertyGroup> | |
<OutputType>Exe</OutputType> | |
<TargetFramework>net6.0</TargetFramework> | |
<ImplicitUsings>enable</ImplicitUsings> | |
<Nullable>enable</Nullable> | |
</PropertyGroup> | |
<ItemGroup> | |
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.2" /> | |
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" /> | |
<PackageReference Include="Quartz" Version="3.3.3" /> | |
<PackageReference Include="Quartz.Extensions.DependencyInjection" Version="3.3.3" /> | |
<PackageReference Include="Quartz.Extensions.Hosting" Version="3.3.3" /> | |
</ItemGroup> | |
<ItemGroup> | |
<Using Include="Microsoft.Extensions.DependencyInjection" /> | |
<Using Include="Microsoft.Extensions.Hosting" /> | |
<Using Include="Microsoft.Extensions.Logging" /> | |
<Using Include="Quartz" /> | |
<Using Include="Quartz.Listener" /> | |
</ItemGroup> | |
</Project> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment