IoTSharp/SilkierQuartz

Example of using with existing Quartz setup in asp.net core

Opened this issue · 1 comments

Not an issue so much but possibly useful for other people looking at this library.

I have an existing set up using the main libraries QuartzHostedService. So, my service configuration looks like:

services.AddQuartz(
    q => {
        q.UsePersistentStore(
            x => {
                x.UseProperties = true;
                x.UseSqlServer(
                    sql => {
                        sql.ConnectionString = builder.Configuration.GetConnectionString("foo");
                        sql.TablePrefix      = "[quartz].";
                    });
                x.UseNewtonsoftJsonSerializer();
            });
        q.SetProperty("quartz.plugin.recentHistory.type", "Quartz.Plugins.RecentHistory.ExecutionHistoryPlugin, Quartz.Plugins.RecentHistory");
        q.SetProperty("quartz.plugin.recentHistory.storeType", "Quartz.Plugins.RecentHistory.Impl.InProcExecutionHistoryStore, Quartz.Plugins.RecentHistory");
    });
services.AddQuartzHostedService(options => { options.WaitForJobsToComplete = true; });

Note that I've set up a persistent store (using SQL Server) and I've set up the ExecutionHistoryPlugin as well.

If you use services.AddSilkierQuartz() various bits of the above get overwritten through the use of SilkierQuartz's UseQuartzHostedService() that is inside that call, as well as it overriding the SchedulerFactory. Instead of using that then, I pulled out the service registrations that you need:

services.AddSingleton(new SilkierQuartzOptions { VirtualPathRoot = string.Empty, UseLocalTime = true });
services.AddSingleton(new SilkierQuartzAuthenticationOptions { AccessRequirement = SilkierQuartzAuthenticationOptions.SimpleAccessRequirement.AllowAnonymous });
services.AddAuthorization(
    (Action<AuthorizationOptions>)(opts => opts.AddPolicy(
                                          "SilkierQuartz",
                                          (Action<AuthorizationPolicyBuilder>)(builder => builder.AddRequirements(
                                                                                      new SilkierQuartzDefaultAuthorizationRequirement(
                                                                                          SilkierQuartzAuthenticationOptions.SimpleAccessRequirement.AllowAnonymous))))));
services.AddScoped<IAuthorizationHandler, SilkierQuartzDefaultAuthorizationHandler>();

Then, once the app has been built you can set up your aspnet core alongside setting up SilkierQuartz to use your existing Scheduler:

webApp.UseStaticFiles();
webApp.UseRouting();
webApp.UseAuthentication();
webApp.UseAuthorization();

var scheduler = await webApp.Services.GetRequiredService<ISchedulerFactory>().GetScheduler();
webApp.UseSilkierQuartz(s => { s.Scheduler = scheduler; });

That gets you a pretty functional dashboard with an existing Quartz set up.

NOTE: this is the unauthenticated version

Hopefully useful....

@markjerz excellent post! I'm working with net8 and I already configured many jobs with AddQuartz method and your post was useful! Thanks a lot!