quartznet icon indicating copy to clipboard operation
quartznet copied to clipboard

Need to access resolved IOptions<T> during scheduling.

Open AnthonyMastrean opened this issue 4 years ago • 3 comments

Is your feature request related to a problem? Please describe.

We are trying to allow application administrators to configure some of the scheduling properties of certain "maintenance" jobs. We were hoping to use the normal configuration file sources (the whole IConfiguration setup) and, better, specific IOptions<T>, resolved during scheduling. However, these are not available as part of the configure signature.

public void ConfigureServices(IServiceCollection services)
{
    services.AddQuartz(options =>
    {
        options.ScheduleJob<ExampleJob>(trigger => trigger
            .WithIdentity(typeof(ExampleJob).FullName)
            .StartNow()
            .WithCronSchedule(???) // TODO: pull this from configuration/options
        );
    });
}

Describe the solution you'd like Various parts of the standard library have signatures that allow resolving TDep1, TDep2, ..., TDepN as part of the configure signature. This might be nice:

public void ConfigureServices(IServiceCollection services)
{
    services.AddQuartz(options =>
    {
        options.ScheduleJob<ExampleJob, IOptions<ExampleJobOptions>>((trigger, options) => trigger
            .WithIdentity(typeof(ExampleJob).FullName)
            .StartNow()
            .WithCronSchedule(options.Value.CronExpression)
        );
    });
}

Describe alternatives you've considered I'm open to other options... I'm not sure I've thought this through all the way 😆

AnthonyMastrean avatar Sep 30 '21 14:09 AnthonyMastrean

I think that you can achieve this already by using AddOptions that's basically a dependency chain of options. From the example:

https://github.com/quartznet/quartznet/blob/09b33a20ef5e1caf710fa07ac83a17bcfb0ed7bc/src/Quartz.Examples.AspNetCore/Startup.cs#L218-L231

lahma avatar Sep 30 '21 15:09 lahma

Seems like I'd still use the new AddQuartz call to get things started... but then I'd manipulate QuartzOptions directly? Looks like I'd be losing access to the super-convenient ScheduleJob method 😢

Of course, that ScheduleJob extension method is doing something similar!

I wonder if I could do something useful if I had access to the IServiceCollection Services private member... 🤔 (I've seen this on various aspnetcore builder types)

AnthonyMastrean avatar Sep 30 '21 15:09 AnthonyMastrean

If you can figure out how to refactor the ScheduleJob logic it could be extension against QuartzOptions too? Might be worth a PR 😉

lahma avatar Sep 30 '21 15:09 lahma

Closing due to no activity, happy to reopen if someone wants to build something.

lahma avatar Apr 11 '23 17:04 lahma