mirror of
https://github.com/bitwarden/server.git
synced 2025-07-05 18:12:48 -05:00
abstract quartz jobs sevrice out to core
This commit is contained in:
@ -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()
|
||||
{ }
|
||||
}
|
||||
}
|
27
src/Core/Jobs/JobFactory.cs
Normal file
27
src/Core/Jobs/JobFactory.cs
Normal file
@ -0,0 +1,27 @@
|
||||
using System;
|
||||
using Quartz;
|
||||
using Quartz.Spi;
|
||||
|
||||
namespace Bit.Core.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();
|
||||
}
|
||||
}
|
||||
}
|
40
src/Core/Jobs/JobListener.cs
Normal file
40
src/Core/Jobs/JobListener.cs
Normal file
@ -0,0 +1,40 @@
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Bit.Core.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);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user