Refactor scheduling and system clock configurations

Moved specific services from Scoped to Singleton in SchedulingFeature and SystemClockFeature for better performance and consistency. Refactored DefaultWorkflowScheduler to use a constructor with an IScheduler parameter and removed redundant private field. Removed attribute RequiresUnreferencedCode in WorkflowManagementFeature.
This commit is contained in:
Sipke Schoorstra 2024-07-18 16:00:49 +02:00
parent 68c6623555
commit e5ba9a8a9d
4 changed files with 13 additions and 26 deletions

View file

@ -6,9 +6,7 @@ using Microsoft.Extensions.DependencyInjection;
namespace Elsa.Common.Features;
/// <summary>
/// Configures the system clock.
/// </summary>
public class SystemClockFeature : FeatureBase
{
/// <inheritdoc />
@ -19,6 +17,6 @@ public class SystemClockFeature : FeatureBase
/// <inheritdoc />
public override void Apply()
{
Services.AddScoped<ISystemClock, SystemClock>();
Services.AddSingleton<ISystemClock, SystemClock>();
}
}

View file

@ -42,12 +42,12 @@ public class SchedulingFeature : FeatureBase
public override void Apply()
{
Services
.AddSingleton<IScheduler, LocalScheduler>()
.AddSingleton<CronosCronParser>()
.AddSingleton(CronParser)
.AddScoped<ITriggerScheduler, DefaultTriggerScheduler>()
.AddScoped<IBookmarkScheduler, DefaultBookmarkScheduler>()
.AddScoped<IScheduler, LocalScheduler>()
.AddScoped<DefaultWorkflowScheduler>()
.AddScoped<CronosCronParser>()
.AddScoped(CronParser)
.AddScoped(WorkflowScheduler)
.AddHandlersFrom<ScheduleWorkflows>();

View file

@ -8,22 +8,12 @@ namespace Elsa.Scheduling.Services;
/// <summary>
/// A default implementation of <see cref="IWorkflowScheduler"/> that uses the <see cref="LocalScheduler"/>.
/// </summary>
public class DefaultWorkflowScheduler : IWorkflowScheduler
public class DefaultWorkflowScheduler(IScheduler scheduler) : IWorkflowScheduler
{
private readonly IScheduler _scheduler;
/// <summary>
/// Initializes a new instance of the <see cref="DefaultWorkflowScheduler"/> class.
/// </summary>
public DefaultWorkflowScheduler(IScheduler scheduler)
{
_scheduler = scheduler;
}
/// <inheritdoc />
public async ValueTask ScheduleAtAsync(string taskName, DispatchWorkflowDefinitionRequest request, DateTimeOffset at, CancellationToken cancellationToken = default)
{
await _scheduler.ScheduleAsync(taskName, new RunWorkflowTask(request), new SpecificInstantSchedule(at), cancellationToken);
await scheduler.ScheduleAsync(taskName, new RunWorkflowTask(request), new SpecificInstantSchedule(at), cancellationToken);
}
/// <inheritdoc />
@ -31,7 +21,7 @@ public class DefaultWorkflowScheduler : IWorkflowScheduler
{
var task = new ResumeWorkflowTask(request);
var schedule = new SpecificInstantSchedule(at);
await _scheduler.ScheduleAsync(taskName, task, schedule, cancellationToken);
await scheduler.ScheduleAsync(taskName, task, schedule, cancellationToken);
}
/// <inheritdoc />
@ -39,7 +29,7 @@ public class DefaultWorkflowScheduler : IWorkflowScheduler
{
var task = new RunWorkflowTask(request);
var schedule = new RecurringSchedule(startAt, interval);
await _scheduler.ScheduleAsync(taskName, task, schedule, cancellationToken);
await scheduler.ScheduleAsync(taskName, task, schedule, cancellationToken);
}
/// <inheritdoc />
@ -47,7 +37,7 @@ public class DefaultWorkflowScheduler : IWorkflowScheduler
{
var task = new ResumeWorkflowTask(request);
var schedule = new RecurringSchedule(startAt, interval);
await _scheduler.ScheduleAsync(taskName, task, schedule, cancellationToken);
await scheduler.ScheduleAsync(taskName, task, schedule, cancellationToken);
}
/// <inheritdoc />
@ -55,7 +45,7 @@ public class DefaultWorkflowScheduler : IWorkflowScheduler
{
var task = new RunWorkflowTask(request);
var schedule = new CronSchedule(cronExpression);
await _scheduler.ScheduleAsync(taskName, task, schedule, cancellationToken);
await scheduler.ScheduleAsync(taskName, task, schedule, cancellationToken);
}
/// <inheritdoc />
@ -63,12 +53,12 @@ public class DefaultWorkflowScheduler : IWorkflowScheduler
{
var task = new ResumeWorkflowTask(request);
var schedule = new CronSchedule(cronExpression);
await _scheduler.ScheduleAsync(taskName, task, schedule, cancellationToken);
await scheduler.ScheduleAsync(taskName, task, schedule, cancellationToken);
}
/// <inheritdoc />
public async ValueTask UnscheduleAsync(string workflowInstanceId, CancellationToken cancellationToken = default)
public async ValueTask UnscheduleAsync(string taskName, CancellationToken cancellationToken = default)
{
await _scheduler.ClearScheduleAsync(workflowInstanceId, cancellationToken);
await scheduler.ClearScheduleAsync(taskName, cancellationToken);
}
}

View file

@ -101,7 +101,6 @@ public class WorkflowManagementFeature : FeatureBase
/// <summary>
/// Adds all types implementing <see cref="IActivity"/> to the system.
/// </summary>
[RequiresUnreferencedCode("The assembly containing the specified marker type will be scanned for activity types.")]
public WorkflowManagementFeature AddActivitiesFrom<TMarker>()
{
var activityTypes = typeof(TMarker).Assembly.GetExportedTypes()