Add project files.
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
using Elsa.Alterations.Core.Contracts;
|
||||
using Elsa.Alterations.MassTransit.Messages;
|
||||
using MassTransit;
|
||||
|
||||
namespace Elsa.Alterations.MassTransit.Consumers;
|
||||
|
||||
/// <summary>
|
||||
/// Consumes <see cref="RunAlterationJob"/> messages.
|
||||
/// </summary>
|
||||
public class RunAlterationJobConsumer : IConsumer<RunAlterationJob>
|
||||
{
|
||||
private readonly IAlterationJobRunner _alterationJobRunner;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="RunAlterationJobConsumer"/> class.
|
||||
/// </summary>
|
||||
public RunAlterationJobConsumer(IAlterationJobRunner alterationJobRunner)
|
||||
{
|
||||
_alterationJobRunner = alterationJobRunner;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task Consume(ConsumeContext<RunAlterationJob> context)
|
||||
{
|
||||
await _alterationJobRunner.RunAsync(context.Message.JobId, context.CancellationToken);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<Description>
|
||||
Provides alteration background job runners using MassTransit.
|
||||
</Description>
|
||||
<PackageTags>elsa module alterations masstransit</PackageTags>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Elsa.Alterations\Elsa.Alterations.csproj" />
|
||||
<ProjectReference Include="..\Elsa.MassTransit\Elsa.MassTransit.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
@@ -0,0 +1,20 @@
|
||||
using Elsa.Alterations.Features;
|
||||
using Elsa.Alterations.MassTransit.Features;
|
||||
using Elsa.Extensions;
|
||||
using Elsa.Features.Services;
|
||||
|
||||
namespace Elsa.Alterations.MassTransit.Extensions;
|
||||
|
||||
/// <summary>
|
||||
/// Adds the <see cref="AlterationsFeature"/>.
|
||||
/// </summary>
|
||||
public static class ModuleExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Adds the <see cref="MassTransitAlterationsFeature"/>.
|
||||
/// </summary>
|
||||
public static IModule UseMassTransitDispatcher(this AlterationsFeature alterations, Action<MassTransitAlterationsFeature>? configure = default)
|
||||
{
|
||||
return alterations.Module.Use(configure);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
using Elsa.Alterations.Features;
|
||||
using Elsa.Alterations.MassTransit.Consumers;
|
||||
using Elsa.Alterations.MassTransit.Messages;
|
||||
using Elsa.Alterations.MassTransit.Services;
|
||||
using Elsa.Extensions;
|
||||
using Elsa.Features.Abstractions;
|
||||
using Elsa.Features.Attributes;
|
||||
using Elsa.Features.Services;
|
||||
using Elsa.MassTransit.Features;
|
||||
using MassTransit;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Elsa.Alterations.MassTransit.Features;
|
||||
|
||||
/// <summary>
|
||||
/// A feature for enabling the Alterations MassTransit feature.
|
||||
/// </summary>
|
||||
[DependsOn(typeof(MassTransitFeature))]
|
||||
[DependsOn(typeof(AlterationsFeature))]
|
||||
public class MassTransitAlterationsFeature : FeatureBase
|
||||
{
|
||||
/// <inheritdoc />
|
||||
public MassTransitAlterationsFeature(IModule module) : base(module)
|
||||
{
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Configure()
|
||||
{
|
||||
Module.Configure<AlterationsFeature>(feature => feature.AlterationJobDispatcherFactory = sp => sp.GetRequiredService<MassTransitAlterationJobDispatcher>());
|
||||
var queueName = KebabCaseEndpointNameFormatter.Instance.Consumer<RunAlterationJobConsumer>();
|
||||
var queueAddress = new Uri($"queue:elsa-{queueName}");
|
||||
EndpointConvention.Map<RunAlterationJob>(queueAddress);
|
||||
Module.AddMassTransitConsumer<RunAlterationJobConsumer>(queueName);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public override void Apply()
|
||||
{
|
||||
Services.AddScoped<MassTransitAlterationJobDispatcher>();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
<Weavers xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd">
|
||||
<ConfigureAwait />
|
||||
</Weavers>
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Elsa.Alterations.MassTransit.Messages;
|
||||
|
||||
/// <summary>
|
||||
/// Represents a request to run an alteration job.
|
||||
/// </summary>
|
||||
/// <param name="JobId">The job ID.</param>
|
||||
public record RunAlterationJob(string JobId);
|
||||
@@ -0,0 +1,4 @@
|
||||
# MassTransit Alterations Background Runner
|
||||
|
||||
This module provides an implementation that processes alteration plans in the background using a MassTransit dispatcher and consumer.
|
||||
This implementation is more resilient than the in-memory queue implementation provided by **Elsa.Alterations.BackgroundRunner** because it uses a message broker to persist messages.
|
||||
+28
@@ -0,0 +1,28 @@
|
||||
using Elsa.Alterations.Core.Contracts;
|
||||
using Elsa.Alterations.MassTransit.Messages;
|
||||
using MassTransit;
|
||||
|
||||
namespace Elsa.Alterations.MassTransit.Services;
|
||||
|
||||
/// <summary>
|
||||
/// Dispatches an alteration job for execution using MassTransit.
|
||||
/// </summary>
|
||||
public class MassTransitAlterationJobDispatcher : IAlterationJobDispatcher
|
||||
{
|
||||
private readonly IBus _bus;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="MassTransitAlterationJobDispatcher"/> class.
|
||||
/// </summary>
|
||||
public MassTransitAlterationJobDispatcher(IBus bus)
|
||||
{
|
||||
_bus = bus;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async ValueTask DispatchAsync(string jobId, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var message = new RunAlterationJob(jobId);
|
||||
await _bus.Send(message, cancellationToken);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user