alphacloud / Autofac.Extras.Quartz

Autofac integration for Quartz.Net
MIT License
121 stars 38 forks source link

Multitenancy support #243

Closed junaidahmed92 closed 1 year ago

junaidahmed92 commented 1 year ago

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

We are trying to use Quartz in a multitenant application. I found this issue where there was already a request for running separate tenant aware instances of IScheduler. I also tried overriding QuartzAutofacFactoryModule and using InstancePerTenant as the guy did in the pasted issue but I am getting TenantLifetimeScope errors. So do you have any idea around that? This is how I tried it:

                 builder.Register(c =>
                    {
                        var ctx = c.Resolve<IComponentContext>();
                        var lifetimeScope = ctx.Resolve<ILifetimeScope>();
                        return new AutofacJobFactory(lifetimeScope, _lifetimeScopeTag, JobScopeConfigurator);
                    })
                    .AsSelf()
                    .As<IJobFactory>()
                    .InstancePerTenant();

                builder.Register<ISchedulerFactory>(c => {
                    var cfgProvider = ConfigurationProvider;
                    var ctx = c.Resolve<IComponentContext>();
                    var autofacSchedulerFactory = cfgProvider != null
                        ? new AutofacSchedulerFactory(cfgProvider(ctx), ctx.Resolve<AutofacJobFactory>())
                        : new AutofacSchedulerFactory(c.Resolve<AutofacJobFactory>());
                    return autofacSchedulerFactory;
                })
                    .InstancePerTenant();

                builder.Register(c => {
                        var ctx = c.Resolve<IComponentContext>();
                    var factory = ctx.Resolve<ISchedulerFactory>();
                    return factory.GetScheduler().ConfigureAwait(false).GetAwaiter().GetResult();
                })
                    .As<IScheduler>()
                    .InstancePerTenant();

Describe the solution you'd like

It would be great if I get something that is tenant aware so that we could rely on Autofac for Quartz jobs registration. Do you have any suggestions for that?

cd21h commented 1 year ago

Sorry for late response. This does not look like Quartz issue, I recommend to check with Autofac community on multi-tenancy registration