auto registration of scheduled jobs
This commit is contained in:
parent
1276c0269e
commit
a2029a3ca3
|
@ -1,4 +1,4 @@
|
|||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Data;
|
||||
using System.IO;
|
||||
|
@ -244,14 +244,6 @@ namespace Oqtane.Infrastructure
|
|||
db.Tenant.Add(tenant);
|
||||
db.SaveChanges();
|
||||
_cache.Remove("tenants");
|
||||
|
||||
if (install.TenantName == TenantNames.Master)
|
||||
{
|
||||
var job = new Job { Name = "Notification Job", JobType = "Oqtane.Infrastructure.NotificationJob, Oqtane.Server", Frequency = "m", Interval = 1, StartDate = null, EndDate = null, IsEnabled = false, IsStarted = false, IsExecuting = false, NextExecution = null, RetentionHistory = 10, CreatedBy = "", CreatedOn = DateTime.UtcNow, ModifiedBy = "", ModifiedOn = DateTime.UtcNow };
|
||||
db.Job.Add(job);
|
||||
db.SaveChanges();
|
||||
_cache.Remove("jobs");
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
using System;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
|
@ -25,6 +25,15 @@ namespace Oqtane.Infrastructure
|
|||
// abstract method must be overridden
|
||||
public abstract string ExecuteJob(IServiceProvider provider);
|
||||
|
||||
// public properties which can be overridden and are used during auto registration of job
|
||||
public string Name { get; set; } = "";
|
||||
public string Frequency { get; set; } = "d"; // day
|
||||
public int Interval { get; set; } = 1;
|
||||
public DateTime? StartDate { get; set; } = null;
|
||||
public DateTime? EndDate { get; set; } = null;
|
||||
public int RetentionHistory { get; set; } = 10;
|
||||
public bool IsEnabled { get; set; } = false;
|
||||
|
||||
protected async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await Task.Yield(); // required so that this method does not block startup
|
||||
|
@ -153,27 +162,52 @@ 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);
|
||||
string jobTypeName = Utilities.GetFullTypeName(GetType().AssemblyQualifiedName);
|
||||
IJobRepository jobs = scope.ServiceProvider.GetRequiredService<IJobRepository>();
|
||||
Job job = jobs.GetJobs().Where(item => item.JobType == jobType).FirstOrDefault();
|
||||
Job job = jobs.GetJobs().Where(item => item.JobType == jobTypeName).FirstOrDefault();
|
||||
if (job != null)
|
||||
{
|
||||
job.IsStarted = true;
|
||||
job.IsExecuting = false;
|
||||
jobs.UpdateJob(job);
|
||||
}
|
||||
else
|
||||
{
|
||||
// auto registration
|
||||
job = new Job { JobType = jobTypeName };
|
||||
// optional properties
|
||||
var jobType = Type.GetType(jobTypeName);
|
||||
var jobObject = ActivatorUtilities.CreateInstance(scope.ServiceProvider, jobType) as HostedServiceBase;
|
||||
if (jobObject.Name != "")
|
||||
{
|
||||
job.Name = jobObject.Name;
|
||||
}
|
||||
else
|
||||
{
|
||||
job.Name = Utilities.GetTypeName(job.JobType);
|
||||
}
|
||||
job.Frequency = jobObject.Frequency;
|
||||
job.Interval = jobObject.Interval;
|
||||
job.StartDate = jobObject.StartDate;
|
||||
job.EndDate = jobObject.EndDate;
|
||||
job.RetentionHistory = jobObject.RetentionHistory;
|
||||
job.IsEnabled = jobObject.IsEnabled;
|
||||
job.IsStarted = true;
|
||||
job.IsExecuting = false;
|
||||
jobs.AddJob(job);
|
||||
}
|
||||
}
|
||||
|
||||
_executingTask = ExecuteAsync(_cancellationTokenSource.Token);
|
||||
|
||||
if (_executingTask.IsCompleted)
|
||||
{
|
||||
return _executingTask;
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// can occur during the initial installation as there is no DBContext
|
||||
}
|
||||
|
||||
_executingTask = ExecuteAsync(_cancellationTokenSource.Token);
|
||||
|
||||
if (_executingTask.IsCompleted)
|
||||
{
|
||||
return _executingTask;
|
||||
// can occur during the initial installation because this method is called during startup and the database has not yet been created
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
|
|
|
@ -14,7 +14,13 @@ namespace Oqtane.Infrastructure
|
|||
{
|
||||
// JobType = "Oqtane.Infrastructure.NotificationJob, Oqtane.Server"
|
||||
|
||||
public NotificationJob(IServiceScopeFactory serviceScopeFactory) : base(serviceScopeFactory) {}
|
||||
public NotificationJob(IServiceScopeFactory serviceScopeFactory) : base(serviceScopeFactory)
|
||||
{
|
||||
Name = "Notification Job";
|
||||
Frequency = "m"; // minute
|
||||
Interval = 1;
|
||||
IsEnabled = false;
|
||||
}
|
||||
|
||||
public override string ExecuteJob(IServiceProvider provider)
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue
Block a user