From 8be9fd6eb2ae9d7dc0d1d436043983934c995636 Mon Sep 17 00:00:00 2001 From: Shaun Walker Date: Mon, 18 Jan 2021 08:59:07 -0500 Subject: [PATCH] set SiteState in HostedServiceBase for scheduled jobs --- .../Infrastructure/Jobs/HostedServiceBase.cs | 10 +- .../Infrastructure/Jobs/NotificationJob.cs | 162 ++++++++---------- 2 files changed, 83 insertions(+), 89 deletions(-) diff --git a/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs b/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs index 96ab55a0..36fbefb6 100644 --- a/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs +++ b/Oqtane.Server/Infrastructure/Jobs/HostedServiceBase.cs @@ -91,7 +91,15 @@ namespace Oqtane.Infrastructure // execute the job try { - log.Notes = ExecuteJob(scope.ServiceProvider); + var notes = ""; + var tenants = scope.ServiceProvider.GetRequiredService(); + var siteState = scope.ServiceProvider.GetRequiredService(); + foreach (var tenant in tenants.GetTenants()) + { + siteState.Alias = new Alias { TenantId = tenant.TenantId }; + notes += ExecuteJob(scope.ServiceProvider); + } + log.Notes = notes; log.Succeeded = true; } catch (Exception ex) diff --git a/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs b/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs index bc1a2d31..92d1d336 100644 --- a/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs +++ b/Oqtane.Server/Infrastructure/Jobs/NotificationJob.cs @@ -26,103 +26,89 @@ namespace Oqtane.Infrastructure { string log = ""; - // iterate through tenants in this installation - List tenants = new List(); - var aliasRepository = provider.GetRequiredService(); - List aliases = aliasRepository.GetAliases().ToList(); - foreach (Alias alias in aliases) + // get services + var siteRepository = provider.GetRequiredService(); + var settingRepository = provider.GetRequiredService(); + var notificationRepository = provider.GetRequiredService(); + + // iterate through sites for this tenant + List sites = siteRepository.GetSites().ToList(); + foreach (Site site in sites) { - if (tenants.Contains(alias.TenantId)) continue; - tenants.Add(alias.TenantId); + log += "Processing Notifications For Site: " + site.Name + "
"; - // use the SiteState to set the Alias explicitly so the tenant can be resolved - var siteState = provider.GetRequiredService(); - siteState.Alias = alias; - - // get services which require tenant resolution - var siteRepository = provider.GetRequiredService(); - var settingRepository = provider.GetRequiredService(); - var notificationRepository = provider.GetRequiredService(); - - // iterate through sites for this tenant - List sites = siteRepository.GetSites().ToList(); - foreach (Site site in sites) + // get site settings + List sitesettings = settingRepository.GetSettings(EntityNames.Site, site.SiteId).ToList(); + Dictionary settings = GetSettings(sitesettings); + if (settings.ContainsKey("SMTPHost") && settings["SMTPHost"] != "" && + settings.ContainsKey("SMTPPort") && settings["SMTPPort"] != "" && + settings.ContainsKey("SMTPSSL") && settings["SMTPSSL"] != "" && + settings.ContainsKey("SMTPSender") && settings["SMTPSender"] != "") { - log += "Processing Notifications For Site: " + site.Name + "
"; - - // get site settings - List sitesettings = settingRepository.GetSettings(EntityNames.Site, site.SiteId).ToList(); - Dictionary settings = GetSettings(sitesettings); - if (settings.ContainsKey("SMTPHost") && settings["SMTPHost"] != "" && - settings.ContainsKey("SMTPPort") && settings["SMTPPort"] != "" && - settings.ContainsKey("SMTPSSL") && settings["SMTPSSL"] != "" && - settings.ContainsKey("SMTPSender") && settings["SMTPSender"] != "") + // construct SMTP Client + var client = new SmtpClient() { - // construct SMTP Client - var client = new SmtpClient() - { - DeliveryMethod = SmtpDeliveryMethod.Network, - UseDefaultCredentials = false, - Host = settings["SMTPHost"], - Port = int.Parse(settings["SMTPPort"]), - EnableSsl = bool.Parse(settings["SMTPSSL"]) - }; - if (settings["SMTPUsername"] != "" && settings["SMTPPassword"] != "") - { - client.Credentials = new NetworkCredential(settings["SMTPUsername"], settings["SMTPPassword"]); - } + DeliveryMethod = SmtpDeliveryMethod.Network, + UseDefaultCredentials = false, + Host = settings["SMTPHost"], + Port = int.Parse(settings["SMTPPort"]), + EnableSsl = bool.Parse(settings["SMTPSSL"]) + }; + if (settings["SMTPUsername"] != "" && settings["SMTPPassword"] != "") + { + client.Credentials = new NetworkCredential(settings["SMTPUsername"], settings["SMTPPassword"]); + } - // iterate through notifications - int sent = 0; - List notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList(); - foreach (Notification notification in notifications) + // iterate through notifications + int sent = 0; + List notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList(); + foreach (Notification notification in notifications) + { + MailMessage mailMessage = new MailMessage(); + mailMessage.From = new MailAddress(settings["SMTPSender"], site.Name); + mailMessage.Subject = notification.Subject; + if (notification.FromUserId != null) { - MailMessage mailMessage = new MailMessage(); - mailMessage.From = new MailAddress(settings["SMTPSender"], site.Name); - mailMessage.Subject = notification.Subject; - if (notification.FromUserId != null) - { - mailMessage.Body = "From: " + notification.FromDisplayName + "<" + notification.FromEmail + ">" + "\n"; - } - else - { - mailMessage.Body = "From: " + site.Name + "\n"; - } - mailMessage.Body += "Sent: " + notification.CreatedOn + "\n"; - if (notification.ToUserId != null) - { - mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName)); - mailMessage.Body += "To: " + notification.ToDisplayName + "<" + notification.ToEmail + ">" + "\n"; - } - else - { - mailMessage.To.Add(new MailAddress(notification.ToEmail)); - mailMessage.Body += "To: " + notification.ToEmail + "\n"; - } - mailMessage.Body += "Subject: " + notification.Subject + "\n\n"; - mailMessage.Body += notification.Body; + mailMessage.Body = "From: " + notification.FromDisplayName + "<" + notification.FromEmail + ">" + "\n"; + } + else + { + mailMessage.Body = "From: " + site.Name + "\n"; + } + mailMessage.Body += "Sent: " + notification.CreatedOn + "\n"; + if (notification.ToUserId != null) + { + mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName)); + mailMessage.Body += "To: " + notification.ToDisplayName + "<" + notification.ToEmail + ">" + "\n"; + } + else + { + mailMessage.To.Add(new MailAddress(notification.ToEmail)); + mailMessage.Body += "To: " + notification.ToEmail + "\n"; + } + mailMessage.Body += "Subject: " + notification.Subject + "\n\n"; + mailMessage.Body += notification.Body; - // send mail - try - { - client.Send(mailMessage); - sent = sent++; - notification.IsDelivered = true; - notification.DeliveredOn = DateTime.UtcNow; - notificationRepository.UpdateNotification(notification); - } - catch (Exception ex) - { - // error - log += ex.Message + "
"; - } + // send mail + try + { + client.Send(mailMessage); + sent = sent++; + notification.IsDelivered = true; + notification.DeliveredOn = DateTime.UtcNow; + notificationRepository.UpdateNotification(notification); + } + catch (Exception ex) + { + // error + log += ex.Message + "
"; } - log += "Notifications Delivered: " + sent + "
"; - } - else - { - log += "SMTP Not Configured Properly In Site Settings - Host, Port, SSL, And Sender Are All Required" + "
"; } + log += "Notifications Delivered: " + sent + "
"; + } + else + { + log += "SMTP Not Configured Properly In Site Settings - Host, Port, SSL, And Sender Are All Required" + "
"; } }