forked from elsa-workflows/elsa-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'v3' into elasticsearch
# Conflicts: # Elsa.sln
- Loading branch information
Showing
46 changed files
with
822 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
name: Elsa 3 Prerelease | ||
on: | ||
workflow_dispatch: | ||
push: | ||
branches: | ||
- v3 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
src/bundles/Elsa.WorkflowServer.Web/Elsa.WorkflowServer.Web.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
src/modules/Elsa.Common/Exceptions/MissingConfigurationException.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
namespace Elsa.Common.Exceptions; | ||
|
||
/// <summary> | ||
/// Configuration is missing. | ||
/// </summary> | ||
public class MissingConfigurationException : Exception | ||
{ | ||
/// <inheritdoc /> | ||
public MissingConfigurationException(string message) : base(message) | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
using Elsa.Mediator.Services; | ||
using Elsa.Webhooks.Models; | ||
|
||
namespace Elsa.Webhooks.Commands; | ||
|
||
/// <summary> | ||
/// Represents a command to invoke all registered webhook endpoints. | ||
/// </summary> | ||
public record InvokeWebhook(WebhookRegistration WebhookRegistration, WebhookEvent WebhookEvent) : ICommand; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="..\..\..\common.props" /> | ||
<Import Project="..\..\..\configureawait.props" /> | ||
|
||
<PropertyGroup> | ||
<TargetFrameworks>net6.0;net7.0</TargetFrameworks> | ||
<Description> | ||
Provides a way to register webhook endpoints that should be invoked when certain event occur. | ||
</Description> | ||
<PackageTags>elsa, module, webhooks</PackageTags> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Elsa.Workflows.Runtime\Elsa.Workflows.Runtime.csproj" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Microsoft.Extensions.Http" Version="7.0.0" /> | ||
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="7.0.1" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
using Elsa.Features.Services; | ||
using Elsa.Webhooks.Features; | ||
|
||
namespace Elsa.Webhooks.Extensions; | ||
|
||
/// <summary> | ||
/// Adds extensions to <see cref="IModule"/> that enables the <see cref="WebhooksFeature"/> feature. | ||
/// </summary> | ||
public static class ModuleExtensions | ||
{ | ||
/// <summary> | ||
/// Enables and configures the <see cref="WebhooksFeature"/> feature. | ||
/// </summary> | ||
public static IModule UseWebhooks(this IModule module, Action<WebhooksFeature>? configure = default) | ||
{ | ||
module.Configure(configure); | ||
return module; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
using Elsa.Extensions; | ||
using Elsa.Features.Abstractions; | ||
using Elsa.Features.Services; | ||
using Elsa.Webhooks.Implementations; | ||
using Elsa.Webhooks.Models; | ||
using Elsa.Webhooks.Options; | ||
using Elsa.Webhooks.Services; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Polly; | ||
|
||
namespace Elsa.Webhooks.Features; | ||
|
||
/// <summary> | ||
/// Installs and configures services that let the user register webhook endpoints. | ||
/// </summary> | ||
public class WebhooksFeature : FeatureBase | ||
{ | ||
/// <inheritdoc /> | ||
public WebhooksFeature(IModule module) : base(module) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// A delegate that resolves the <see cref="IWebhookDispatcher"/> to use. | ||
/// </summary> | ||
public Func<IServiceProvider, IWebhookDispatcher> WebhookDispatcher { get; set; } = sp => sp.GetRequiredService<BackgroundWebhookDispatcher>(); | ||
|
||
/// <summary> | ||
/// A delegate that is invoked when configuring <see cref="Options.WebhookOptions"/>. | ||
/// </summary> | ||
public Action<WebhookOptions> WebhookOptions { get; set; } = _ => { }; | ||
|
||
/// <summary> | ||
/// A delegate to configure the <see cref="System.Net.Http.HttpClient"/> used when invoking webhook endpoints. | ||
/// </summary> | ||
public Action<IServiceProvider, HttpClient> HttpClient { get; set; } = (_, _) => { }; | ||
|
||
/// <summary> | ||
/// A delegate to configure the <see cref="IHttpClientBuilder"/>. For example, to configure Polly policies. | ||
/// </summary> | ||
public Action<IHttpClientBuilder> HttpClientBuilder { get; set; } = builder => builder.AddTransientHttpErrorPolicy(policy => policy.WaitAndRetryAsync(3, retry => TimeSpan.FromSeconds(retry))); | ||
|
||
/// <summary> | ||
/// Registers the specified webhook with <see cref="Options.WebhookOptions"/> | ||
/// </summary> | ||
public WebhooksFeature RegisterWebhook(Uri endpoint) => RegisterWebhook(new WebhookRegistration(endpoint)); | ||
|
||
/// <summary> | ||
/// Registers the specified webhook with <see cref="Options.WebhookOptions"/> | ||
/// </summary> | ||
public WebhooksFeature RegisterWebhook(WebhookRegistration registration) => RegisterWebhooks(registration); | ||
|
||
/// <summary> | ||
/// Registers the specified webhooks with <see cref="Options.WebhookOptions"/> | ||
/// </summary> | ||
public WebhooksFeature RegisterWebhooks(params WebhookRegistration[] registrations) | ||
{ | ||
Services.Configure<WebhookOptions>(options => options.Endpoints.AddRange(registrations)); | ||
return this; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public override void Apply() | ||
{ | ||
Services | ||
.AddHandlersFrom<WebhooksFeature>() | ||
.AddSingleton<BackgroundWebhookDispatcher>() | ||
.AddSingleton(WebhookDispatcher) | ||
.AddSingleton<IWebhookRegistrationService, DefaultWebhookRegistrationService>() | ||
.AddSingleton<IWebhookRegistrationProvider, OptionsWebhookRegistrationProvider>(); | ||
|
||
Services.Configure(WebhookOptions); | ||
|
||
var httpClientBuilder = Services.AddHttpClient<IWebhookInvoker, HttpWebhookInvoker>(HttpClient); | ||
HttpClientBuilder(httpClientBuilder); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<Weavers xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="FodyWeavers.xsd"> | ||
<ConfigureAwait /> | ||
</Weavers> |
29 changes: 29 additions & 0 deletions
29
src/modules/Elsa.Webhooks/Handlers/InvokeWebhookHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using Elsa.Mediator.Models; | ||
using Elsa.Mediator.Services; | ||
using Elsa.Webhooks.Commands; | ||
using Elsa.Webhooks.Services; | ||
|
||
namespace Elsa.Webhooks.Handlers; | ||
|
||
/// <summary> | ||
/// Handles the <see cref="InvokeWebhook"/> command. | ||
/// </summary> | ||
public class InvokeWebhookHandler : ICommandHandler<InvokeWebhook> | ||
{ | ||
private readonly IWebhookInvoker _webhookInvoker; | ||
|
||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
public InvokeWebhookHandler(IWebhookInvoker webhookInvoker) | ||
{ | ||
_webhookInvoker = webhookInvoker; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task<Unit> HandleAsync(InvokeWebhook command, CancellationToken cancellationToken) | ||
{ | ||
await _webhookInvoker.InvokeWebhookAsync(command.WebhookRegistration, command.WebhookEvent, cancellationToken); | ||
return Unit.Instance; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
using Elsa.Common.Services; | ||
using Elsa.Mediator.Services; | ||
using Elsa.Webhooks.Models; | ||
using Elsa.Webhooks.Services; | ||
using Elsa.Workflows.Runtime.Bookmarks; | ||
using Elsa.Workflows.Runtime.Notifications; | ||
|
||
namespace Elsa.Webhooks.Handlers; | ||
|
||
/// <summary> | ||
/// Handles the <see cref="RunTaskRequest"/> notification and asynchronously invokes all registered webhook endpoints. | ||
/// </summary> | ||
public class RunTaskHandler : INotificationHandler<RunTaskRequest> | ||
{ | ||
private readonly IWebhookDispatcher _webhookDispatcher; | ||
private readonly ISystemClock _systemClock; | ||
|
||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
public RunTaskHandler(IWebhookDispatcher webhookDispatcher, ISystemClock systemClock) | ||
{ | ||
_webhookDispatcher = webhookDispatcher; | ||
_systemClock = systemClock; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task HandleAsync(RunTaskRequest notification, CancellationToken cancellationToken) | ||
{ | ||
var payload = new RunTaskWebhook(notification.TaskId, notification.TaskName, notification.TaskParams); | ||
var now = _systemClock.UtcNow; | ||
var webhookEvent = new WebhookEvent("RunTask", payload, now); | ||
await _webhookDispatcher.DispatchAsync(webhookEvent, cancellationToken); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/modules/Elsa.Webhooks/Implementations/BackgroundWebhookDispatcher.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
using Elsa.Mediator.Services; | ||
using Elsa.Webhooks.Commands; | ||
using Elsa.Webhooks.Models; | ||
using Elsa.Webhooks.Services; | ||
|
||
namespace Elsa.Webhooks.Implementations; | ||
|
||
/// <summary> | ||
/// Uses a background channel to asynchronously invoke each webhook url. | ||
/// </summary> | ||
public class BackgroundWebhookDispatcher : IWebhookDispatcher | ||
{ | ||
private readonly IBackgroundCommandSender _backgroundCommandSender; | ||
private readonly IWebhookRegistrationService _webhookRegistrationService; | ||
|
||
/// <summary> | ||
/// Constructor. | ||
/// </summary> | ||
public BackgroundWebhookDispatcher(IBackgroundCommandSender backgroundCommandSender, IWebhookRegistrationService webhookRegistrationService) | ||
{ | ||
_backgroundCommandSender = backgroundCommandSender; | ||
_webhookRegistrationService = webhookRegistrationService; | ||
} | ||
|
||
/// <inheritdoc /> | ||
public async Task DispatchAsync(WebhookEvent webhookEvent, CancellationToken cancellationToken = default) | ||
{ | ||
var registrations = await _webhookRegistrationService.ListByEventTypeAsync(webhookEvent.EventType, cancellationToken); | ||
|
||
foreach (var registration in registrations) | ||
{ | ||
var notification = new InvokeWebhook(registration, webhookEvent); | ||
await _backgroundCommandSender.SendAsync(notification, cancellationToken); | ||
} | ||
} | ||
} |
Oops, something went wrong.