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));
|
||||
}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user