Path: redb-route/redb.Route.Quartz/QuartzRouteJob.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Quartz/QuartzRouteJob.cs
----
using System.Collections.Concurrent;
using Microsoft.Extensions.Logging;
using Quartz;
namespace redb.Route.Quartz;
///
/// Universal Quartz job that bridges between Quartz scheduler and redb.Route consumers.
/// Created via reflection by Quartz. Finds the running consumer by the job it claimed on this scheduler
/// () and delegates execution. Concurrency is controlled by the consumer's
/// semaphore, NOT by Quartz's [DisallowConcurrentExecution] — this allows configurable threads > 1.
///
/// A fire with no consumer on this node: in an in-memory store the job is this process's own and is cleaned up; in a
/// persistent store it belongs to the store — the next start of this node or another node of the cluster runs it (a
/// rolling upgrade, a route stopped on one node) — so it is skipped and logged once, never deleted.
///
///
public class QuartzRouteJob : IJob
{
/// Key of the logger the context's scheduler holder leaves in the scheduler context.
internal const string LoggerKey = "redb.Route.Quartz.Logger";
private static readonly ConcurrentDictionary Warned = new(StringComparer.Ordinal);
/// Parameterless constructor required by Quartz reflection-based instantiation.
public QuartzRouteJob() { }
///
public async Task Execute(IJobExecutionContext context)
{
var consumer = QuartzConsumerRegistry.Get(context.Scheduler, context.JobDetail.Key);
if (consumer is not null)
{
// The consumer's semaphore + processor pipeline; the route's error scope runs inside it.
await consumer.ExecuteJob(context).ConfigureAwait(false);
return;
}
var metaData = await context.Scheduler.GetMetaData(context.CancellationToken).ConfigureAwait(false);
if (!metaData.JobStoreSupportsPersistence)
{
// In-memory: the job is this process's own and its consumer is gone — clean up job + trigger.
await QuartzConsumerBase.CleanupOrphanedJob(
context.Scheduler, context.JobDetail.Key, context.Trigger.Key).ConfigureAwait(false);
return;
}
if (Warned.TryAdd(QuartzConsumerRegistry.KeyOf(context.Scheduler, context.JobDetail.Key), 0)
&& context.Scheduler.Context.Get(LoggerKey) is ILogger logger)
{
logger.LogWarning(
"Quartz job '{Job}' fired on node {Node} of scheduler {Scheduler}, which runs no consumer for it; the fire is " +
"skipped and the job kept in the store. Nodes that share a scheduler name must carry the same schedules.",
context.JobDetail.Key, context.Scheduler.SchedulerInstanceId, context.Scheduler.SchedulerName);
}
}
}
///
/// Stateful variant of . Persists JobDataMap after execution
/// and disallows concurrent execution (single-threaded at Quartz scheduler level).
/// Used when stateful=true is set on the endpoint.
///
[DisallowConcurrentExecution]
[PersistJobDataAfterExecution]
public class QuartzStatefulRouteJob : QuartzRouteJob
{
}