Beispiel #1
0
        public static IServiceCollection BackgroundJobsConfiguration(this IServiceCollection services, IConfiguration configuration)
        {
            var hangfireSettings = new HangfireSettings()
            {
                ConnectionString = configuration.GetValue <string>("Hangfire:ConnectionString"),
                Interval         = configuration.GetValue <string>("Hangfire:Interval"),
            };

            services.AddSingleton(hangfireSettings);

            services.AddScoped <IBackgroundJob, FillFilterCacheBackgroundJob>();
            services.AddScoped <FillFilterCacheBackgroundJob>();

            services.AddHangfire(conf => conf
                                 .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
                                 .UseSimpleAssemblyNameTypeSerializer()
                                 .UseRecommendedSerializerSettings()
                                 .UseSqlServerStorage(hangfireSettings.ConnectionString, new SqlServerStorageOptions
            {
                CommandBatchMaxTimeout       = TimeSpan.FromMinutes(5),
                SlidingInvisibilityTimeout   = TimeSpan.FromMinutes(5),
                QueuePollInterval            = TimeSpan.Zero,
                UseRecommendedIsolationLevel = true,
                UsePageLocksOnDequeue        = true,
                DisableGlobalLocks           = true,
            }));

            // Add the processing server as IHostedService
            services.AddHangfireServer();

            return(services);
        }
Beispiel #2
0
        private static void AddRecurringJobs(HangfireSettings hangfireSettings)
        {
            RecurringJob.RemoveIfExists(nameof(FillFilterCacheBackgroundJob));
            RecurringJob.AddOrUpdate <FillFilterCacheBackgroundJob>(
                nameof(FillFilterCacheBackgroundJob),
                job => job.Execute(),
                hangfireSettings.Interval);

            RecurringJob.Trigger(nameof(FillFilterCacheBackgroundJob));
        }