Last active
February 18, 2022 03:50
-
-
Save bgokoglu/4738aad7665742308cde4ff7d227c5a6 to your computer and use it in GitHub Desktop.
Configure Hangfire
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static class HangfireConfig | |
| { | |
| public static void AddHangfire(this IServiceCollection services, IConfiguration configuration) | |
| { | |
| Log.Information("Configuring hangfire..."); | |
| services.AddScoped<IBackgroundJobWorker, HangfireBackgroundJobManager>(); | |
| services.AddHangfire(config => config | |
| .SetDataCompatibilityLevel(CompatibilityLevel.Version_170) | |
| .UseSimpleAssemblyNameTypeSerializer() | |
| .UseRecommendedSerializerSettings() | |
| .UseSqlServerStorage(configuration["ConnectionStrings:Default"], new SqlServerStorageOptions | |
| { | |
| CommandBatchMaxTimeout = TimeSpan.FromMinutes(5), | |
| SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5), | |
| QueuePollInterval = TimeSpan.Zero, | |
| UseRecommendedIsolationLevel = true, | |
| DisableGlobalLocks = true | |
| }) | |
| .UseFilter(new HangfireProlongJobExpirationAttribute()) | |
| .UseFilter(new HangfireLogFailureAttribute()) | |
| .UseConsole()); | |
| services.AddHangfireServer(options => | |
| { | |
| options.ServerName = $"{Environment.MachineName}:AdminServer"; | |
| options.WorkerCount = Math.Min(Environment.ProcessorCount * 5, 20); | |
| options.Queues = new[] { Environment.MachineName.ToLower(), "default" }; | |
| options.SchedulePollingInterval = TimeSpan.FromMinutes(1); | |
| options.CancellationCheckInterval = TimeSpan.FromMinutes(1); | |
| }); | |
| } | |
| public static void UseHangfireDashboard(this IApplicationBuilder app) | |
| { | |
| app.UseHangfireDashboard("/admin/hangfire", new DashboardOptions | |
| { | |
| AppPath = "/admin/home", | |
| Authorization = new[] { new HangfireAuthorizationFilter() }, | |
| StatsPollingInterval = 15000 | |
| }, JobStorage.Current); | |
| InitializeRecurringJobs(); | |
| } | |
| public static void InitializeRecurringJobs() | |
| { | |
| using (var connection = JobStorage.Current.GetConnection()) | |
| { | |
| foreach (var recurringJob in connection.GetRecurringJobs()) | |
| { | |
| RecurringJob.RemoveIfExists(recurringJob.Id); | |
| } | |
| } | |
| RecurringJob.AddOrUpdate<GeneralTask>("Keep_Alive", h => h.KeepAlive(1, null), "*/20 * * * *"); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment