Created
August 29, 2019 09:06
-
-
Save Alexei000/9316ea11abe95b3c8dc2b56f63a6b4aa to your computer and use it in GitHub Desktop.
This file contains 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
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