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

AspNetCore extensions #28

Open
Softtinn opened this issue Mar 15, 2021 · 2 comments
Open

AspNetCore extensions #28

Softtinn opened this issue Mar 15, 2021 · 2 comments

Comments

@Softtinn
Copy link

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
Copy link
Author

            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?

@bugproof
Copy link

bugproof commented Mar 29, 2021

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 quartznet/quartznet#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

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