Skip to content

Instantly share code, notes, and snippets.

@bgokoglu
Last active February 18, 2022 03:50
Show Gist options
  • Select an option

  • Save bgokoglu/4738aad7665742308cde4ff7d227c5a6 to your computer and use it in GitHub Desktop.

Select an option

Save bgokoglu/4738aad7665742308cde4ff7d227c5a6 to your computer and use it in GitHub Desktop.
Configure Hangfire
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