|
| 1 | +using Microsoft.AspNetCore.Identity; |
| 2 | +using ServiceStack; |
| 3 | +using ServiceStack.Jobs; |
| 4 | +using MyApp.Data; |
| 5 | +using MyApp.ServiceInterface; |
| 6 | + |
| 7 | +[assembly: HostingStartup(typeof(MyApp.ConfigureBackgroundJobs))] |
| 8 | + |
| 9 | +namespace MyApp; |
| 10 | + |
| 11 | +public class ConfigureBackgroundJobs : IHostingStartup |
| 12 | +{ |
| 13 | + public void Configure(IWebHostBuilder builder) => builder |
| 14 | + .ConfigureServices((context,services) => { |
| 15 | + var smtpConfig = context.Configuration.GetSection(nameof(SmtpConfig))?.Get<SmtpConfig>(); |
| 16 | + if (smtpConfig is not null) |
| 17 | + { |
| 18 | + services.AddSingleton(smtpConfig); |
| 19 | + } |
| 20 | + // Lazily register SendEmailCommand to allow SmtpConfig to only be required if used |
| 21 | + services.AddTransient<SendEmailCommand>(c => new SendEmailCommand( |
| 22 | + c.GetRequiredService<ILogger<SendEmailCommand>>(), |
| 23 | + c.GetRequiredService<IBackgroundJobs>(), |
| 24 | + c.GetRequiredService<SmtpConfig>())); |
| 25 | + |
| 26 | + services.AddPlugin(new CommandsFeature()); |
| 27 | + services.AddPlugin(new BackgroundsJobFeature()); |
| 28 | + services.AddHostedService<JobsHostedService>(); |
| 29 | + }).ConfigureAppHost(afterAppHostInit: appHost => { |
| 30 | + var services = appHost.GetApplicationServices(); |
| 31 | + |
| 32 | + // Log if EmailSender is enabled and SmtpConfig missing |
| 33 | + var log = services.GetRequiredService<ILogger<ConfigureBackgroundJobs>>(); |
| 34 | + var emailSender = services.GetRequiredService<IEmailSender<ApplicationUser>>(); |
| 35 | + if (emailSender is EmailSender) |
| 36 | + { |
| 37 | + var smtpConfig = services.GetService<SmtpConfig>(); |
| 38 | + if (smtpConfig is null) |
| 39 | + { |
| 40 | + log.LogWarning("SMTP is not configured, please configure SMTP to enable sending emails"); |
| 41 | + } |
| 42 | + else |
| 43 | + { |
| 44 | + log.LogWarning("SMTP is configured with <{FromEmail}> {FromName}", smtpConfig.FromEmail, smtpConfig.FromName); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + var jobs = services.GetRequiredService<IBackgroundJobs>(); |
| 49 | + // Example of registering a Recurring Job to run Every Hour |
| 50 | + //jobs.RecurringCommand<MyCommand>(Schedule.Hourly); |
| 51 | + }); |
| 52 | +} |
| 53 | + |
| 54 | +public class JobsHostedService(ILogger<JobsHostedService> log, IBackgroundJobs jobs) : BackgroundService |
| 55 | +{ |
| 56 | + protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
| 57 | + { |
| 58 | + await jobs.StartAsync(stoppingToken); |
| 59 | + |
| 60 | + using var timer = new PeriodicTimer(TimeSpan.FromSeconds(3)); |
| 61 | + while (!stoppingToken.IsCancellationRequested && await timer.WaitForNextTickAsync(stoppingToken)) |
| 62 | + { |
| 63 | + await jobs.TickAsync(); |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +/// <summary> |
| 69 | +/// Sends emails by executing SendEmailCommand in a background job where it's serially processed by 'smtp' worker |
| 70 | +/// </summary> |
| 71 | +public class EmailSender(IBackgroundJobs jobs) : IEmailSender<ApplicationUser> |
| 72 | +{ |
| 73 | + public Task SendEmailAsync(string email, string subject, string htmlMessage) |
| 74 | + { |
| 75 | + jobs.EnqueueCommand<SendEmailCommand>(new SendEmail { |
| 76 | + To = email, |
| 77 | + Subject = subject, |
| 78 | + BodyHtml = htmlMessage, |
| 79 | + }); |
| 80 | + return Task.CompletedTask; |
| 81 | + } |
| 82 | + |
| 83 | + public Task SendConfirmationLinkAsync(ApplicationUser user, string email, string confirmationLink) => |
| 84 | + SendEmailAsync(email, "Confirm your email", $"Please confirm your account by <a href='{confirmationLink}'>clicking here</a>."); |
| 85 | + |
| 86 | + public Task SendPasswordResetLinkAsync(ApplicationUser user, string email, string resetLink) => |
| 87 | + SendEmailAsync(email, "Reset your password", $"Please reset your password by <a href='{resetLink}'>clicking here</a>."); |
| 88 | + |
| 89 | + public Task SendPasswordResetCodeAsync(ApplicationUser user, string email, string resetCode) => |
| 90 | + SendEmailAsync(email, "Reset your password", $"Please reset your password using the following code: {resetCode}"); |
| 91 | +} |
0 commit comments