Repository navigation
WithMaxConcurrency(1) leaves overlapping cron occurrences in InProgress #917
DescriptionWhen a cron job is configured with Only one occurrence enters I expected one of the following behaviors:
However, Configurationvar kafkaOutboxDispatcherJobOptions =
builder.AddOptionsSectionWithValidation<KafkaOutboxDispatcherJobOptions>($"CronJobs:{KafkaOutboxDispatcherJobOptions.SectionName}");
builder.Services.MapTicker<KafkaOutboxDispatcherJob>()
.WithMaxConcurrency(1)
.WithPriority(TickerTaskPriority.Normal)
.WithCron(kafkaOutboxDispatcherJobOptions.Cron);Job implementationpublic sealed class KafkaOutboxDispatcherJob(
IKafkaOutboxRepository kafkaOutboxRepository,
IKafkaPublisher kafkaPublisher,
IOptions<KafkaOutboxDispatcherJobOptions> kafkaOutboxDispatcherJobOptions,
ILogger<KafkaOutboxDispatcherJob> logger
) : ITickerFunction
{
public async Task ExecuteAsync(
TickerFunctionContext context,
CancellationToken cancellationToken = default)
{
context.CronOccurrenceOperations.SkipIfAlreadyRunning();
await Task.Delay(100_000, cancellationToken);
try
{
await DispatchOnce(cancellationToken);
}
catch (Exception ex) when (ex is not TerminateExecutionException)
{
logger.LogError(ex, "Kafka outbox tick failed");
}
}
}Steps to reproduce
Actual behavior
Expected behaviorWith Occurrences waiting for the concurrency slot should remain Questions
|
Replies: 1 comment 1 reply
|
Yes, that's how the current code behaves. In That ordering is also why If you want overlapping occurrences skipped, drop builder.Services.MapTicker<KafkaOutboxDispatcherJob>()
.WithCron(options.Cron); // no WithMaxConcurrencyOne caveat: that registry is a static in-process dictionary, so the skip only sees overlaps on the same node. If you run several instances, you'd still need locking in your own code. Moving the |
Yes, that's how the current code behaves.
WithMaxConcurrencyonly limits handler executions, and the status flips before the slot is acquired.In
TickerQSchedulerBackgroundService, the loop callsSetTickersInProgress(...)for every due occurrence first. Only then does it queue each one behind the per-functionSemaphoreSlim(maxConcurrency). So anything waiting on the semaphore is alreadyInProgressin storage.That ordering is also why
SkipIfAlreadyRunning()never fires in your setup. It checksTickerCancellationTokenManager.IsParentRunningExcludingSelf(parentId, self), and an occurrence is only registered there after it has passed the semaphore and enteredRunContextFunctionAsync. With ma…