fix issue #170 which is related to the host user not being part of the Registered Users role
This commit is contained in:
92
Oqtane.Server/Infrastructure/HostedServiceBase.cs
Normal file
92
Oqtane.Server/Infrastructure/HostedServiceBase.cs
Normal file
@ -0,0 +1,92 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Repository;
|
||||
using Oqtane.Shared;
|
||||
|
||||
namespace Oqtane.Infrastructure
|
||||
{
|
||||
public abstract class HostedServiceBase : IHostedService, IDisposable
|
||||
{
|
||||
private Task ExecutingTask;
|
||||
private readonly CancellationTokenSource CancellationTokenSource = new CancellationTokenSource();
|
||||
private readonly IServiceScopeFactory ServiceScopeFactory;
|
||||
|
||||
public HostedServiceBase(IServiceScopeFactory ServiceScopeFactory)
|
||||
{
|
||||
this.ServiceScopeFactory = ServiceScopeFactory;
|
||||
}
|
||||
|
||||
// abstract method must be overridden by job
|
||||
public abstract void ExecuteJob(IServiceProvider provider);
|
||||
|
||||
|
||||
protected async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
try
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
// allows consumption of scoped services
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
string JobType = Utilities.GetFullTypeName(this.GetType().AssemblyQualifiedName);
|
||||
IScheduleRepository ScheduleRepository = scope.ServiceProvider.GetRequiredService<IScheduleRepository>();
|
||||
List<Schedule> schedules = ScheduleRepository.GetSchedules().ToList();
|
||||
Schedule schedule = schedules.Where(item => item.JobType == JobType).FirstOrDefault();
|
||||
if (schedule != null && schedule.IsActive)
|
||||
{
|
||||
ExecuteJob(scope.ServiceProvider);
|
||||
}
|
||||
}
|
||||
|
||||
await Task.Delay(TimeSpan.FromMinutes(1), stoppingToken);
|
||||
}
|
||||
}
|
||||
catch
|
||||
{
|
||||
// can occur during the initial installation as there is no DBContext
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
ExecutingTask = ExecuteAsync(CancellationTokenSource.Token);
|
||||
|
||||
if (ExecutingTask.IsCompleted)
|
||||
{
|
||||
return ExecutingTask;
|
||||
}
|
||||
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken CancellationToken)
|
||||
{
|
||||
if (ExecutingTask == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
CancellationTokenSource.Cancel();
|
||||
}
|
||||
finally
|
||||
{
|
||||
await Task.WhenAny(ExecutingTask, Task.Delay(Timeout.Infinite, CancellationToken));
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
CancellationTokenSource.Cancel();
|
||||
}
|
||||
}
|
||||
}
|
@ -62,11 +62,18 @@ namespace Oqtane.Infrastructure
|
||||
log.Level = Enum.GetName(typeof(LogLevel), Level);
|
||||
if (Exception != null)
|
||||
{
|
||||
log.Exception = JsonSerializer.Serialize(Exception.ToString());
|
||||
log.Exception = Exception.ToString();
|
||||
}
|
||||
log.Message = Message;
|
||||
log.MessageTemplate = "";
|
||||
log.Properties = JsonSerializer.Serialize(Args);
|
||||
try
|
||||
{
|
||||
log.Properties = JsonSerializer.Serialize(Args);
|
||||
}
|
||||
catch // serialization error occurred
|
||||
{
|
||||
log.Properties = "";
|
||||
}
|
||||
Log(log);
|
||||
}
|
||||
|
||||
|
21
Oqtane.Server/Infrastructure/TestJob.cs
Normal file
21
Oqtane.Server/Infrastructure/TestJob.cs
Normal file
@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Oqtane.Models;
|
||||
using Oqtane.Repository;
|
||||
|
||||
namespace Oqtane.Infrastructure
|
||||
{
|
||||
public class TestJob : HostedServiceBase
|
||||
{
|
||||
public TestJob(IServiceScopeFactory ServiceScopeFactory) : base(ServiceScopeFactory) {}
|
||||
|
||||
public override void ExecuteJob(IServiceProvider provider)
|
||||
{
|
||||
var Tenants = provider.GetRequiredService<ITenantRepository>();
|
||||
foreach(Tenant tenant in Tenants.GetTenants())
|
||||
{
|
||||
// is it possible to set the HttpContext so that DbContextBase will resolve properly for tenants?
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user