Server naming fixes and cleanup
Server is now completely cleaned up and without warnings
This commit is contained in:
@ -13,7 +13,7 @@ namespace Oqtane.Infrastructure
|
||||
{
|
||||
public abstract class HostedServiceBase : IHostedService, IDisposable
|
||||
{
|
||||
private Task ExecutingTask;
|
||||
private Task _executingTask;
|
||||
private readonly CancellationTokenSource _cancellationTokenSource = new CancellationTokenSource();
|
||||
private readonly IServiceScopeFactory _serviceScopeFactory;
|
||||
|
||||
@ -34,43 +34,43 @@ namespace Oqtane.Infrastructure
|
||||
using (var scope = _serviceScopeFactory.CreateScope())
|
||||
{
|
||||
// get name of job
|
||||
string JobType = Utilities.GetFullTypeName(GetType().AssemblyQualifiedName);
|
||||
string jobType = Utilities.GetFullTypeName(GetType().AssemblyQualifiedName);
|
||||
|
||||
// load jobs and find current job
|
||||
IJobRepository Jobs = scope.ServiceProvider.GetRequiredService<IJobRepository>();
|
||||
Job Job = Jobs.GetJobs().Where(item => item.JobType == JobType).FirstOrDefault();
|
||||
if (Job != null && Job.IsEnabled && !Job.IsExecuting)
|
||||
IJobRepository jobs = scope.ServiceProvider.GetRequiredService<IJobRepository>();
|
||||
Job job = jobs.GetJobs().Where(item => item.JobType == jobType).FirstOrDefault();
|
||||
if (job != null && job.IsEnabled && !job.IsExecuting)
|
||||
{
|
||||
// set next execution date
|
||||
if (Job.NextExecution == null)
|
||||
if (job.NextExecution == null)
|
||||
{
|
||||
if (Job.StartDate != null)
|
||||
if (job.StartDate != null)
|
||||
{
|
||||
Job.NextExecution = Job.StartDate;
|
||||
job.NextExecution = job.StartDate;
|
||||
}
|
||||
else
|
||||
{
|
||||
Job.NextExecution = DateTime.UtcNow;
|
||||
job.NextExecution = DateTime.UtcNow;
|
||||
}
|
||||
}
|
||||
|
||||
// determine if the job should be run
|
||||
if (Job.NextExecution <= DateTime.UtcNow && (Job.EndDate == null || Job.EndDate >= DateTime.UtcNow))
|
||||
if (job.NextExecution <= DateTime.UtcNow && (job.EndDate == null || job.EndDate >= DateTime.UtcNow))
|
||||
{
|
||||
IJobLogRepository JobLogs = scope.ServiceProvider.GetRequiredService<IJobLogRepository>();
|
||||
IJobLogRepository jobLogs = scope.ServiceProvider.GetRequiredService<IJobLogRepository>();
|
||||
|
||||
// create a job log entry
|
||||
JobLog log = new JobLog();
|
||||
log.JobId = Job.JobId;
|
||||
log.JobId = job.JobId;
|
||||
log.StartDate = DateTime.UtcNow;
|
||||
log.FinishDate = null;
|
||||
log.Succeeded = false;
|
||||
log.Notes = "";
|
||||
log = JobLogs.AddJobLog(log);
|
||||
log = jobLogs.AddJobLog(log);
|
||||
|
||||
// update the job to indicate it is running
|
||||
Job.IsExecuting = true;
|
||||
Jobs.UpdateJob(Job);
|
||||
job.IsExecuting = true;
|
||||
jobs.UpdateJob(job);
|
||||
|
||||
// execute the job
|
||||
try
|
||||
@ -86,19 +86,19 @@ namespace Oqtane.Infrastructure
|
||||
|
||||
// update the job log
|
||||
log.FinishDate = DateTime.UtcNow;
|
||||
JobLogs.UpdateJobLog(log);
|
||||
jobLogs.UpdateJobLog(log);
|
||||
|
||||
// update the job
|
||||
Job.NextExecution = CalculateNextExecution(Job.NextExecution.Value, Job.Frequency, Job.Interval);
|
||||
Job.IsExecuting = false;
|
||||
Jobs.UpdateJob(Job);
|
||||
job.NextExecution = CalculateNextExecution(job.NextExecution.Value, job.Frequency, job.Interval);
|
||||
job.IsExecuting = false;
|
||||
jobs.UpdateJob(job);
|
||||
|
||||
// trim the job log
|
||||
List<JobLog> logs = JobLogs.GetJobLogs().Where(item => item.JobId == Job.JobId)
|
||||
List<JobLog> logs = jobLogs.GetJobLogs().Where(item => item.JobId == job.JobId)
|
||||
.OrderByDescending(item => item.JobLogId).ToList();
|
||||
for (int i = logs.Count; i > Job.RetentionHistory; i--)
|
||||
for (int i = logs.Count; i > job.RetentionHistory; i--)
|
||||
{
|
||||
JobLogs.DeleteJobLog(logs[i - 1].JobLogId);
|
||||
jobLogs.DeleteJobLog(logs[i - 1].JobLogId);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -115,28 +115,28 @@ namespace Oqtane.Infrastructure
|
||||
|
||||
}
|
||||
|
||||
private DateTime CalculateNextExecution(DateTime NextExecution, string Frequency, int Interval)
|
||||
private DateTime CalculateNextExecution(DateTime nextExecution, string frequency, int interval)
|
||||
{
|
||||
switch (Frequency)
|
||||
switch (frequency)
|
||||
{
|
||||
case "m": // minutes
|
||||
NextExecution = NextExecution.AddMinutes(Interval);
|
||||
nextExecution = nextExecution.AddMinutes(interval);
|
||||
break;
|
||||
case "H": // hours
|
||||
NextExecution = NextExecution.AddHours(Interval);
|
||||
nextExecution = nextExecution.AddHours(interval);
|
||||
break;
|
||||
case "d": // days
|
||||
NextExecution = NextExecution.AddDays(Interval);
|
||||
nextExecution = nextExecution.AddDays(interval);
|
||||
break;
|
||||
case "M": // months
|
||||
NextExecution = NextExecution.AddMonths(Interval);
|
||||
nextExecution = nextExecution.AddMonths(interval);
|
||||
break;
|
||||
}
|
||||
if (NextExecution < DateTime.UtcNow)
|
||||
if (nextExecution < DateTime.UtcNow)
|
||||
{
|
||||
NextExecution = DateTime.UtcNow;
|
||||
nextExecution = DateTime.UtcNow;
|
||||
}
|
||||
return NextExecution;
|
||||
return nextExecution;
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
@ -146,14 +146,14 @@ namespace Oqtane.Infrastructure
|
||||
// set IsExecuting to false in case this job was forcefully terminated previously
|
||||
using (var scope = _serviceScopeFactory.CreateScope())
|
||||
{
|
||||
string JobType = Utilities.GetFullTypeName(GetType().AssemblyQualifiedName);
|
||||
IJobRepository Jobs = scope.ServiceProvider.GetRequiredService<IJobRepository>();
|
||||
Job Job = Jobs.GetJobs().Where(item => item.JobType == JobType).FirstOrDefault();
|
||||
if (Job != null)
|
||||
string jobType = Utilities.GetFullTypeName(GetType().AssemblyQualifiedName);
|
||||
IJobRepository jobs = scope.ServiceProvider.GetRequiredService<IJobRepository>();
|
||||
Job job = jobs.GetJobs().Where(item => item.JobType == jobType).FirstOrDefault();
|
||||
if (job != null)
|
||||
{
|
||||
Job.IsStarted = true;
|
||||
Job.IsExecuting = false;
|
||||
Jobs.UpdateJob(Job);
|
||||
job.IsStarted = true;
|
||||
job.IsExecuting = false;
|
||||
jobs.UpdateJob(job);
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -162,19 +162,19 @@ namespace Oqtane.Infrastructure
|
||||
// can occur during the initial installation as there is no DBContext
|
||||
}
|
||||
|
||||
ExecutingTask = ExecuteAsync(_cancellationTokenSource.Token);
|
||||
_executingTask = ExecuteAsync(_cancellationTokenSource.Token);
|
||||
|
||||
if (ExecutingTask.IsCompleted)
|
||||
if (_executingTask.IsCompleted)
|
||||
{
|
||||
return ExecutingTask;
|
||||
return _executingTask;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken CancellationToken)
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
if (ExecutingTask == null)
|
||||
if (_executingTask == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
@ -185,7 +185,7 @@ namespace Oqtane.Infrastructure
|
||||
}
|
||||
finally
|
||||
{
|
||||
await Task.WhenAny(ExecutingTask, Task.Delay(Timeout.Infinite, CancellationToken));
|
||||
await Task.WhenAny(_executingTask, Task.Delay(Timeout.Infinite, cancellationToken));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -14,34 +14,34 @@ namespace Oqtane.Infrastructure
|
||||
{
|
||||
// JobType = "Oqtane.Infrastructure.NotificationJob, Oqtane.Server"
|
||||
|
||||
public NotificationJob(IServiceScopeFactory ServiceScopeFactory) : base(ServiceScopeFactory) {}
|
||||
public NotificationJob(IServiceScopeFactory serviceScopeFactory) : base(serviceScopeFactory) {}
|
||||
|
||||
public override string ExecuteJob(IServiceProvider provider)
|
||||
{
|
||||
string log = "";
|
||||
|
||||
// iterate through aliases in this installation
|
||||
var Aliases = provider.GetRequiredService<IAliasRepository>();
|
||||
List<Alias> aliases = Aliases.GetAliases().ToList();
|
||||
var aliasRepository = provider.GetRequiredService<IAliasRepository>();
|
||||
List<Alias> aliases = aliasRepository.GetAliases().ToList();
|
||||
foreach (Alias alias in aliases)
|
||||
{
|
||||
// use the SiteState to set the Alias explicitly so the tenant can be resolved
|
||||
var sitestate = provider.GetRequiredService<SiteState>();
|
||||
sitestate.Alias = alias;
|
||||
var siteState = provider.GetRequiredService<SiteState>();
|
||||
siteState.Alias = alias;
|
||||
|
||||
// get services which require tenant resolution
|
||||
var Sites = provider.GetRequiredService<ISiteRepository>();
|
||||
var Settings = provider.GetRequiredService<ISettingRepository>();
|
||||
var Notifications = provider.GetRequiredService<INotificationRepository>();
|
||||
var siteRepository = provider.GetRequiredService<ISiteRepository>();
|
||||
var settingRepository = provider.GetRequiredService<ISettingRepository>();
|
||||
var notificationRepository = provider.GetRequiredService<INotificationRepository>();
|
||||
|
||||
// iterate through sites
|
||||
List<Site> sites = Sites.GetSites().ToList();
|
||||
List<Site> sites = siteRepository.GetSites().ToList();
|
||||
foreach (Site site in sites)
|
||||
{
|
||||
log += "Processing Notifications For Site: " + site.Name + "\n\n";
|
||||
|
||||
// get site settings
|
||||
List<Setting> sitesettings = Settings.GetSettings("Site", site.SiteId).ToList();
|
||||
List<Setting> sitesettings = settingRepository.GetSettings("Site", site.SiteId).ToList();
|
||||
Dictionary<string, string> settings = GetSettings(sitesettings);
|
||||
if (settings.ContainsKey("SMTPHost") && settings["SMTPHost"] != "")
|
||||
{
|
||||
@ -61,7 +61,7 @@ namespace Oqtane.Infrastructure
|
||||
|
||||
// iterate through notifications
|
||||
int sent = 0;
|
||||
List<Notification> notifications = Notifications.GetNotifications(site.SiteId, -1, -1).ToList();
|
||||
List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList();
|
||||
foreach (Notification notification in notifications)
|
||||
{
|
||||
MailMessage mailMessage = new MailMessage();
|
||||
@ -75,7 +75,7 @@ namespace Oqtane.Infrastructure
|
||||
{
|
||||
mailMessage.Body = "From: " + site.Name + "\n";
|
||||
}
|
||||
mailMessage.Body += "Sent: " + notification.CreatedOn.ToString() + "\n";
|
||||
mailMessage.Body += "Sent: " + notification.CreatedOn + "\n";
|
||||
if (notification.ToUserId != null)
|
||||
{
|
||||
mailMessage.To.Add(new MailAddress(notification.ToUser.Email, notification.ToUser.DisplayName));
|
||||
@ -96,15 +96,15 @@ namespace Oqtane.Infrastructure
|
||||
sent = sent++;
|
||||
notification.IsDelivered = true;
|
||||
notification.DeliveredOn = DateTime.UtcNow;
|
||||
Notifications.UpdateNotification(notification);
|
||||
notificationRepository.UpdateNotification(notification);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// error
|
||||
log += ex.Message.ToString() + "\n\n";
|
||||
log += ex.Message + "\n\n";
|
||||
}
|
||||
}
|
||||
log += "Notifications Delivered: " + sent.ToString() + "\n\n";
|
||||
log += "Notifications Delivered: " + sent + "\n\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
@ -117,10 +117,10 @@ namespace Oqtane.Infrastructure
|
||||
}
|
||||
|
||||
|
||||
private Dictionary<string, string> GetSettings(List<Setting> Settings)
|
||||
private Dictionary<string, string> GetSettings(List<Setting> settings)
|
||||
{
|
||||
Dictionary<string, string> dictionary = new Dictionary<string, string>();
|
||||
foreach (Setting setting in Settings.OrderBy(item => item.SettingName).ToList())
|
||||
foreach (Setting setting in settings.OrderBy(item => item.SettingName).ToList())
|
||||
{
|
||||
dictionary.Add(setting.SettingName, setting.SettingValue);
|
||||
}
|
||||
|
Reference in New Issue
Block a user