Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
markjerz opened this issue Mar 22, 2024 · 1 comment
Open

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

markjerz opened this issue Mar 22, 2024 · 1 comment

Comments

@markjerz
Copy link

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....

@ricky6982
Copy link

@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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants