FluentScheduler icon indicating copy to clipboard operation
FluentScheduler copied to clipboard

Support for DI?

Open ghost opened this issue 4 years ago • 4 comments

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?

ghost avatar Jan 05 '21 04:01 ghost

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();
    }}

t-ashraf avatar May 25 '21 22:05 t-ashraf

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:

dev-greene avatar Aug 05 '21 19:08 dev-greene

NonReentrantAsDefault();

@t-ashraf why set this? cant we run parallel when its DI?

phoenixcoded20 avatar Jun 01 '23 12:06 phoenixcoded20

@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

:)

t-ashraf avatar Jun 01 '23 13:06 t-ashraf