Skip to content

Instantly share code, notes, and snippets.

@Alexei000
Created August 29, 2019 09:37
Show Gist options
  • Save Alexei000/22b4ca0e88e86ac5a0987f2abd538efa to your computer and use it in GitHub Desktop.
Save Alexei000/22b4ca0e88e86ac5a0987f2abd538efa to your computer and use it in GitHub Desktop.
Startup configuration for Hangfire
using Hangfire;
using Hangfire.SqlServer;
/// <summary>
/// specifies if hangfire job run or not
/// </summary>
protected bool HangFireEnabled => Configuration.GetSection("GeneralApplicationSettings").GetValue<bool>("HangFireEnabled");
/// <summary>
/// configures Hangfire functionality (async jobs)
/// </summary>
/// <param name="services"></param>
protected virtual void ConfigureHangfire(IServiceCollection services)
{
bool prepare = !Configuration.GetSection("GeneralApplicationSettings").GetValue<bool>("HangFireSkipPrepareSchemaIfNecessary");
services.AddHangfire(opt => opt.UseSqlServerStorage(Configuration.GetConnectionString("Default"),
new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
// SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.FromSeconds(30),
UseRecommendedIsolationLevel = true,
UsePageLocksOnDequeue = true,
DisableGlobalLocks = true,
//TODO: run when deployed only (do not run on dev to reduce run times)
PrepareSchemaIfNecessary = prepare
}));
}
/// <summary>
/// configures Hangfire functionality (async jobs)
/// </summary>
/// <param name="services"></param>
protected virtual void ConfigureHangfire(IServiceCollection services)
{
bool prepare = !Configuration.GetSection("GeneralApplicationSettings").GetValue<bool>("HangFireSkipPrepareSchemaIfNecessary");
services.AddHangfire(opt => opt.UseSqlServerStorage(Configuration.GetConnectionString("Default"),
new SqlServerStorageOptions
{
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
// SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
QueuePollInterval = TimeSpan.FromSeconds(30),
UseRecommendedIsolationLevel = true,
UsePageLocksOnDequeue = true,
DisableGlobalLocks = true,
//TODO: run when deployed only (do not run on dev to reduce run times)
PrepareSchemaIfNecessary = prepare
}));
}
// ReSharper disable once UnusedMember.Global
public void ConfigureServices(IServiceCollection services)
{
ConfigureGeneralServices(services);
ConfigureCustomServices(services);
AddDbContext(services);
AddCors(services);
if (HangFireEnabled)
ConfigureHangfire(services);
}
/// <summary>
/// schedules hangfire jobs
/// </summary>
/// <param name="app"></param>
/// <param name="serviceProvider"></param>
protected virtual void StartHangFireJobs(IApplicationBuilder app, IServiceProvider serviceProvider)
{
app.UseHangfireServer();
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
Authorization = new [] { new HangfireDashboardAuthorizationFilter() },
StatsPollingInterval = 30000,
// no back link
AppPath = null
});
RecurringJob.AddOrUpdate<SyncUserStatusJob>(nameof(SyncUserStatusJob),
x => x.DoWork(),
"0 0 17 * * *");
// TODO: this can be defined, but the issue is that it runs once at startup
// commented out as not start, but can be manually triggered
//RecurringJob.AddOrUpdate<DataSyncService>(nameof(DataSyncService),
// x => x.SyncAllData(),
// "0 0 0 31 FEB *");
}
// ReSharper disable once UnusedMember.Global
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory,
IApplicationLifetime lifetime,
ILoggingService logger,
IConfigurationService configurationService,
IServiceProvider serviceProvider,
IHostingEnvironment hostingEnvironment)
{
ConfigureLogging(loggerFactory);
ConfigureExceptionPage(app, env);
if (HangFireEnabled)
StartHangFireJobs(app, serviceProvider);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment