Esempio n. 1
0
        public static IApplicationBuilder UseHangfireServer(
            [NotNull] this IApplicationBuilder app,
            [CanBeNull] BackgroundJobServerOptions options = null,
            [CanBeNull] IEnumerable <IBackgroundProcess> additionalProcesses = null,
            [CanBeNull] JobStorage storage = null)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            HangfireServiceCollectionExtensions.ThrowIfNotConfigured(app.ApplicationServices);

            var services = app.ApplicationServices;
            var lifetime = services.GetRequiredService <IApplicationLifetime>();

            storage             = storage ?? services.GetRequiredService <JobStorage>();
            options             = options ?? services.GetService <BackgroundJobServerOptions>() ?? new BackgroundJobServerOptions();
            additionalProcesses = additionalProcesses ?? services.GetServices <IBackgroundProcess>();

            options.Activator        = options.Activator ?? services.GetService <JobActivator>();
            options.FilterProvider   = options.FilterProvider ?? services.GetService <IJobFilterProvider>();
            options.TimeZoneResolver = options.TimeZoneResolver ?? services.GetService <ITimeZoneResolver>();

            var server = HangfireServiceCollectionExtensions.GetInternalServices(services, out var factory, out var stateChanger, out var performer)
#pragma warning disable 618
                ? new BackgroundJobServer(options, storage, additionalProcesses, null, null, factory, performer, stateChanger)
#pragma warning restore 618
                : new BackgroundJobServer(options, storage, additionalProcesses);

            lifetime.ApplicationStopping.Register(() => server.SendStop());
            lifetime.ApplicationStopped.Register(() => server.Dispose());

            return(app);
        }
        public static IApplicationBuilder UseHangfireDashboard(
            [NotNull] this IApplicationBuilder app,
            [NotNull] string pathMatch           = "/hangfire",
            [CanBeNull] DashboardOptions options = null,
            [CanBeNull] JobStorage storage       = null)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }
            if (pathMatch == null)
            {
                throw new ArgumentNullException(nameof(pathMatch));
            }

            HangfireServiceCollectionExtensions.ThrowIfNotConfigured(app.ApplicationServices);

            var services = app.ApplicationServices;

            storage = storage ?? services.GetRequiredService <JobStorage>();
            options = options ?? services.GetService <DashboardOptions>() ?? new DashboardOptions();
            options.TimeZoneResolver = options.TimeZoneResolver ?? services.GetService <ITimeZoneResolver>();

            var routes = app.ApplicationServices.GetRequiredService <RouteCollection>();

            app.Map(new PathString(pathMatch), x => x.UseMiddleware <AspNetCoreDashboardMiddleware>(storage, options, routes));

            return(app);
        }
Esempio n. 3
0
        public static IEndpointConventionBuilder MapHangfireDashboard(
            [NotNull] this IEndpointRouteBuilder endpoints,
            [NotNull] string pattern,
            [CanBeNull] DashboardOptions options = null,
            [CanBeNull] JobStorage storage       = null)
        {
            if (endpoints == null)
            {
                throw new ArgumentNullException(nameof(endpoints));
            }
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }

            var app = endpoints.CreateApplicationBuilder();

            HangfireServiceCollectionExtensions.ThrowIfNotConfigured(app.ApplicationServices);

            var services = app.ApplicationServices;

            storage = storage ?? services.GetRequiredService <JobStorage>();
            options = options ?? services.GetService <DashboardOptions>() ?? new DashboardOptions();
            options.TimeZoneResolver = options.TimeZoneResolver ?? services.GetService <ITimeZoneResolver>();

            var routes = app.ApplicationServices.GetRequiredService <Dashboard.RouteCollection>();

            var pipeline = app
                           .UsePathBase(pattern)
                           .UseMiddleware <AspNetCoreDashboardMiddleware>(storage, options, routes)
                           .Build();

            return(endpoints.Map(pattern + "/{**path}", pipeline));
        }
Esempio n. 4
0
        public IBackgroundJobClient GetClient(JobStorage storage)
        {
            if (HangfireServiceCollectionExtensions.GetInternalServices(_serviceProvider, out var factory, out var stateChanger, out _))
            {
                return(new BackgroundJobClient(storage, factory, stateChanger));
            }

            return(new BackgroundJobClient(
                       storage,
                       _serviceProvider.GetRequiredService <IJobFilterProvider>()));
        }
Esempio n. 5
0
        public IRecurringJobManager GetManager(JobStorage storage)
        {
            if (HangfireServiceCollectionExtensions.GetInternalServices(_serviceProvider, out var factory, out _, out _))
            {
                return(new RecurringJobManager(
                           storage,
                           factory,
                           _serviceProvider.GetRequiredService <ITimeZoneResolver>()));
            }

            return(new RecurringJobManager(
                       storage,
                       _serviceProvider.GetRequiredService <IJobFilterProvider>(),
                       _serviceProvider.GetRequiredService <ITimeZoneResolver>()));
        }