97 lines
4.2 KiB
C#
97 lines
4.2 KiB
C#
|
|
using Ease.Core.DataAccess;
|
||
|
|
using Hangfire;
|
||
|
|
using Hangfire.Dashboard;
|
||
|
|
using Hangfire.SqlServer;
|
||
|
|
using HRM.BO;
|
||
|
|
using HRM.BO.Configuration;
|
||
|
|
using Microsoft.AspNetCore.Builder;
|
||
|
|
using Microsoft.AspNetCore.Hosting;
|
||
|
|
using Microsoft.Extensions.Configuration;
|
||
|
|
using Microsoft.Extensions.DependencyInjection;
|
||
|
|
using Microsoft.Extensions.Hosting;
|
||
|
|
using Microsoft.Extensions.Options;
|
||
|
|
using System;
|
||
|
|
using System.Data.Common;
|
||
|
|
|
||
|
|
namespace HRM.UI.BackgroundJobs.Calendar
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Hangfire set-up for the calendar (reminders and notifications), kept out of Startup.
|
||
|
|
/// Hangfire keeps its own tables in the "HangFire" schema of the same database and creates them on first start.
|
||
|
|
/// </summary>
|
||
|
|
public static class CalendarHangfireSetup
|
||
|
|
{
|
||
|
|
/// <summary>
|
||
|
|
/// Registers Calendar settings, Hangfire storage (same database as the app) and one Hangfire server.
|
||
|
|
/// In the Development environment calendar e-mails go out only when Calendar:RedirectAllMailTo is set, so a
|
||
|
|
/// developer PC never mails real people.
|
||
|
|
/// </summary>
|
||
|
|
public static IServiceCollection AddCalendarBackgroundJobs(this IServiceCollection services, IConfiguration configuration,
|
||
|
|
IWebHostEnvironment environment)
|
||
|
|
{
|
||
|
|
services.Configure<CalendarSettings>(configuration.GetSection("Calendar"));
|
||
|
|
if (environment.IsDevelopment())
|
||
|
|
{
|
||
|
|
services.PostConfigure<CalendarSettings>(settings =>
|
||
|
|
{
|
||
|
|
if (string.IsNullOrWhiteSpace(settings.RedirectAllMailTo))
|
||
|
|
settings.SendMail = false;
|
||
|
|
});
|
||
|
|
}
|
||
|
|
|
||
|
|
services.AddHangfire(config => config
|
||
|
|
.SetDataCompatibilityLevel(CompatibilityLevel.Version_180)
|
||
|
|
.UseSimpleAssemblyNameTypeSerializer()
|
||
|
|
.UseRecommendedSerializerSettings()
|
||
|
|
// Ease.Core's factory gives a new connection with the (decrypted) app connection string.
|
||
|
|
.UseSqlServerStorage(
|
||
|
|
() => (DbConnection)ConnectionFactory.Default.CreateConnection(),
|
||
|
|
new SqlServerStorageOptions
|
||
|
|
{
|
||
|
|
SchemaName = "HangFire",
|
||
|
|
PrepareSchemaIfNecessary = true
|
||
|
|
}));
|
||
|
|
|
||
|
|
services.AddHangfireServer(options =>
|
||
|
|
{
|
||
|
|
options.WorkerCount = 4;
|
||
|
|
options.Queues = new[] { CalendarJobs.QueueName, "default" };
|
||
|
|
});
|
||
|
|
|
||
|
|
services.AddTransient<CalendarJobs>();
|
||
|
|
return services;
|
||
|
|
}
|
||
|
|
|
||
|
|
/// <summary>
|
||
|
|
/// Dashboard at /hangfire (only from the server itself) and the per-minute dispatcher (reminders, schedule jobs and
|
||
|
|
/// schedule emails).
|
||
|
|
/// Must run before UseSpa, which would otherwise answer /hangfire with the Angular page.
|
||
|
|
/// </summary>
|
||
|
|
public static IApplicationBuilder UseCalendarBackgroundJobs(this IApplicationBuilder app)
|
||
|
|
{
|
||
|
|
app.UseHangfireDashboard("/hangfire", new DashboardOptions
|
||
|
|
{
|
||
|
|
Authorization = new IDashboardAuthorizationFilter[] { new LocalRequestsOnlyAuthorizationFilter() }
|
||
|
|
});
|
||
|
|
|
||
|
|
CalendarSettings settings = app.ApplicationServices.GetRequiredService<IOptions<CalendarSettings>>().Value;
|
||
|
|
IRecurringJobManager recurringJobs = app.ApplicationServices.GetRequiredService<IRecurringJobManager>();
|
||
|
|
// one per-minute dispatcher serves reminders, schedule-job runs and schedule-email runs
|
||
|
|
if (settings.RemindersEnabled || settings.ScheduleJobsEnabled || settings.ScheduleEmailsEnabled)
|
||
|
|
{
|
||
|
|
recurringJobs.AddOrUpdate<CalendarJobs>(CalendarJobs.DispatcherJobId, j => j.Dispatch(), Cron.Minutely(),
|
||
|
|
new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
recurringJobs.RemoveIfExists(CalendarJobs.DispatcherJobId);
|
||
|
|
}
|
||
|
|
|
||
|
|
// the daily birthday digest is retired (everyone sees only their own birthday): drop it where it was set up
|
||
|
|
recurringJobs.RemoveIfExists(CalendarJobs.BirthdayDigestJobId);
|
||
|
|
|
||
|
|
return app;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|