1
0
mirror of https://github.com/bitwarden/server synced 2026-02-27 18:03:17 +00:00

[PM-11612] Use SchedulerBuilder to configure Quartz (#4729)

* Use SchedulerBuilder t configure Quartz

* Add null check for instanceName to prevent an unlikely null ref

* Fix startup by assigning the jobFactory manually

---------

Co-authored-by: Daniel James Smith <djsmith85@users.noreply.github.com>
Co-authored-by: Daniel James Smith <2670567+djsmith85@users.noreply.github.com>
This commit is contained in:
Marko Lahma
2026-02-26 09:37:26 +00:00
committed by GitHub
parent 44e993efa1
commit 5eb7bd28f6

View File

@@ -1,9 +1,7 @@
using System.Collections.Specialized;
using Bit.Core.Settings;
using Bit.Core.Settings;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Quartz;
using Quartz.Impl;
using Quartz.Impl.Matchers;
namespace Bit.Core.Jobs;
@@ -37,31 +35,32 @@ public abstract class BaseJobsHostedService : IHostedService, IDisposable
public virtual async Task StartAsync(CancellationToken cancellationToken)
{
var props = new NameValueCollection
var fullName = GetType().FullName;
if (fullName == null)
{
{"quartz.serializer.type", "binary"},
};
throw new InvalidOperationException("Hosted service must have a valid type name.");
}
var schedulerBuilder = SchedulerBuilder.Create()
.WithName(fullName) // Ensure each project has a unique instanceName
.WithId("AUTO");
if (!string.IsNullOrEmpty(_globalSettings.SqlServer.JobSchedulerConnectionString))
{
// Ensure each project has a unique instanceName
props.Add("quartz.scheduler.instanceName", GetType().FullName);
props.Add("quartz.scheduler.instanceId", "AUTO");
props.Add("quartz.jobStore.type", "Quartz.Impl.AdoJobStore.JobStoreTX");
props.Add("quartz.jobStore.driverDelegateType", "Quartz.Impl.AdoJobStore.SqlServerDelegate");
props.Add("quartz.jobStore.useProperties", "true");
props.Add("quartz.jobStore.dataSource", "default");
props.Add("quartz.jobStore.tablePrefix", "QRTZ_");
props.Add("quartz.jobStore.clustered", "true");
props.Add("quartz.dataSource.default.provider", "SqlServer");
props.Add("quartz.dataSource.default.connectionString", _globalSettings.SqlServer.JobSchedulerConnectionString);
schedulerBuilder = schedulerBuilder.UsePersistentStore(options =>
{
options.UseProperties = true;
options.UseClustering();
options.UseBinarySerializer();
options.UseSqlServer(connectionString: _globalSettings.SqlServer.JobSchedulerConnectionString);
});
}
var factory = new StdSchedulerFactory(props);
var factory = schedulerBuilder.Build();
_scheduler = await factory.GetScheduler(cancellationToken);
_scheduler.JobFactory = new JobFactory(_serviceProvider);
_scheduler.ListenerManager.AddJobListener(new JobListener(_listenerLogger),
GroupMatcher<JobKey>.AnyGroup());
_scheduler.ListenerManager.AddJobListener(new JobListener(_listenerLogger), GroupMatcher<JobKey>.AnyGroup());
await _scheduler.Start(cancellationToken);
var jobKeys = new List<JobKey>();
@@ -81,23 +80,23 @@ public abstract class BaseJobsHostedService : IHostedService, IDisposable
// There's a race condition when starting multiple containers simultaneously, retry until it succeeds..
try
{
var dupeT = await _scheduler.GetTrigger(trigger.Key);
var dupeT = await _scheduler.GetTrigger(trigger.Key, cancellationToken);
if (dupeT != null)
{
await _scheduler.RescheduleJob(trigger.Key, trigger);
await _scheduler.RescheduleJob(trigger.Key, trigger, cancellationToken);
}
var jobDetail = JobBuilder.Create(job)
.WithIdentity(job.FullName!)
.Build();
var dupeJ = await _scheduler.GetJobDetail(jobDetail.Key);
var dupeJ = await _scheduler.GetJobDetail(jobDetail.Key, cancellationToken);
if (dupeJ != null)
{
await _scheduler.DeleteJob(jobDetail.Key);
await _scheduler.DeleteJob(jobDetail.Key, cancellationToken);
}
await _scheduler.ScheduleJob(jobDetail, trigger);
await _scheduler.ScheduleJob(jobDetail, trigger, cancellationToken);
break;
}
catch (Exception e)
@@ -116,7 +115,7 @@ public abstract class BaseJobsHostedService : IHostedService, IDisposable
}
// Delete old Jobs and Triggers
var existingJobKeys = await _scheduler.GetJobKeys(GroupMatcher<JobKey>.AnyGroup());
var existingJobKeys = await _scheduler.GetJobKeys(GroupMatcher<JobKey>.AnyGroup(), cancellationToken);
foreach (var key in existingJobKeys)
{
@@ -126,10 +125,10 @@ public abstract class BaseJobsHostedService : IHostedService, IDisposable
}
_logger.LogInformation("Deleting old job with key {Key}", key);
await _scheduler.DeleteJob(key);
await _scheduler.DeleteJob(key, cancellationToken);
}
var existingTriggerKeys = await _scheduler.GetTriggerKeys(GroupMatcher<TriggerKey>.AnyGroup());
var existingTriggerKeys = await _scheduler.GetTriggerKeys(GroupMatcher<TriggerKey>.AnyGroup(), cancellationToken);
foreach (var key in existingTriggerKeys)
{
@@ -139,7 +138,7 @@ public abstract class BaseJobsHostedService : IHostedService, IDisposable
}
_logger.LogInformation("Unscheduling old trigger with key {Key}", key);
await _scheduler.UnscheduleJob(key);
await _scheduler.UnscheduleJob(key, cancellationToken);
}
}