mongodb-quartz-net icon indicating copy to clipboard operation
mongodb-quartz-net copied to clipboard

AspNetCore extensions

Open Softtinn opened this issue 3 years ago • 2 comments

Hi!

Is it possible to use this together with the Quartz.Extensions.DependencyInjection?

I was thinking of something like this:

                q.UsePersistentStore(ps =>
                {
                    ps.UseMongoDb(connectionString: MongoDbConfig.ConnectionString, prefix: "quartz");
                });

How would the extensionmethod UseMongoDb look like?

Softtinn avatar Mar 15 '21 09:03 Softtinn

            services.AddQuartz(q =>
            {
                q.UseMicrosoftDependencyInjectionScopedJobFactory(options =>
                {
                    // if we don't have the job in DI, allow fallback to configure via default constructor
                    options.AllowDefaultConstructor = true;
                });

                q.UseSimpleTypeLoader();
                q.UseInMemoryStore();

                q.UseDefaultThreadPool(tp =>
                {
                    tp.MaxConcurrency = 5;
                });
            });

            // ASP.NET Core hosting
            services.AddQuartzServer(options =>
            {
                // when shutting down we want jobs to complete gracefully
                options.WaitForJobsToComplete = true;
            });

            var properties = new NameValueCollection();
            properties["quartz.scheduler.instanceId"] = $"{Environment.MachineName}-{Guid.NewGuid()}";
            properties["quartz.jobStore.type"] = typeof(MongoDbJobStore).AssemblyQualifiedName;
            properties["quartz.jobStore.collectionPrefix"] = "quartz_local";
            properties["quartz.jobStore.connectionString"] = MongoDbConfig.ConnectionString;
            properties["quartz.serializer.type"] = "json";

I've done as the code example before. Is this approach bad?

Softtinn avatar Mar 15 '21 14:03 Softtinn

I think you would do it like this:

public static class QuartzConfiguratorExtensions
{
    // Or you can just use this SchedulerBuilder.PersistentStoreOptions if you really want to use it 
    // inside UsePersistentStore
    public static void UseMongoDbStore(this IServiceCollectionQuartzConfigurator cfg, string connectionString, string collectionPrefix)
    {
        cfg.SetProperty(StdSchedulerFactory.PropertyJobStoreType, typeof (MongoDbJobStore).AssemblyQualifiedName!);
        cfg.SetProperty(
            $"{StdSchedulerFactory.PropertyJobStorePrefix}.{StdSchedulerFactory.PropertyDataSourceConnectionString}",
            connectionString);
        // optional prefix
        cfg.SetProperty($"{StdSchedulerFactory.PropertyJobStorePrefix}.collectionPrefix", collectionPrefix);
    }
}

// Using it
services.AddQuartz(cfg =>
{
    // This library will create it's own MongoClient instance inside
    cfg.UseMongoDbStore("mongodb://localhost/quartz", collectionPrefix: "quartz");
});
services.AddQuartzHostedService();

I'm not sure if trying to integrate it with UsePersistentStore would bring any benefits. It's really just a wrapper for setting different properties... https://github.com/quartznet/quartznet/blob/2fccbc1a17c3fc1bd6f7433f8aedc2c541a1fa46/src/Quartz/SchedulerBuilder.cs#L187 I think it was more meant for relational databases and not document databases see https://github.com/quartznet/quartznet/issues/988

Refactor persistence API to be more document database friendly, #814 probably contributes to this

I'm currently not using Quartz.NET but Hangfire, as I like to test and compare different libraries and Hangfire, has a slightly easier to use API but it can be more limited.

Azure functions can also be a good choice but I think can be more costly.

Refer to the documentation for property descriptions https://www.quartz-scheduler.net/documentation/quartz-3.x/configuration/reference.html

bugproof avatar Mar 29 '21 12:03 bugproof