FluentScheduler
FluentScheduler copied to clipboard
Support for DI?
Hi,
According to the documentation there is no support for DI.
https://fluentscheduler.github.io/dependency-injection/
Any advice on what should be done here?
This is what I've done for D.I. (excuse the formatting, I copied and pasted from Github which messed it up):
JobManager.JobStart += JobManager_JobStart;
JobManager.JobEnd += JobManager_JobEnd;
JobManager.JobException += JobManager_JobException;
JobManager.UseUtcTime();
JobManager.Initialize(new JobRegistry(serviceProvider));
using System;
using Autofac;
using Autofac.Extensions.DependencyInjection;
using FluentScheduler;
using Jobs;
public class JobRegistry : Registry
{
public JobRegistry(IServiceProvider serviceProvider)
{
//Prevent concurrent jobs
NonReentrantAsDefault();
// Schedule PerformanceMonitor once per hour
Schedule(() => {
var rootScope = serviceProvider.GetAutofacRoot();
// Note, if you call 'return nestedScope.Resolve<PerformanceMonitor>();' then FluentScheduler will call Execute, but it won't Dispose any objects. Hence the using statement below and I'm manually calling Execute on the IJob implementation.
using(var nestedScope = rootScope.BeginLifetimeScope()) {
nestedScope.Resolve<PerformanceMonitor>().Execute();
}
}).ToRunNow().AndEvery(60).Minutes();
}}
I'm using IMediatr for DI. Pretty standard setup as far as that is concerned. Then I use Mediatr calls dispatched on a schedule as I outlined in a post over here:
NonReentrantAsDefault();
@t-ashraf why set this? cant we run parallel when its DI?
@phoenixcoded20 - You can run in parallel when it's DI. This is just something we do because pretty much every one of our jobs uses our database and do not wish to see any of these: https://learn.microsoft.com/en-us/dotnet/api/system.data.entity.infrastructure.dbupdateconcurrencyexception?view=entity-framework-6.2.0
:)