mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 16:42:50 -05:00
abstract quartz jobs sevrice out to core
This commit is contained in:
@ -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,27 +0,0 @@
|
||||
using System;
|
||||
using Quartz;
|
||||
using Quartz.Spi;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class JobFactory : IJobFactory
|
||||
{
|
||||
private readonly IServiceProvider _container;
|
||||
|
||||
public JobFactory(IServiceProvider container)
|
||||
{
|
||||
_container = container;
|
||||
}
|
||||
|
||||
public IJob NewJob(TriggerFiredBundle bundle, IScheduler scheduler)
|
||||
{
|
||||
return _container.GetService(bundle.JobDetail.JobType) as IJob;
|
||||
}
|
||||
|
||||
public void ReturnJob(IJob job)
|
||||
{
|
||||
var disposable = job as IDisposable;
|
||||
disposable?.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
@ -1,40 +0,0 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Api.Jobs
|
||||
{
|
||||
public class JobListener : IJobListener
|
||||
{
|
||||
private readonly ILogger<JobListener> _logger;
|
||||
|
||||
public JobListener(ILogger<JobListener> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public string Name => "JobListener";
|
||||
|
||||
public Task JobExecutionVetoed(IJobExecutionContext context,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task JobToBeExecuted(IJobExecutionContext context,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
_logger.LogInformation("Starting job {0} at {1}.", context.JobDetail.JobType.Name, DateTime.UtcNow);
|
||||
return Task.FromResult(0);
|
||||
}
|
||||
|
||||
public Task JobWasExecuted(IJobExecutionContext context, JobExecutionException jobException,
|
||||
CancellationToken cancellationToken = default(CancellationToken))
|
||||
{
|
||||
_logger.LogInformation("Finished job {0} at {1}.", context.JobDetail.JobType.Name, DateTime.UtcNow);
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user