set SiteState in HostedServiceBase for scheduled jobs

This commit is contained in:
Shaun Walker 2021-01-18 08:59:07 -05:00
parent a2029a3ca3
commit 8be9fd6eb2
2 changed files with 83 additions and 89 deletions

View File

@ -91,7 +91,15 @@ namespace Oqtane.Infrastructure
// execute the job // execute the job
try try
{ {
log.Notes = ExecuteJob(scope.ServiceProvider); var notes = "";
var tenants = scope.ServiceProvider.GetRequiredService<ITenantRepository>();
var siteState = scope.ServiceProvider.GetRequiredService<SiteState>();
foreach (var tenant in tenants.GetTenants())
{
siteState.Alias = new Alias { TenantId = tenant.TenantId };
notes += ExecuteJob(scope.ServiceProvider);
}
log.Notes = notes;
log.Succeeded = true; log.Succeeded = true;
} }
catch (Exception ex) catch (Exception ex)

View File

@ -26,103 +26,89 @@ namespace Oqtane.Infrastructure
{ {
string log = ""; string log = "";
// iterate through tenants in this installation // get services
List<int> tenants = new List<int>(); var siteRepository = provider.GetRequiredService<ISiteRepository>();
var aliasRepository = provider.GetRequiredService<IAliasRepository>(); var settingRepository = provider.GetRequiredService<ISettingRepository>();
List<Alias> aliases = aliasRepository.GetAliases().ToList(); var notificationRepository = provider.GetRequiredService<INotificationRepository>();
foreach (Alias alias in aliases)
// iterate through sites for this tenant
List<Site> sites = siteRepository.GetSites().ToList();
foreach (Site site in sites)
{ {
if (tenants.Contains(alias.TenantId)) continue; log += "Processing Notifications For Site: " + site.Name + "<br />";
tenants.Add(alias.TenantId);
// use the SiteState to set the Alias explicitly so the tenant can be resolved // get site settings
var siteState = provider.GetRequiredService<SiteState>(); List<Setting> sitesettings = settingRepository.GetSettings(EntityNames.Site, site.SiteId).ToList();
siteState.Alias = alias; Dictionary<string, string> settings = GetSettings(sitesettings);
if (settings.ContainsKey("SMTPHost") && settings["SMTPHost"] != "" &&
// get services which require tenant resolution settings.ContainsKey("SMTPPort") && settings["SMTPPort"] != "" &&
var siteRepository = provider.GetRequiredService<ISiteRepository>(); settings.ContainsKey("SMTPSSL") && settings["SMTPSSL"] != "" &&
var settingRepository = provider.GetRequiredService<ISettingRepository>(); settings.ContainsKey("SMTPSender") && settings["SMTPSender"] != "")
var notificationRepository = provider.GetRequiredService<INotificationRepository>();
// iterate through sites for this tenant
List<Site> sites = siteRepository.GetSites().ToList();
foreach (Site site in sites)
{ {
log += "Processing Notifications For Site: " + site.Name + "<br />"; // construct SMTP Client
var client = new SmtpClient()
// get site settings
List<Setting> sitesettings = settingRepository.GetSettings(EntityNames.Site, site.SiteId).ToList();
Dictionary<string, string> 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 DeliveryMethod = SmtpDeliveryMethod.Network,
var client = new SmtpClient() UseDefaultCredentials = false,
{ Host = settings["SMTPHost"],
DeliveryMethod = SmtpDeliveryMethod.Network, Port = int.Parse(settings["SMTPPort"]),
UseDefaultCredentials = false, EnableSsl = bool.Parse(settings["SMTPSSL"])
Host = settings["SMTPHost"], };
Port = int.Parse(settings["SMTPPort"]), if (settings["SMTPUsername"] != "" && settings["SMTPPassword"] != "")
EnableSsl = bool.Parse(settings["SMTPSSL"]) {
}; client.Credentials = new NetworkCredential(settings["SMTPUsername"], settings["SMTPPassword"]);
if (settings["SMTPUsername"] != "" && settings["SMTPPassword"] != "") }
{
client.Credentials = new NetworkCredential(settings["SMTPUsername"], settings["SMTPPassword"]);
}
// iterate through notifications // iterate through notifications
int sent = 0; int sent = 0;
List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList(); List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList();
foreach (Notification notification in notifications) 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.Body = "From: " + notification.FromDisplayName + "<" + notification.FromEmail + ">" + "\n";
mailMessage.From = new MailAddress(settings["SMTPSender"], site.Name); }
mailMessage.Subject = notification.Subject; else
if (notification.FromUserId != null) {
{ mailMessage.Body = "From: " + site.Name + "\n";
mailMessage.Body = "From: " + notification.FromDisplayName + "<" + notification.FromEmail + ">" + "\n"; }
} mailMessage.Body += "Sent: " + notification.CreatedOn + "\n";
else if (notification.ToUserId != null)
{ {
mailMessage.Body = "From: " + site.Name + "\n"; mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName));
} mailMessage.Body += "To: " + notification.ToDisplayName + "<" + notification.ToEmail + ">" + "\n";
mailMessage.Body += "Sent: " + notification.CreatedOn + "\n"; }
if (notification.ToUserId != null) else
{ {
mailMessage.To.Add(new MailAddress(notification.ToEmail, notification.ToDisplayName)); mailMessage.To.Add(new MailAddress(notification.ToEmail));
mailMessage.Body += "To: " + notification.ToDisplayName + "<" + notification.ToEmail + ">" + "\n"; mailMessage.Body += "To: " + notification.ToEmail + "\n";
} }
else mailMessage.Body += "Subject: " + notification.Subject + "\n\n";
{ mailMessage.Body += notification.Body;
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 // send mail
try try
{ {
client.Send(mailMessage); client.Send(mailMessage);
sent = sent++; sent = sent++;
notification.IsDelivered = true; notification.IsDelivered = true;
notification.DeliveredOn = DateTime.UtcNow; notification.DeliveredOn = DateTime.UtcNow;
notificationRepository.UpdateNotification(notification); notificationRepository.UpdateNotification(notification);
} }
catch (Exception ex) catch (Exception ex)
{ {
// error // error
log += ex.Message + "<br />"; log += ex.Message + "<br />";
}
} }
log += "Notifications Delivered: " + sent + "<br />";
}
else
{
log += "SMTP Not Configured Properly In Site Settings - Host, Port, SSL, And Sender Are All Required" + "<br />";
} }
log += "Notifications Delivered: " + sent + "<br />";
}
else
{
log += "SMTP Not Configured Properly In Site Settings - Host, Port, SSL, And Sender Are All Required" + "<br />";
} }
} }