From e5ba9a8a9d31541ad24fa0ea0853cce5c4cc9601 Mon Sep 17 00:00:00 2001 From: Sipke Schoorstra Date: Thu, 18 Jul 2024 16:00:49 +0200 Subject: [PATCH] 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. --- .../Features/SystemClockFeature.cs | 4 +-- .../Features/SchedulingFeature.cs | 6 ++-- .../Services/DefaultWorkflowScheduler.cs | 28 ++++++------------- .../Features/WorkflowManagementFeature.cs | 1 - 4 files changed, 13 insertions(+), 26 deletions(-) diff --git a/src/modules/Elsa.Common/Features/SystemClockFeature.cs b/src/modules/Elsa.Common/Features/SystemClockFeature.cs index 027319e11..04aea968f 100644 --- a/src/modules/Elsa.Common/Features/SystemClockFeature.cs +++ b/src/modules/Elsa.Common/Features/SystemClockFeature.cs @@ -6,9 +6,7 @@ using Microsoft.Extensions.DependencyInjection; namespace Elsa.Common.Features; -/// /// Configures the system clock. -/// public class SystemClockFeature : FeatureBase { /// @@ -19,6 +17,6 @@ public class SystemClockFeature : FeatureBase /// public override void Apply() { - Services.AddScoped(); + Services.AddSingleton(); } } \ No newline at end of file diff --git a/src/modules/Elsa.Scheduling/Features/SchedulingFeature.cs b/src/modules/Elsa.Scheduling/Features/SchedulingFeature.cs index 368ab9ba2..19c634c6a 100644 --- a/src/modules/Elsa.Scheduling/Features/SchedulingFeature.cs +++ b/src/modules/Elsa.Scheduling/Features/SchedulingFeature.cs @@ -42,12 +42,12 @@ public class SchedulingFeature : FeatureBase public override void Apply() { Services + .AddSingleton() + .AddSingleton() + .AddSingleton(CronParser) .AddScoped() .AddScoped() - .AddScoped() .AddScoped() - .AddScoped() - .AddScoped(CronParser) .AddScoped(WorkflowScheduler) .AddHandlersFrom(); diff --git a/src/modules/Elsa.Scheduling/Services/DefaultWorkflowScheduler.cs b/src/modules/Elsa.Scheduling/Services/DefaultWorkflowScheduler.cs index d1a8d22f3..6bde761c2 100644 --- a/src/modules/Elsa.Scheduling/Services/DefaultWorkflowScheduler.cs +++ b/src/modules/Elsa.Scheduling/Services/DefaultWorkflowScheduler.cs @@ -8,22 +8,12 @@ namespace Elsa.Scheduling.Services; /// /// A default implementation of that uses the . /// -public class DefaultWorkflowScheduler : IWorkflowScheduler +public class DefaultWorkflowScheduler(IScheduler scheduler) : IWorkflowScheduler { - private readonly IScheduler _scheduler; - - /// - /// Initializes a new instance of the class. - /// - public DefaultWorkflowScheduler(IScheduler scheduler) - { - _scheduler = scheduler; - } - /// 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); } /// @@ -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); } /// @@ -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); } /// @@ -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); } /// @@ -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); } /// @@ -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); } /// - 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); } } \ No newline at end of file diff --git a/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs b/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs index cfb45929c..3fdc5adf8 100644 --- a/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs +++ b/src/modules/Elsa.Workflows.Management/Features/WorkflowManagementFeature.cs @@ -101,7 +101,6 @@ public class WorkflowManagementFeature : FeatureBase /// /// Adds all types implementing to the system. /// - [RequiresUnreferencedCode("The assembly containing the specified marker type will be scanned for activity types.")] public WorkflowManagementFeature AddActivitiesFrom() { var activityTypes = typeof(TMarker).Assembly.GetExportedTypes()