ravendb / quartznet-RavenDB

RavenDB JobStore support for Quartz.NET scheduler.
Apache License 2.0
27 stars 19 forks source link

Work hand in hand withRavenDB.DependencyInjection #19

Closed cadilhac closed 2 years ago

cadilhac commented 2 years ago

My asp.net core project already uses RavenDB.DependencyInjection, which creates a DocumentStore that is available to controllers via DI. Your library also creates a store. It would be great if it could use an injected store already created by another service. Otherwise, there are 2 stores and 2 configs to create them. In other words, how can I pass an existing IDocumentStore to this library?

ayende commented 2 years ago

You can now use it like so:

builder.Services.AddQuartz(q =>
{
    // Use a Scoped container to create jobs.
    q.UseMicrosoftDependencyInjectionJobFactory();

    q.UsePersistentStore(storeOptions =>
    {
        storeOptions.UseRavenDbWithInjectedDocumentStore();

        storeOptions.UseJsonSerializer();
    });

    // Comment out these lines and uncomment app.Lifetime.ApplicationStarted.Register(OnStarted); to make it work
    q.AddJob<SampleJob>(j => j.WithIdentity("SampleJob"));
    q.AddTrigger(t => t
        .WithIdentity("SampleTrigger")
        .ForJob("SampleJob")
        .WithSchedule(CronScheduleBuilder.DailyAtHourAndMinute(4, 0).InTimeZone(TimeZoneInfo.Utc))
    );
});

builder.Services.AddSingleton<IDocumentStore>(sp =>
{
    var docStore = new DocumentStore
    {
        Urls = new[] { "http://localhost:8080" },
        Database = "QuartzTest"
    };
    docStore.Initialize();
    return docStore;
});
cadilhac commented 2 years ago

@ayende Does not work. I get:

Quartz.SchedulerException: 'JobStore of type 'Quartz.Impl.RavenDB.RavenJobStoreWithInjectDocumentStore' could not be instantiated.'
Inner exception:
ArgumentException: Cannot instantiate type which has no empty constructor (Parameter 'RavenJobStoreWithInjectDocumentStore')

Seems like there is no constructor dependency injection for stores...

cadilhac commented 2 years ago

@ayende Any fix for this DI issue?

ayende commented 2 years ago

Haven't dropped this, still looking into this, but finding time to actually devote to it is tough.

cadilhac commented 2 years ago

Mentioned the issue on the quartz.net github page.

I think reopening this issue here would be good too.

ayende commented 2 years ago

See here: https://github.com/quartznet/quartznet/pull/1630