* Add tenant awareness to bookmark handling and route resolution Added tenant ID support across various components, including bookmark updates, route resolution, and middleware processing. This ensures that bookmark and route operations can now appropriately handle tenant-specific data, improving the system's multitenancy capabilities. * Add Multitenant HTTP Routing feature to Tenants module Introduced a new MultitenantHttpRoutingFeature class to the Elsa.Tenants.AspNetCore module, enhancing the tenant resolution capabilities. Moved RoutePrefixTenantResolver from Elsa.Http to Elsa.Tenants.AspNetCore and updated relevant project references and namespaces accordingly. This refactor improves modularity and separation of concerns between HTTP and tenancy features. * Refactor route handling and tenant configuration Removed redundant `RouteTableExtensions` and replaced with new route providers and updaters, enhancing flexibility and modularity. Introduced tenant-specific HTTP endpoint configurations for better customization and configuration management. * Rename HttpEndpointBookmarkStimulus to HttpEndpointBookmarkPayload Refactor various classes and methods to reflect the renaming from `HttpEndpointBookmarkStimulus` to `HttpEndpointBookmarkPayload`. Add and configure new extension methods for tenant route handling, update the route provider to support multi-tenancy, and adjust the tenants provider to bind configuration properly. * Add HeaderTenantResolver and refactor Http namespace. Introduce HeaderTenantResolver to resolve tenants via HTTP headers. Refactor multiple classes and interfaces to move from the Elsa.Http.Models namespace directly into Elsa.Http for clarity and consistency. * Add Host-based tenant resolution Implemented a HostTenantResolver to resolve tenants based on the request's host and updated tenant configurations with host information. Modified the tenant resolver pipeline and added the new host resolver to the service registrations. * Add tenant-aware caching and accessor support Enhanced caching by incorporating tenant identifiers into cache keys for more granular cache management. Introduced ITenantAccessor dependencies in various services to retrieve the current tenant information. This ensures that cache entries are correctly isolated per tenant. * Reorder tenant resolvers for pipeline setup. Reordered the tenant resolvers in the pipeline to prioritize HostTenantResolver before RoutePrefixTenantResolver. This ensures that tenant resolution is correctly aligned with host-based resolving before checking the route prefix. * Remove unused imports This commit eliminates redundant `using` directives across multiple files to streamline the codebase. This cleanup helps improve code readability and maintainability by removing unnecessary dependencies.
106 lines
4.5 KiB
C#
106 lines
4.5 KiB
C#
using Elsa.Common;
|
|
using Elsa.Extensions;
|
|
using Elsa.Scheduling.Activities;
|
|
using Elsa.Scheduling.Bookmarks;
|
|
using Elsa.Workflows.Models;
|
|
using Elsa.Workflows.Runtime.Entities;
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
namespace Elsa.Scheduling.Services;
|
|
|
|
/// <summary>
|
|
/// A default implementation of <see cref="ITriggerScheduler"/> that schedules triggers using <see cref="IWorkflowScheduler"/>.
|
|
/// </summary>
|
|
public class DefaultTriggerScheduler(IWorkflowScheduler workflowScheduler, ISystemClock systemClock, ILogger<DefaultTriggerScheduler> logger)
|
|
: ITriggerScheduler
|
|
{
|
|
/// <inheritdoc />
|
|
public async Task ScheduleAsync(IEnumerable<StoredTrigger> triggers, CancellationToken cancellationToken = default)
|
|
{
|
|
var triggerList = triggers.ToList();
|
|
var timerTriggers = triggerList.Filter<Activities.Timer>();
|
|
var startAtTriggers = triggerList.Filter<StartAt>();
|
|
var cronTriggers = triggerList.Filter<Cron>();
|
|
var now = systemClock.UtcNow;
|
|
|
|
// Schedule each Timer trigger.
|
|
foreach (var trigger in timerTriggers)
|
|
{
|
|
var (startAt, interval) = trigger.GetPayload<TimerTriggerPayload>();
|
|
var input = new { StartAt = startAt, Interval = interval }.ToDictionary();
|
|
var request = new ScheduleNewWorkflowInstanceRequest
|
|
{
|
|
WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionVersionId(trigger.WorkflowDefinitionVersionId),
|
|
TriggerActivityId = trigger.ActivityId,
|
|
Input = input
|
|
};
|
|
await workflowScheduler.ScheduleRecurringAsync(trigger.Id, request, startAt, interval, cancellationToken);
|
|
}
|
|
|
|
// Schedule each StartAt trigger.
|
|
foreach (var trigger in startAtTriggers)
|
|
{
|
|
var executeAt = trigger.GetPayload<StartAtPayload>().ExecuteAt;
|
|
|
|
// If the trigger is in the past, log info and skip scheduling.
|
|
if (executeAt < now)
|
|
{
|
|
logger.LogInformation("StartAt trigger is in the past. TriggerId: {TriggerId}. ExecuteAt: {ExecuteAt}. Skipping scheduling", trigger.Id, executeAt);
|
|
continue;
|
|
}
|
|
|
|
var input = new { ExecuteAt = executeAt }.ToDictionary();
|
|
var request = new ScheduleNewWorkflowInstanceRequest
|
|
{
|
|
WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionVersionId(trigger.WorkflowDefinitionVersionId),
|
|
TriggerActivityId = trigger.ActivityId,
|
|
Input = input
|
|
};
|
|
|
|
await workflowScheduler.ScheduleAtAsync(trigger.Id, request, executeAt, cancellationToken);
|
|
}
|
|
|
|
// Schedule each Cron trigger.
|
|
foreach (var trigger in cronTriggers)
|
|
{
|
|
var payload = trigger.GetPayload<CronTriggerPayload>();
|
|
var cronExpression = payload.CronExpression;
|
|
|
|
if (string.IsNullOrWhiteSpace(cronExpression))
|
|
{
|
|
logger.LogWarning("Cron expression is empty. TriggerId: {TriggerId}. Skipping scheduling of this trigger", trigger.Id);
|
|
continue;
|
|
}
|
|
|
|
var input = new { CronExpression = cronExpression }.ToDictionary();
|
|
var request = new ScheduleNewWorkflowInstanceRequest
|
|
{
|
|
WorkflowDefinitionHandle = WorkflowDefinitionHandle.ByDefinitionVersionId(trigger.WorkflowDefinitionVersionId),
|
|
TriggerActivityId = trigger.ActivityId,
|
|
Input = input
|
|
};
|
|
try
|
|
{
|
|
await workflowScheduler.ScheduleCronAsync(trigger.Id, request, cronExpression, cancellationToken);
|
|
}
|
|
catch (FormatException ex)
|
|
{
|
|
logger.LogWarning(ex, "Cron expression format error. CronExpression: {CronExpression}", cronExpression);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public async Task UnscheduleAsync(IEnumerable<StoredTrigger> triggers, CancellationToken cancellationToken = default)
|
|
{
|
|
var triggerList = triggers.ToList();
|
|
var timerTriggers = triggerList.Filter<Activities.Timer>();
|
|
var startAtTriggers = triggerList.Filter<StartAt>();
|
|
var cronTriggers = triggerList.Filter<Cron>();
|
|
var filteredTriggers = timerTriggers.Concat(startAtTriggers).Concat(cronTriggers);
|
|
|
|
foreach (var trigger in filteredTriggers)
|
|
await workflowScheduler.UnscheduleAsync(trigger.Id, cancellationToken);
|
|
}
|
|
}
|