Skip to content

Commit

Permalink
Refactor recurring tasks scheduling logic.
Browse files Browse the repository at this point in the history
Removed `ConfigureRecurringTasksScheduleStartupTask` and moved its functionality into `RecurringTaskScheduleManager`. Simplified recurring task configuration and streamlined dependencies, improving maintainability. Added retention policies to `Elsa.Server.Web`.
  • Loading branch information
sfmskywalker committed Jan 2, 2025
1 parent d3af106 commit 6c0f7a4
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 40 deletions.
1 change: 1 addition & 0 deletions src/apps/Elsa.Server.Web/Elsa.Server.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
<ProjectReference Include="..\..\modules\Elsa.MassTransit.AzureServiceBus\Elsa.MassTransit.AzureServiceBus.csproj"/>
<ProjectReference Include="..\..\modules\Elsa.OpenTelemetry\Elsa.OpenTelemetry.csproj" />
<ProjectReference Include="..\..\modules\Elsa.Quartz.EntityFrameworkCore.MySql\Elsa.Quartz.EntityFrameworkCore.MySql.csproj" Condition=" '$(TargetFramework)' != 'net9.0' " />
<ProjectReference Include="..\..\modules\Elsa.Retention\Elsa.Retention.csproj" />
<ProjectReference Include="..\..\modules\Elsa.Secrets.Api\Elsa.Secrets.Api.csproj" />
<ProjectReference Include="..\..\modules\Elsa.Secrets.Persistence.EntityFrameworkCore.PostgreSql\Elsa.Secrets.Persistence.EntityFrameworkCore.PostgreSql.csproj" />
<ProjectReference Include="..\..\modules\Elsa.Secrets.Persistence.EntityFrameworkCore.Sqlite\Elsa.Secrets.Persistence.EntityFrameworkCore.Sqlite.csproj" />
Expand Down
16 changes: 16 additions & 0 deletions src/apps/Elsa.Server.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
using Elsa.MongoDb.Modules.Runtime;
using Elsa.MongoDb.Modules.Tenants;
using Elsa.OpenTelemetry.Middleware;
using Elsa.Retention.Extensions;
using Elsa.Retention.Models;
using Elsa.Secrets.Extensions;
using Elsa.Secrets.Management.Tasks;
using Elsa.Secrets.Persistence;
Expand All @@ -36,6 +38,7 @@
using Elsa.Server.Web.Messages;
using Elsa.Tenants.AspNetCore;
using Elsa.Tenants.Extensions;
using Elsa.Workflows;
using Elsa.Workflows.Api;
using Elsa.Workflows.LogPersistence;
using Elsa.Workflows.Management;
Expand Down Expand Up @@ -513,6 +516,19 @@
.UseSecretsScripting()
;
}

elsa.UseRetention(r =>
{
r.SweepInterval = TimeSpan.FromHours(5);
r.AddDeletePolicy("Delete all finished workflows", sp =>
{
var filter = new RetentionWorkflowInstanceFilter
{
WorkflowStatus = WorkflowStatus.Finished
};
return filter;
});
});

if (useMultitenancy)
{
Expand Down
2 changes: 0 additions & 2 deletions src/modules/Elsa.Common/Features/MultitenancyFeature.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using Elsa.Common.Multitenancy.EventHandlers;
using Elsa.Common.Multitenancy.HostedServices;
using Elsa.Common.RecurringTasks;
using Elsa.Extensions;
using Elsa.Features.Abstractions;
using Elsa.Features.Services;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -52,7 +51,6 @@ public override void Apply()
.AddSingleton<ITenantActivatedEvent, RunStartupTasks>()
.AddSingleton<RecurringTaskScheduleManager>()
.AddSingleton<TenantEventsManager>()
.AddStartupTask<ConfigureRecurringTasksScheduleStartupTask>()
.AddScoped<DefaultTenantsProvider>()
.AddScoped<DefaultTenantResolver>()
.AddScoped<ITaskExecutor, TaskExecutor>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Elsa.Common.Multitenancy.EventHandlers;
public class StartRecurringTasks(RecurringTaskScheduleManager scheduleManager, ILogger<StartRecurringTasks> logger) : ITenantActivatedEvent, ITenantDeactivatedEvent
{
private readonly ICollection<ScheduledTimer> _scheduledTimers = new List<ScheduledTimer>();
private CancellationTokenSource _cancellationTokenSource = default!;
private CancellationTokenSource _cancellationTokenSource = null!;

public async Task TenantActivatedAsync(TenantActivatedEventArgs args)
{
Expand All @@ -16,7 +16,7 @@ public async Task TenantActivatedAsync(TenantActivatedEventArgs args)
var tenantScope = args.TenantScope;
var tasks = tenantScope.ServiceProvider.GetServices<IRecurringTask>().ToList();
var taskExecutor = tenantScope.ServiceProvider.GetRequiredService<ITaskExecutor>();

foreach (var task in tasks)
{
var schedule = scheduleManager.GetScheduleFor(task.GetType());
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
using Cronos;
using Microsoft.Extensions.Options;

namespace Elsa.Common.RecurringTasks;

public class RecurringTaskScheduleManager
public class RecurringTaskScheduleManager(IOptions<RecurringTaskOptions> options, ISystemClock systemClock)
{
public IDictionary<Type, ISchedule> ScheduledTasks { get; set; } = new Dictionary<Type, ISchedule>();

public void ConfigureScheduledTask<T>(ISchedule schedule) where T : IRecurringTask
public ISchedule GetScheduleFor(Type taskType)
{
ConfigureScheduledTask(typeof(T), schedule);
if (!ScheduledTasks.TryGetValue(taskType, out var schedule))
{
var intervalExpression = options.Value.Schedule.ScheduledTasks.TryGetValue(taskType, out var expr) ? expr : null;
schedule = intervalExpression != null ? CreateSchedule(intervalExpression) : new IntervalSchedule(TimeSpan.FromMinutes(1));
ScheduledTasks[taskType] = schedule;
}

return schedule;
}

public void ConfigureScheduledTask(Type recurringTaskType, ISchedule schedule)
{
ScheduledTasks[recurringTaskType] = schedule;
}

public ISchedule GetScheduleFor(Type taskType)
private ISchedule CreateSchedule(IntervalExpression intervalExpression)
{
return ScheduledTasks.TryGetValue(taskType, out var schedule) ? schedule : new IntervalSchedule(TimeSpan.FromMinutes(1));
return intervalExpression.Type switch
{
IntervalExpressionType.Cron => new CronSchedule(systemClock, CronExpression.Parse(intervalExpression.Expression)),
IntervalExpressionType.Interval => new IntervalSchedule(TimeSpan.Parse(intervalExpression.Expression)),
_ => throw new NotSupportedException($"Interval expression type '{intervalExpression.Type}' is not supported.")
};
}
}

0 comments on commit 6c0f7a4

Please sign in to comment.