using Elsa.Common.Features;
using Elsa.Common.Multitenancy;
using Elsa.Extensions;
using Elsa.Features.Abstractions;
using Elsa.Features.Attributes;
using Elsa.Features.Services;
using Elsa.Scheduling.Bookmarks;
using Elsa.Scheduling.Handlers;
using Elsa.Scheduling.Options;
using Elsa.Scheduling.Services;
using Elsa.Scheduling.StartupTasks;
using Elsa.Scheduling.TriggerPayloadValidators;
using Elsa.Workflows.Management.Features;
using Elsa.Workflows.Options;
using Microsoft.Extensions.DependencyInjection;
using Elsa.Common.Serialization;
namespace Elsa.Scheduling.Features;
///
/// Provides scheduling features to the system.
///
[DependsOn(typeof(SystemClockFeature))]
public class SchedulingFeature : FeatureBase
{
///
public SchedulingFeature(IModule module) : base(module)
{
}
///
/// Gets or sets the trigger scheduler.
///
public Func WorkflowScheduler { get; set; } = sp => sp.GetRequiredService();
///
/// Gets or sets the CRON parser.
///
public Func CronParser { get; set; } = sp => sp.GetRequiredService();
///
public override void Apply()
{
Services
.AddSingleton()
.AddSingleton(sp => sp.GetRequiredService())
.AddSingleton()
.AddSingleton()
.AddSingleton()
.AddSingleton(CronParser)
.AddScoped()
.AddScoped()
.AddScoped()
.AddScoped(WorkflowScheduler)
.AddStartupTask()
.AddHandlersFrom()
//Trigger payload validators.
.AddTriggerPayloadValidator()
// Graceful shutdown: register scheduled-trigger ingress for diagnostic visibility (FR-006).
.AddSingleton();
Services.Configure(_ => { });
Services.Configure(options =>
{
options.RegisterTypeAlias(typeof(CronBookmarkPayload), nameof(CronBookmarkPayload));
options.RegisterTypeAlias(typeof(CronTriggerPayload), nameof(CronTriggerPayload));
options.RegisterTypeAlias(typeof(DelayPayload), nameof(DelayPayload));
options.RegisterTypeAlias(typeof(StartAtPayload), nameof(StartAtPayload));
options.RegisterTypeAlias(typeof(TimerBookmarkPayload), nameof(TimerBookmarkPayload));
options.RegisterTypeAlias(typeof(TimerTriggerPayload), nameof(TimerTriggerPayload));
});
Module.Configure(management => management.AddActivitiesFrom());
}
}