Refactor scheduling feature: remove unused CronParser, improve error handling in ScheduledRecurringTask.

This commit is contained in:
Sipke Schoorstra 2025-10-03 20:46:29 +02:00
parent a375e5613a
commit c586c690d3
No known key found for this signature in database
GPG key ID: 5C10502B28A4268F
2 changed files with 37 additions and 30 deletions

View file

@ -48,7 +48,6 @@ public class SchedulingFeature : FeatureBase
.AddScoped<ITriggerScheduler, DefaultTriggerScheduler>()
.AddScoped<IBookmarkScheduler, DefaultBookmarkScheduler>()
.AddScoped<DefaultWorkflowScheduler>()
.AddSingleton(CronParser)
.AddScoped(WorkflowScheduler)
.AddBackgroundTask<CreateSchedulesBackgroundTask>()
.AddHandlersFrom<ScheduleWorkflows>()

View file

@ -86,41 +86,49 @@ public class ScheduledRecurringTask : IScheduledTask, IDisposable
_timer.Elapsed += async (_, _) =>
{
_timer?.Dispose();
_timer = null;
_startAt = _systemClock.UtcNow + _interval;
using var scope = _scopeFactory.CreateScope();
var commandSender = scope.ServiceProvider.GetRequiredService<ICommandSender>();
var cancellationToken = _cancellationTokenSource.Token;
if (!cancellationToken.IsCancellationRequested)
try
{
try
{
var acquired = await _executionSemaphore.WaitAsync(0, cancellationToken);
if (!acquired) return;
_executing = true;
await commandSender.SendAsync(new RunScheduledTask(_task), cancellationToken);
if (_cancellationRequested)
_timer?.Dispose();
_timer = null;
_startAt = _systemClock.UtcNow + _interval;
using var scope = _scopeFactory.CreateScope();
var commandSender = scope.ServiceProvider.GetRequiredService<ICommandSender>();
var cancellationToken = _cancellationTokenSource.Token;
if (!cancellationToken.IsCancellationRequested)
{
try
{
_cancellationRequested = false;
_cancellationTokenSource.Cancel();
var acquired = await _executionSemaphore.WaitAsync(0, cancellationToken);
if (!acquired) return;
_executing = true;
await commandSender.SendAsync(new RunScheduledTask(_task), cancellationToken);
if (_cancellationRequested)
{
_cancellationRequested = false;
_cancellationTokenSource.Cancel();
}
}
catch (Exception e)
{
_logger.LogError(e, "Error executing scheduled task");
}
finally
{
_executing = false;
_executionSemaphore.Release();
}
}
catch (Exception e)
{
_logger.LogError(e, "Error executing scheduled task");
}
finally
{
_executing = false;
_executionSemaphore.Release();
}
}
if (!cancellationToken.IsCancellationRequested)
Schedule();
if (!cancellationToken.IsCancellationRequested)
Schedule();
}
catch (ObjectDisposedException ex)
{
_logger.LogWarning(ex, "Service Provider was disposed.");
}
};
}