mirror of
https://github.com/bitwarden/server.git
synced 2025-02-28 03:51:23 +01:00
abstract quartz jobs sevrice out to core
This commit is contained in:
parent
06d5b4af29
commit
670f79a861
@ -18,7 +18,6 @@
|
||||
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.2" />
|
||||
<PackageReference Include="IdentityServer4.AccessTokenValidation" Version="2.6.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Server.Kestrel.Transport.Libuv" Version="2.1.2" />
|
||||
<PackageReference Include="Quartz" Version="3.0.6" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
@ -1,20 +1,16 @@
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Jobs;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class AliveJob : IJob
|
||||
public class AliveJob : BaseJob
|
||||
{
|
||||
private readonly ILogger<AliveJob> _logger;
|
||||
public AliveJob(ILogger<AliveJob> logger)
|
||||
: base(logger) { }
|
||||
|
||||
public AliveJob(
|
||||
ILogger<AliveJob> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public Task Execute(IJobExecutionContext context)
|
||||
protected override Task ExecuteJobAsync(IJobExecutionContext context)
|
||||
{
|
||||
_logger.LogInformation("It's alive!");
|
||||
return Task.FromResult(0);
|
||||
|
@ -1,52 +1,27 @@
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Jobs;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
using Quartz.Impl;
|
||||
using Quartz.Impl.Matchers;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class JobsHostedService : IHostedService, IDisposable
|
||||
public class JobsHostedService : BaseJobsHostedService
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILogger<JobListener> _listenerLogger;
|
||||
private IScheduler _scheduler;
|
||||
|
||||
public JobsHostedService(
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger<JobsHostedService> logger,
|
||||
ILogger<JobListener> listenerLogger)
|
||||
: base(serviceProvider, logger, listenerLogger) { }
|
||||
|
||||
public override async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_logger = logger;
|
||||
_listenerLogger = listenerLogger;
|
||||
}
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var factory = new StdSchedulerFactory(new NameValueCollection
|
||||
{
|
||||
{ "quartz.serializer.type", "binary" }
|
||||
});
|
||||
_scheduler = await factory.GetScheduler(cancellationToken);
|
||||
_scheduler.JobFactory = new JobFactory(_serviceProvider);
|
||||
_scheduler.ListenerManager.AddJobListener(new JobListener(_listenerLogger),
|
||||
GroupMatcher<JobKey>.AnyGroup());
|
||||
await _scheduler.Start(cancellationToken);
|
||||
|
||||
var aliveJob = JobBuilder.Create<AliveJob>().Build();
|
||||
var validateUsersJob = JobBuilder.Create<ValidateUsersJob>().Build();
|
||||
var validateOrganizationsJob = JobBuilder.Create<ValidateOrganizationsJob>().Build();
|
||||
|
||||
var everyTopOfTheHourTrigger = TriggerBuilder.Create()
|
||||
.StartNow()
|
||||
.WithCronSchedule("0 0 * * * ?")
|
||||
.WithCronSchedule("* * * * * ?")
|
||||
.Build();
|
||||
var everyTopOfTheSixthHourTrigger = TriggerBuilder.Create()
|
||||
.StartNow()
|
||||
@ -57,19 +32,16 @@ namespace Bit.Api.Jobs
|
||||
.WithCronSchedule("0 30 */12 * * ?")
|
||||
.Build();
|
||||
|
||||
await _scheduler.ScheduleJob(aliveJob, everyTopOfTheHourTrigger);
|
||||
await _scheduler.ScheduleJob(validateUsersJob, everyTopOfTheSixthHourTrigger);
|
||||
await _scheduler.ScheduleJob(validateOrganizationsJob, everyTwelfthHourAndThirtyMinutesTrigger);
|
||||
}
|
||||
Jobs = new List<Tuple<Type, ITrigger>>
|
||||
{
|
||||
new Tuple<Type, ITrigger>(typeof(AliveJob), everyTopOfTheHourTrigger),
|
||||
new Tuple<Type, ITrigger>(typeof(ValidateUsersJob), everyTopOfTheSixthHourTrigger),
|
||||
new Tuple<Type, ITrigger>(typeof(ValidateOrganizationsJob), everyTwelfthHourAndThirtyMinutesTrigger)
|
||||
};
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _scheduler?.Shutdown(cancellationToken);
|
||||
await base.StartAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{ }
|
||||
|
||||
public static void AddJobsServices(IServiceCollection services)
|
||||
{
|
||||
services.AddTransient<AliveJob>();
|
||||
|
@ -1,34 +1,27 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Jobs;
|
||||
using Bit.Core.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class ValidateOrganizationsJob : IJob
|
||||
public class ValidateOrganizationsJob : BaseJob
|
||||
{
|
||||
private readonly ILicensingService _licensingService;
|
||||
private readonly ILogger<ValidateOrganizationsJob> _logger;
|
||||
|
||||
public ValidateOrganizationsJob(
|
||||
ILicensingService licensingService,
|
||||
ILogger<ValidateOrganizationsJob> logger)
|
||||
: base(logger)
|
||||
{
|
||||
_licensingService = licensingService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
protected async override Task ExecuteJobAsync(IJobExecutionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _licensingService.ValidateOrganizationsAsync();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_logger.LogError(2, e, "Error performing {0}.", nameof(ValidateOrganizationsJob));
|
||||
}
|
||||
await _licensingService.ValidateUsersAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,34 +1,26 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Jobs;
|
||||
using Bit.Core.Services;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class ValidateUsersJob : IJob
|
||||
public class ValidateUsersJob : BaseJob
|
||||
{
|
||||
private readonly ILicensingService _licensingService;
|
||||
private readonly ILogger<ValidateUsersJob> _logger;
|
||||
|
||||
public ValidateUsersJob(
|
||||
ILicensingService licensingService,
|
||||
ILogger<ValidateUsersJob> logger)
|
||||
: base(logger)
|
||||
{
|
||||
_licensingService = licensingService;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
protected async override Task ExecuteJobAsync(IJobExecutionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await _licensingService.ValidateUsersAsync();
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_logger.LogError(2, e, "Error performing {0}.", nameof(ValidateUsersJob));
|
||||
}
|
||||
await _licensingService.ValidateUsersAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -23,6 +23,7 @@
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.EnvironmentVariables" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.UserSecrets" Version="2.1.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Identity.Stores" Version="2.1.2" />
|
||||
<PackageReference Include="Quartz" Version="3.0.6" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="2.0.2" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging.File" Version="1.1.0" />
|
||||
<PackageReference Include="Serilog.Sinks.AzureDocumentDB" Version="3.8.0" />
|
||||
|
31
src/Core/Jobs/BaseJob.cs
Normal file
31
src/Core/Jobs/BaseJob.cs
Normal file
@ -0,0 +1,31 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Core.Jobs
|
||||
{
|
||||
public abstract class BaseJob : IJob
|
||||
{
|
||||
protected readonly ILogger _logger;
|
||||
|
||||
public BaseJob(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
try
|
||||
{
|
||||
await ExecuteJobAsync(context);
|
||||
}
|
||||
catch(Exception e)
|
||||
{
|
||||
_logger.LogError(2, e, "Error performing {0}.", GetType().Name);
|
||||
}
|
||||
}
|
||||
|
||||
protected abstract Task ExecuteJobAsync(IJobExecutionContext context);
|
||||
}
|
||||
}
|
63
src/Core/Jobs/BaseJobsHostedService.cs
Normal file
63
src/Core/Jobs/BaseJobsHostedService.cs
Normal file
@ -0,0 +1,63 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
using Quartz.Impl;
|
||||
using Quartz.Impl.Matchers;
|
||||
|
||||
namespace Bit.Core.Jobs
|
||||
{
|
||||
public abstract class BaseJobsHostedService : IHostedService, IDisposable
|
||||
{
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
private readonly ILogger<JobListener> _listenerLogger;
|
||||
protected readonly ILogger _logger;
|
||||
|
||||
private IScheduler _scheduler;
|
||||
|
||||
public BaseJobsHostedService(
|
||||
IServiceProvider serviceProvider,
|
||||
ILogger logger,
|
||||
ILogger<JobListener> listenerLogger)
|
||||
{
|
||||
_serviceProvider = serviceProvider;
|
||||
_logger = logger;
|
||||
_listenerLogger = listenerLogger;
|
||||
}
|
||||
|
||||
public IEnumerable<Tuple<Type, ITrigger>> Jobs { get; protected set; }
|
||||
|
||||
public virtual async Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
var factory = new StdSchedulerFactory(new NameValueCollection
|
||||
{
|
||||
{ "quartz.serializer.type", "binary" }
|
||||
});
|
||||
_scheduler = await factory.GetScheduler(cancellationToken);
|
||||
_scheduler.JobFactory = new JobFactory(_serviceProvider);
|
||||
_scheduler.ListenerManager.AddJobListener(new JobListener(_listenerLogger),
|
||||
GroupMatcher<JobKey>.AnyGroup());
|
||||
await _scheduler.Start(cancellationToken);
|
||||
if(Jobs != null)
|
||||
{
|
||||
foreach(var job in Jobs)
|
||||
{
|
||||
var builtJob = JobBuilder.Create(job.Item1).Build();
|
||||
await _scheduler.ScheduleJob(builtJob, job.Item2);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _scheduler?.Shutdown(cancellationToken);
|
||||
}
|
||||
|
||||
public virtual void Dispose()
|
||||
{ }
|
||||
}
|
||||
}
|
@ -2,7 +2,7 @@
|
||||
using Quartz;
|
||||
using Quartz.Spi;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
namespace Bit.Core.Jobs
|
||||
{
|
||||
public class JobFactory : IJobFactory
|
||||
{
|
@ -4,7 +4,7 @@ using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
namespace Bit.Core.Jobs
|
||||
{
|
||||
public class JobListener : IJobListener
|
||||
{
|
Loading…
Reference in New Issue
Block a user