Skip to content

Commit

Permalink
Refactor scheduling and system clock configurations
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
sfmskywalker committed Jul 18, 2024
1 parent 68c6623 commit e5ba9a8
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 26 deletions.
4 changes: 1 addition & 3 deletions src/modules/Elsa.Common/Features/SystemClockFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@

namespace Elsa.Common.Features;

/// <summary>
/// Configures the system clock.
/// </summary>
public class SystemClockFeature : FeatureBase
{
/// <inheritdoc />
Expand All @@ -19,6 +17,6 @@ public SystemClockFeature(IModule module) : base(module)
/// <inheritdoc />
public override void Apply()
{
Services.AddScoped<ISystemClock, SystemClock>();
Services.AddSingleton<ISystemClock, SystemClock>();
}
}
6 changes: 3 additions & 3 deletions src/modules/Elsa.Scheduling/Features/SchedulingFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ public override void ConfigureHostedServices()
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>();

Expand Down
28 changes: 9 additions & 19 deletions src/modules/Elsa.Scheduling/Services/DefaultWorkflowScheduler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,67 +8,57 @@ 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 />
public async ValueTask ScheduleAtAsync(string taskName, DispatchWorkflowInstanceRequest request, DateTimeOffset at, CancellationToken cancellationToken = default)
{
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 />
public async ValueTask ScheduleRecurringAsync(string taskName, DispatchWorkflowDefinitionRequest request, DateTimeOffset startAt, TimeSpan interval, CancellationToken cancellationToken = default)
{
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 />
public async ValueTask ScheduleRecurringAsync(string taskName, DispatchWorkflowInstanceRequest request, DateTimeOffset startAt, TimeSpan interval, CancellationToken cancellationToken = default)
{
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 />
public async ValueTask ScheduleCronAsync(string taskName, DispatchWorkflowDefinitionRequest request, string cronExpression, CancellationToken cancellationToken = default)
{
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 />
public async ValueTask ScheduleCronAsync(string taskName, DispatchWorkflowInstanceRequest request, string cronExpression, CancellationToken cancellationToken = default)
{
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,6 @@ public WorkflowManagementFeature AddActivity(Type activityType)
/// <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()
Expand Down

0 comments on commit e5ba9a8

Please sign in to comment.