├── artwork ├── freakout.png ├── freakout.webp ├── transparent_2024-04-11T18-52-17.png └── transparent_2024-04-11T18-52-17_cropped.png ├── tools └── NuGet │ └── nuget.exe ├── Freakout ├── Internals │ ├── EmptyFreakoutContext.cs │ ├── InternalsVisibleTo.cs │ ├── IsExternalInit.cs │ ├── DelegatingCommandHandler.cs │ ├── AsyncLocalFreakoutContextAccessor.cs │ ├── InternalExtensions.cs │ ├── DefaultBatchDispatcher.cs │ ├── CommandDispatcher.cs │ ├── Dispatchers │ │ ├── CompiledExpressionCommandDispatcher.cs │ │ └── IlEmitCommandDispatcher.cs │ ├── TypeExtensions.cs │ └── FreakoutBackgroundService.cs ├── IFreakoutContext.cs ├── OutboxCommand.cs ├── HeaderKeys.cs ├── IContextHooks.cs ├── IOutboxCommandStore.cs ├── IBatchDispatcher.cs ├── ICommandDispatcher.cs ├── ICommandHandler.cs ├── IOutbox.cs ├── ICommandSerializer.cs ├── IFreakoutContextAccessor.cs ├── Serialization │ ├── HeaderSerializer.cs │ └── SystemTextJsonCommandSerializer.cs ├── Freakout.csproj ├── PendingOutboxCommand.cs ├── FreakoutConfiguration.cs ├── FreakoutContextScope.cs ├── OutboxCommandBatch.cs └── Config │ └── FreakoutServiceCollectionExtensions.cs ├── Freakout.MsSql ├── Internals │ ├── InternalsVisibleTo.cs │ ├── IsExternalInit.cs │ ├── InternalExtensions.cs │ ├── MsSqlOutbox.cs │ └── MsSqlOutboxCommandStore.cs ├── PostgresCommandHelper.cs ├── MsSqlFreakoutContext.cs ├── Freakout.MsSql.csproj ├── MsSqlFreakoutConfiguration.cs └── FreakoutSqlConnectionExtensions.cs ├── Freakout.NpgSql ├── Internals │ ├── IsExternalInit.cs │ ├── InternalExtensions.cs │ ├── NpgSqlOutbox.cs │ └── NpgSqlOutboxCommandStore.cs ├── NpgsqlFreakoutContext.cs ├── Freakout.NpgSql.csproj ├── NpgSqlFreakoutConfiguration.cs └── FreakoutNpgsqlConnectionExtensions.cs ├── Freakout.Testing ├── Internals │ ├── IsExternalInit.cs │ ├── InMemOutboxCommandStore.cs │ ├── InMemOutboxDecorator.cs │ └── InMemOutbox.cs ├── InMemOutboxCommand.cs ├── InMemFreakoutContext.cs ├── Freakout.Testing.csproj └── InMemFreakoutConfiguration.cs ├── Freakout.MsSql.Tests ├── Contracts │ ├── MsSqlNormalTests.cs │ ├── MsSqlExtensibilityTests.cs │ └── MsSqlOutboxCommandStoreTests.cs ├── Freakout.MsSql.Tests.csproj ├── MsSqlTestHelper.cs ├── MsSqlFreakoutSystemFactory.cs ├── ThisIsWhatWeWant.cs └── SimpleSqlServerPoc.cs ├── Freakout.NpgSql.Tests ├── Contracts │ ├── NpgSqlNormalTests.cs │ ├── NpgsqlExtensibilityTests.cs │ └── NpgsqlOutboxCommandStoreTests.cs ├── Freakout.NpgSql.Tests.csproj ├── NpgSqlTestHelper.cs └── NpgSqlFreakoutSystemFactory.cs ├── Freakout.Marten ├── FreakoutMartenConfigurationExtensions.cs ├── Freakout.Marten.csproj └── MartenOutboxExtensions.cs ├── Freakout.Tests ├── Contracts │ ├── IFreakoutSystemFactory.cs │ ├── AbstractFreakoutSystemFactory.cs │ ├── FreakoutSystem.cs │ ├── ExtensibilityTests.cs │ ├── OutboxCommandStoreTests.cs │ └── NormalTests.cs ├── Freakout.Tests.csproj ├── TestExtensions.cs ├── TestFreakoutContextScope.cs └── Dispatch │ └── TestCommandDispatcher.cs ├── Freakout.sln.DotSettings ├── Freakout.Testing.Tests ├── Freakout.Testing.Tests.csproj └── CheckHowItLooks.cs ├── scripts ├── build.cmd ├── push.cmd └── release.cmd ├── LICENSE ├── CHANGELOG.md ├── README.md ├── Freakout.sln └── .gitignore /artwork/freakout.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rebus-org/Freakout/HEAD/artwork/freakout.png -------------------------------------------------------------------------------- /artwork/freakout.webp: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rebus-org/Freakout/HEAD/artwork/freakout.webp -------------------------------------------------------------------------------- /tools/NuGet/nuget.exe: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rebus-org/Freakout/HEAD/tools/NuGet/nuget.exe -------------------------------------------------------------------------------- /Freakout/Internals/EmptyFreakoutContext.cs: -------------------------------------------------------------------------------- 1 | namespace Freakout.Internals; 2 | 3 | class EmptyFreakoutContext : IFreakoutContext; -------------------------------------------------------------------------------- /artwork/transparent_2024-04-11T18-52-17.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rebus-org/Freakout/HEAD/artwork/transparent_2024-04-11T18-52-17.png -------------------------------------------------------------------------------- /Freakout.MsSql/Internals/InternalsVisibleTo.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | [assembly: InternalsVisibleTo("Freakout.MsSql.Tests")] -------------------------------------------------------------------------------- /artwork/transparent_2024-04-11T18-52-17_cropped.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/rebus-org/Freakout/HEAD/artwork/transparent_2024-04-11T18-52-17_cropped.png -------------------------------------------------------------------------------- /Freakout/IFreakoutContext.cs: -------------------------------------------------------------------------------- 1 | namespace Freakout; 2 | 3 | /// 4 | /// Marker interface for an ambient Freakout context 5 | /// 6 | public interface IFreakoutContext; -------------------------------------------------------------------------------- /Freakout/Internals/InternalsVisibleTo.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.CompilerServices; 2 | 3 | [assembly: InternalsVisibleTo("Freakout.Tests")] 4 | 5 | [assembly: InternalsVisibleTo("Freakout.MsSql")] -------------------------------------------------------------------------------- /Freakout/Internals/IsExternalInit.cs: -------------------------------------------------------------------------------- 1 | // ReSharper disable CheckNamespace 2 | // ReSharper disable UnusedMember.Global 3 | namespace System.Runtime.CompilerServices; 4 | 5 | class IsExternalInit; 6 | -------------------------------------------------------------------------------- /Freakout.MsSql/Internals/IsExternalInit.cs: -------------------------------------------------------------------------------- 1 | // ReSharper disable CheckNamespace 2 | // ReSharper disable UnusedMember.Global 3 | namespace System.Runtime.CompilerServices; 4 | 5 | class IsExternalInit; 6 | -------------------------------------------------------------------------------- /Freakout.NpgSql/Internals/IsExternalInit.cs: -------------------------------------------------------------------------------- 1 | // ReSharper disable CheckNamespace 2 | // ReSharper disable UnusedMember.Global 3 | namespace System.Runtime.CompilerServices; 4 | 5 | class IsExternalInit; 6 | -------------------------------------------------------------------------------- /Freakout.Testing/Internals/IsExternalInit.cs: -------------------------------------------------------------------------------- 1 | // ReSharper disable CheckNamespace 2 | // ReSharper disable UnusedMember.Global 3 | namespace System.Runtime.CompilerServices; 4 | 5 | class IsExternalInit; 6 | -------------------------------------------------------------------------------- /Freakout.MsSql/PostgresCommandHelper.cs: -------------------------------------------------------------------------------- 1 | namespace Freakout.MsSql; 2 | 3 | /// 4 | /// Useful bits of actual PostgreSQL command generation are made accessible here 5 | /// 6 | public static class PostgresCommandHelper 7 | { 8 | 9 | } -------------------------------------------------------------------------------- /Freakout.MsSql.Tests/Contracts/MsSqlNormalTests.cs: -------------------------------------------------------------------------------- 1 | using Freakout.Tests.Contracts; 2 | using NUnit.Framework; 3 | 4 | namespace Freakout.MsSql.Tests.Contracts; 5 | 6 | [TestFixture] 7 | public class MsSqlNormalTests : NormalTests; -------------------------------------------------------------------------------- /Freakout/OutboxCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Freakout; 4 | 5 | /// 6 | /// Raw store command before being persisted 7 | /// 8 | public record OutboxCommand(Dictionary Headers, byte[] Payload); -------------------------------------------------------------------------------- /Freakout.NpgSql.Tests/Contracts/NpgSqlNormalTests.cs: -------------------------------------------------------------------------------- 1 | using Freakout.Tests.Contracts; 2 | using NUnit.Framework; 3 | 4 | namespace Freakout.NpgSql.Tests.Contracts; 5 | 6 | [TestFixture] 7 | public class NpgsqlNormalTests : NormalTests; -------------------------------------------------------------------------------- /Freakout.MsSql.Tests/Contracts/MsSqlExtensibilityTests.cs: -------------------------------------------------------------------------------- 1 | using Freakout.Tests.Contracts; 2 | using NUnit.Framework; 3 | 4 | namespace Freakout.MsSql.Tests.Contracts; 5 | 6 | [TestFixture] 7 | public class MsSqlExtensibilityTests : ExtensibilityTests; -------------------------------------------------------------------------------- /Freakout.NpgSql.Tests/Contracts/NpgsqlExtensibilityTests.cs: -------------------------------------------------------------------------------- 1 | using Freakout.Tests.Contracts; 2 | using NUnit.Framework; 3 | 4 | namespace Freakout.NpgSql.Tests.Contracts; 5 | 6 | [TestFixture] 7 | public class NpgsqlExtensibilityTests : ExtensibilityTests; -------------------------------------------------------------------------------- /Freakout.MsSql.Tests/Contracts/MsSqlOutboxCommandStoreTests.cs: -------------------------------------------------------------------------------- 1 | using Freakout.Tests.Contracts; 2 | using NUnit.Framework; 3 | 4 | namespace Freakout.MsSql.Tests.Contracts; 5 | 6 | [TestFixture] 7 | public class MsSqlOutboxCommandStoreTests : OutboxCommandStoreTests; -------------------------------------------------------------------------------- /Freakout.NpgSql.Tests/Contracts/NpgsqlOutboxCommandStoreTests.cs: -------------------------------------------------------------------------------- 1 | using Freakout.Tests.Contracts; 2 | using NUnit.Framework; 3 | 4 | namespace Freakout.NpgSql.Tests.Contracts; 5 | 6 | [TestFixture] 7 | public class NpgsqlOutboxCommandStoreTests : OutboxCommandStoreTests; -------------------------------------------------------------------------------- /Freakout.Testing/InMemOutboxCommand.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace Freakout.Testing; 4 | 5 | /// 6 | /// Holds the information of an in-mem outbox command. 7 | /// 8 | public record InMemOutboxCommand(Dictionary Headers, object Command); -------------------------------------------------------------------------------- /Freakout.Marten/FreakoutMartenConfigurationExtensions.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.DependencyInjection; 2 | 3 | namespace Freakout.Marten; 4 | 5 | public static class FreakoutMartenConfigurationExtensions 6 | { 7 | public static void AddFreakoutMartenIntegration(this IServiceCollection services) 8 | { 9 | 10 | } 11 | } -------------------------------------------------------------------------------- /Freakout.Tests/Contracts/IFreakoutSystemFactory.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Extensions.DependencyInjection; 2 | using System; 3 | 4 | namespace Freakout.Tests.Contracts; 5 | 6 | public interface IFreakoutSystemFactory : IDisposable 7 | { 8 | FreakoutSystem Create(Action before = null, Action after = null); 9 | } -------------------------------------------------------------------------------- /Freakout.Testing/Internals/InMemOutboxCommandStore.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously 4 | 5 | namespace Freakout.Testing.Internals; 6 | 7 | class InMemOutboxCommandStore : IOutboxCommandStore 8 | { 9 | public async Task GetPendingOutboxCommandsAsync(int commandProcessingBatchSize, CancellationToken cancellationToken = default) => OutboxCommandBatch.Empty; 10 | } -------------------------------------------------------------------------------- /Freakout.sln.DotSettings: -------------------------------------------------------------------------------- 1 | 2 | server=(localdb)\MSSQLLocalDb; database=freakout_test; trusted_connection=true; encrypt=false -------------------------------------------------------------------------------- /Freakout/Internals/DelegatingCommandHandler.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | 5 | namespace Freakout.Internals; 6 | 7 | /// 8 | /// Built-in generic command handler that just delegates its invocation to the given function. 9 | /// 10 | class DelegatingCommandHandler(Func invoker) : ICommandHandler 11 | { 12 | public Task HandleAsync(TCommand command, CancellationToken cancellationToken) => invoker(command, cancellationToken); 13 | } -------------------------------------------------------------------------------- /Freakout/HeaderKeys.cs: -------------------------------------------------------------------------------- 1 | namespace Freakout; 2 | 3 | /// 4 | /// Keys of headers that have special meaning in Freakout. Please use only if you know what you're doing ;) 5 | /// 6 | public static class HeaderKeys 7 | { 8 | /// 9 | /// Type information for the (de)serializer to use to be able to construct a command object 10 | /// 11 | public const string CommandType = "cmd-type"; 12 | 13 | /// 14 | /// MIME type of the serialized payload 15 | /// 16 | public const string ContentType = "content-type"; 17 | } -------------------------------------------------------------------------------- /Freakout.MsSql/Internals/InternalExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Freakout.MsSql.Internals; 5 | 6 | static class InternalExtensions 7 | { 8 | public static void InsertInto(this Dictionary source, Dictionary target) 9 | { 10 | if (source == null) throw new ArgumentNullException(nameof(source)); 11 | if (target == null) throw new ArgumentNullException(nameof(target)); 12 | 13 | foreach (var kvp in source) 14 | { 15 | target[kvp.Key] = kvp.Value; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Freakout.NpgSql/Internals/InternalExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Freakout.NpgSql.Internals; 5 | 6 | static class InternalExtensions 7 | { 8 | public static void InsertInto(this Dictionary source, Dictionary target) 9 | { 10 | if (source == null) throw new ArgumentNullException(nameof(source)); 11 | if (target == null) throw new ArgumentNullException(nameof(target)); 12 | 13 | foreach (var kvp in source) 14 | { 15 | target[kvp.Key] = kvp.Value; 16 | } 17 | } 18 | } -------------------------------------------------------------------------------- /Freakout/IContextHooks.cs: -------------------------------------------------------------------------------- 1 | namespace Freakout; 2 | 3 | /// 4 | /// Can be implemented by to receive calls after being mounted as the ambient context 5 | /// and after being unmounted again. 6 | /// 7 | public interface IContextHooks : IFreakoutContext 8 | { 9 | /// 10 | /// Called after the context has been mounted as the ambient context by 11 | /// 12 | void Mounted(); 13 | 14 | /// 15 | /// Called after the context has been unmounted again by 16 | /// 17 | void Unmounted(); 18 | } -------------------------------------------------------------------------------- /Freakout/IOutboxCommandStore.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Freakout; 5 | 6 | /// 7 | /// Main store implementation. Must be implemented for the chosen type of technology chosen as the store 8 | /// 9 | public interface IOutboxCommandStore 10 | { 11 | /// 12 | /// Must return an "store batch", which is 0..n store commands and a "completion method" (i.e. a way of marking 13 | /// the contained store commands as handled). 14 | /// 15 | Task GetPendingOutboxCommandsAsync(int commandProcessingBatchSize, CancellationToken cancellationToken = default); 16 | } -------------------------------------------------------------------------------- /Freakout.Testing.Tests/Freakout.Testing.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net9.0 5 | enable 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | -------------------------------------------------------------------------------- /Freakout/IBatchDispatcher.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Freakout; 5 | 6 | /// 7 | /// Interface of Freakout's batch dispatcher. This one basically defines what it means to process an and how it's done. 8 | /// 9 | public interface IBatchDispatcher 10 | { 11 | /// 12 | /// This method will be called by Freakout to process the . If it throws an exception, 13 | /// handling is considered as FAILED - if it doesn't, then it's considered SUCCESSFUL. 14 | /// 15 | Task ExecuteAsync(OutboxCommandBatch batch, CancellationToken cancellationToken = default); 16 | } -------------------------------------------------------------------------------- /Freakout/ICommandDispatcher.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Freakout; 5 | 6 | /// 7 | /// Interface of Freakout's command dispatcher. This one defines what it means to process an and how it's done. 8 | /// 9 | public interface ICommandDispatcher 10 | { 11 | /// 12 | /// This method will be called by Freakout to process the . If it throws an exception, 13 | /// handling is considered as FAILED - if it doesn't, then it's considered SUCCESSFUL. 14 | /// 15 | Task ExecuteAsync(OutboxCommand outboxCommand, CancellationToken cancellationToken = default); 16 | } -------------------------------------------------------------------------------- /Freakout/ICommandHandler.cs: -------------------------------------------------------------------------------- 1 | using System.Threading; 2 | using System.Threading.Tasks; 3 | 4 | namespace Freakout; 5 | 6 | /// 7 | /// Freakout command handler marker interface. Used to enable a little bit of nudging via generics. 8 | /// 9 | public interface ICommandHandler { } 10 | 11 | /// 12 | /// Freakout command handler interface. Classes that implement this one or more times can be registered 13 | /// as command handlers and will get to handle commands. 14 | /// 15 | public interface ICommandHandler : ICommandHandler 16 | { 17 | /// 18 | /// Handler method that will be called by Freakout 19 | /// 20 | Task HandleAsync(TCommand command, CancellationToken cancellationToken); 21 | } -------------------------------------------------------------------------------- /Freakout.Testing/InMemFreakoutContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Collections.Generic; 4 | 5 | namespace Freakout.Testing; 6 | 7 | /// 8 | /// Implementation of that buffers messages in memory 9 | /// 10 | public class InMemFreakoutContext : IContextHooks 11 | { 12 | readonly ConcurrentQueue _commands = new(); 13 | 14 | internal void Enlist(InMemOutboxCommand command) => _commands.Enqueue(command); 15 | 16 | internal Action> UnmountedCallback; 17 | 18 | /// 19 | public void Mounted() 20 | { 21 | } 22 | 23 | /// 24 | public void Unmounted() => UnmountedCallback?.Invoke(_commands); 25 | } -------------------------------------------------------------------------------- /scripts/build.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | set scriptsdir=%~dp0 4 | set root=%scriptsdir%\.. 5 | set project=%1 6 | set version=%2 7 | 8 | if "%project%"=="" ( 9 | echo Please invoke the build script with a project name as its first argument. 10 | echo. 11 | goto exit_fail 12 | ) 13 | 14 | if "%version%"=="" ( 15 | echo Please invoke the build script with a version as its second argument. 16 | echo. 17 | goto exit_fail 18 | ) 19 | 20 | set Version=%version% 21 | 22 | pushd %root% 23 | 24 | dotnet restore --interactive 25 | if %ERRORLEVEL% neq 0 ( 26 | popd 27 | goto exit_fail 28 | ) 29 | 30 | dotnet build -c Release --no-restore 31 | if %ERRORLEVEL% neq 0 ( 32 | popd 33 | goto exit_fail 34 | ) 35 | 36 | popd 37 | 38 | 39 | 40 | 41 | 42 | 43 | goto exit_success 44 | :exit_fail 45 | exit /b 1 46 | :exit_success -------------------------------------------------------------------------------- /Freakout/IOutbox.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | // ReSharper disable UnusedMember.Global 5 | 6 | namespace Freakout; 7 | 8 | /// 9 | /// Main Freakout outbox command adder. 10 | /// 11 | public interface IOutbox 12 | { 13 | /// 14 | /// Adds a single outbox command to the outbox command store 15 | /// 16 | void AddOutboxCommand(object command, Dictionary headers = null, CancellationToken cancellationToken = default); 17 | 18 | /// 19 | /// Adds a single outbox command to the outbox command store 20 | /// 21 | Task AddOutboxCommandAsync(object command, Dictionary headers = null, CancellationToken cancellationToken = default); 22 | } -------------------------------------------------------------------------------- /Freakout/ICommandSerializer.cs: -------------------------------------------------------------------------------- 1 | namespace Freakout; 2 | 3 | /// 4 | /// Command serializer. Must be capable of serializing all the relevant commands 5 | /// 6 | public interface ICommandSerializer 7 | { 8 | /// 9 | /// Serializes the given into an . 10 | /// Please note that the headers returned in the are highly likely 11 | /// to be important to the deserialization process, so please only tamper with them if you know what you're doing. 12 | /// 13 | OutboxCommand Serialize(object command); 14 | 15 | /// 16 | /// Deserializes the given into a copy of the original command. 17 | /// 18 | object Deserialize(OutboxCommand outboxCommand); 19 | } -------------------------------------------------------------------------------- /Freakout/IFreakoutContextAccessor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace Freakout; 4 | 5 | /// 6 | /// Accessor that enables getting the current ambient Freakout context (or NULL if there is none) 7 | /// 8 | public interface IFreakoutContextAccessor 9 | { 10 | /// 11 | /// Returns the current ambient Freakout context of type . 12 | /// If is TRUE, an is thrown if no context could be found. 13 | /// If is FALSE and there's no context, NULL is returned. 14 | /// Throws if there is a context but it is not of type . 15 | /// 16 | TContext GetContext(bool throwIfNull = true) where TContext : class, IFreakoutContext; 17 | } -------------------------------------------------------------------------------- /Freakout/Serialization/HeaderSerializer.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Text.Json; 3 | 4 | namespace Freakout.Serialization; 5 | 6 | /// 7 | /// Built-in header serializer. This is just how store command headers are serialized. Uses System.Text.Json internally. 8 | /// 9 | public static class HeaderSerializer 10 | { 11 | /// 12 | /// Serializes the to a string. 13 | /// 14 | public static string SerializeToString(Dictionary headers) => JsonSerializer.Serialize(headers); 15 | 16 | /// 17 | /// Deserializes the string back to a Dictionary<string, string> 18 | /// 19 | public static Dictionary DeserializeFromString(string headers) => JsonSerializer.Deserialize>(headers); 20 | } -------------------------------------------------------------------------------- /scripts/push.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | set version=%1 4 | 5 | if "%version%"=="" ( 6 | echo Please remember to specify which version to push as an argument. 7 | goto exit_fail 8 | ) 9 | 10 | set reporoot=%~dp0\.. 11 | set destination=%reporoot%\deploy 12 | 13 | if not exist "%destination%" ( 14 | echo Could not find %destination% 15 | echo. 16 | echo Did you remember to build the packages before running this script? 17 | ) 18 | 19 | set nuget=%reporoot%\tools\NuGet\NuGet.exe 20 | 21 | if not exist "%nuget%" ( 22 | echo Could not find NuGet here: 23 | echo. 24 | echo "%nuget%" 25 | echo. 26 | goto exit_fail 27 | ) 28 | 29 | 30 | "%nuget%" push "%destination%\*.%version%.nupkg" -Source https://nuget.org 31 | if %ERRORLEVEL% neq 0 ( 32 | echo NuGet push failed. 33 | goto exit_fail 34 | ) 35 | 36 | 37 | 38 | 39 | 40 | 41 | goto exit_success 42 | :exit_fail 43 | exit /b 1 44 | :exit_success 45 | -------------------------------------------------------------------------------- /Freakout.NpgSql.Tests/Freakout.NpgSql.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net9.0 5 | enable 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | -------------------------------------------------------------------------------- /Freakout/Internals/AsyncLocalFreakoutContextAccessor.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | 4 | namespace Freakout.Internals; 5 | 6 | class AsyncLocalFreakoutContextAccessor : IFreakoutContextAccessor 7 | { 8 | internal static readonly AsyncLocal Instance = new(); 9 | 10 | public TContext GetContext(bool throwIfNull = true) where TContext : class, IFreakoutContext 11 | { 12 | var instance = Instance.Value; 13 | 14 | if (instance == null) 15 | { 16 | if (!throwIfNull) return null; 17 | 18 | throw new InvalidOperationException("Could not get ambient Frekout context. Please be sure that a suitable ambient context is available by using FreakoutContextScope"); 19 | } 20 | 21 | if (instance is TContext context) return context; 22 | 23 | throw new InvalidCastException( 24 | $"Ambient Freakout context of type {instance.GetType()} cannot be cast to {typeof(TContext)}"); 25 | } 26 | } -------------------------------------------------------------------------------- /Freakout.MsSql.Tests/Freakout.MsSql.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net9.0 5 | enable 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | -------------------------------------------------------------------------------- /Freakout.Tests/Freakout.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net9.0 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2024 Rebus 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Freakout.Testing/Freakout.Testing.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0;net462;net8.0;net9.0 5 | 13 6 | mookid8000 7 | Rebus FM ApS 8 | README.md 9 | https://github.com/rebus-org/Freakout 10 | MIT 11 | True 12 | snupkg 13 | transparent_2024-04-11T18-52-17_cropped.png 14 | True 15 | 16 | 17 | 18 | 19 | True 20 | \ 21 | 22 | 23 | True 24 | \ 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | -------------------------------------------------------------------------------- /Freakout.MsSql/MsSqlFreakoutContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data.Common; 3 | 4 | namespace Freakout.MsSql; 5 | 6 | /// 7 | /// Implementation of that wraps a 8 | /// and a 9 | /// 10 | public class MsSqlFreakoutContext : IFreakoutContext 11 | { 12 | /// 13 | /// Gets the current 14 | /// 15 | public DbConnection Connection { get; } 16 | 17 | /// 18 | /// Gets the current 19 | /// 20 | public DbTransaction Transaction { get; } 21 | 22 | /// 23 | /// Creates the context and sets it up to pass the given and 24 | /// to the implementation 25 | /// 26 | public MsSqlFreakoutContext(DbConnection connection, DbTransaction transaction) 27 | { 28 | Connection = connection ?? throw new ArgumentNullException(nameof(connection)); 29 | Transaction = transaction ?? throw new ArgumentNullException(nameof(transaction)); 30 | } 31 | } -------------------------------------------------------------------------------- /Freakout.Tests/Contracts/AbstractFreakoutSystemFactory.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Microsoft.Extensions.DependencyInjection; 3 | using Microsoft.Extensions.Logging; 4 | using Nito.Disposables; 5 | 6 | namespace Freakout.Tests.Contracts; 7 | 8 | public abstract class AbstractFreakoutSystemFactory : IFreakoutSystemFactory 9 | { 10 | protected readonly CollectionDisposable disposables = new(); 11 | 12 | public FreakoutSystem Create(Action before = null, Action after = null) 13 | { 14 | var services = new ServiceCollection(); 15 | 16 | before?.Invoke(services); 17 | 18 | ConfigureServices(services); 19 | 20 | after?.Invoke(services); 21 | 22 | services.AddLogging(builder => builder.AddConsole()); 23 | 24 | var provider = services.BuildServiceProvider(); 25 | 26 | disposables.Add(provider); 27 | 28 | return GetFreakoutSystem(provider); 29 | } 30 | 31 | protected abstract FreakoutSystem GetFreakoutSystem(ServiceProvider provider); 32 | 33 | protected abstract void ConfigureServices(IServiceCollection services); 34 | 35 | public void Dispose() => disposables.Dispose(); 36 | } -------------------------------------------------------------------------------- /Freakout/Internals/InternalExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Microsoft.Extensions.Logging; 6 | using Microsoft.Extensions.Logging.Abstractions; 7 | 8 | namespace Freakout.Internals; 9 | 10 | static class InternalExtensions 11 | { 12 | public static ILogger GetLoggerFor(this IServiceProvider serviceProvider) => serviceProvider.GetLoggerFactory().CreateLogger(); 13 | 14 | public static ILoggerFactory GetLoggerFactory(this IServiceProvider serviceProvider) => serviceProvider.GetService() ?? new NullLoggerFactory(); 15 | 16 | public static string GetValueOrThrow(this Dictionary dictionary, string key) 17 | { 18 | if (dictionary == null) throw new ArgumentNullException(nameof(dictionary)); 19 | if (key == null) throw new ArgumentNullException(nameof(key)); 20 | 21 | return dictionary.TryGetValue(key, out var result) 22 | ? result 23 | : throw new KeyNotFoundException($"Could not find element with key '{key}' among these: {string.Join(", ", dictionary.Keys.Select(k => $"'{k}'"))}"); 24 | } 25 | } -------------------------------------------------------------------------------- /Freakout.NpgSql/NpgsqlFreakoutContext.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Data.Common; 3 | using Npgsql; 4 | 5 | namespace Freakout.NpgSql; 6 | 7 | /// 8 | /// Implementation of that wraps a 9 | /// and a 10 | /// 11 | public class NpgsqlFreakoutContext : IFreakoutContext 12 | { 13 | /// 14 | /// Gets the current 15 | /// 16 | public NpgsqlConnection Connection { get; } 17 | 18 | /// 19 | /// Gets the current 20 | /// 21 | public NpgsqlTransaction Transaction { get; } 22 | 23 | /// 24 | /// Creates the context and sets it up to pass the given and 25 | /// to the implementation 26 | /// 27 | public NpgsqlFreakoutContext(NpgsqlConnection connection, NpgsqlTransaction transaction) 28 | { 29 | Connection = connection ?? throw new ArgumentNullException(nameof(connection)); 30 | Transaction = transaction ?? throw new ArgumentNullException(nameof(transaction)); 31 | } 32 | } -------------------------------------------------------------------------------- /Freakout.Marten/Freakout.Marten.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0;net9.0 5 | 13 6 | mookid8000 7 | Rebus FM ApS 8 | README.md 9 | https://github.com/rebus-org/Freakout 10 | MIT 11 | True 12 | snupkg 13 | transparent_2024-04-11T18-52-17_cropped.png 14 | True 15 | 16 | 17 | 18 | 19 | True 20 | \ 21 | 22 | 23 | True 24 | \ 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Freakout.NpgSql/Freakout.NpgSql.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0;net9.0 5 | 13 6 | mookid8000 7 | Rebus FM ApS 8 | README.md 9 | https://github.com/rebus-org/Freakout 10 | MIT 11 | True 12 | snupkg 13 | transparent_2024-04-11T18-52-17_cropped.png 14 | True 15 | 16 | 17 | 18 | 19 | True 20 | \ 21 | 22 | 23 | True 24 | \ 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Freakout/Internals/DefaultBatchDispatcher.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Diagnostics; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using Microsoft.Extensions.Logging; 6 | 7 | namespace Freakout.Internals; 8 | 9 | class DefaultBatchDispatcher(ICommandDispatcher commandDispatcher, ILogger logger) : IBatchDispatcher 10 | { 11 | public async Task ExecuteAsync(OutboxCommandBatch batch, CancellationToken cancellationToken = default) 12 | { 13 | foreach (var command in batch) 14 | { 15 | var stopwatch = Stopwatch.StartNew(); 16 | 17 | logger.LogDebug("Executing store command {command}", command); 18 | 19 | try 20 | { 21 | await commandDispatcher.ExecuteAsync(command, cancellationToken); 22 | 23 | command.SetState(new SuccessfullyExecutedCommandState(stopwatch.Elapsed)); 24 | 25 | logger.LogDebug("Successfully executed store command {command}", command); 26 | } 27 | catch (Exception exception) 28 | { 29 | command.SetState(new FailedCommandState(stopwatch.Elapsed, exception)); 30 | 31 | throw new ApplicationException($"Could not execute command {command}", exception); 32 | } 33 | } 34 | } 35 | } -------------------------------------------------------------------------------- /Freakout.MsSql/Freakout.MsSql.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net462;net8.0;net9.0 5 | 13 6 | mookid8000 7 | Rebus FM ApS 8 | README.md 9 | https://github.com/rebus-org/Freakout 10 | MIT 11 | True 12 | snupkg 13 | transparent_2024-04-11T18-52-17_cropped.png 14 | True 15 | 16 | 17 | 18 | 19 | True 20 | \ 21 | 22 | 23 | True 24 | \ 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | -------------------------------------------------------------------------------- /Freakout.Testing/Internals/InMemOutboxDecorator.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Runtime.Serialization; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | 7 | namespace Freakout.Testing.Internals; 8 | 9 | class InMemOutboxDecorator(ICommandSerializer serializer, IOutbox outbox) : IOutbox 10 | { 11 | public void AddOutboxCommand(object command, Dictionary headers = null, CancellationToken cancellationToken = default) 12 | { 13 | CheckSerialization(command); 14 | outbox.AddOutboxCommand(command, headers, cancellationToken); 15 | } 16 | 17 | public async Task AddOutboxCommandAsync(object command, Dictionary headers = null, CancellationToken cancellationToken = default) 18 | { 19 | CheckSerialization(command); 20 | await outbox.AddOutboxCommandAsync(command, headers, cancellationToken); 21 | } 22 | 23 | void CheckSerialization(object command) 24 | { 25 | try 26 | { 27 | var outboxCommand = serializer.Serialize(command); 28 | var roundtrippedCommand = serializer.Deserialize(outboxCommand); 29 | } 30 | catch (Exception exception) 31 | { 32 | throw new SerializationException( 33 | $"Serialization check failed – the command {command} could not be roundtripped by serializer {serializer.GetType().Name}", exception); 34 | } 35 | } 36 | } -------------------------------------------------------------------------------- /Freakout.Tests/Contracts/FreakoutSystem.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Freakout.Tests.Contracts; 7 | 8 | public class FreakoutSystem(ServiceProvider ServiceProvider, Func contextFactory, Action commitAction, Action disposeAction) 9 | { 10 | public IOutbox Outbox => ServiceProvider.GetRequiredService(); 11 | 12 | public IOutboxCommandStore OutboxCommandStore => ServiceProvider.GetRequiredService(); 13 | 14 | public ICommandSerializer CommandSerializer => ServiceProvider.GetRequiredService(); 15 | 16 | public FreakoutTestScope CreateScope() => new(contextFactory(), commitAction, disposeAction); 17 | 18 | public class FreakoutTestScope(IFreakoutContext freakoutContext, Action commitAction, Action disposeAction) : IDisposable 19 | { 20 | readonly FreakoutContextScope _innerScope = new(freakoutContext); 21 | 22 | public void Complete() => commitAction(freakoutContext); 23 | 24 | public void Dispose() 25 | { 26 | _innerScope.Dispose(); 27 | disposeAction(freakoutContext); 28 | } 29 | } 30 | 31 | public Task StartCommandProcessorAsync(CancellationToken stoppingToken) 32 | { 33 | return ServiceProvider.RunBackgroundWorkersAsync(stoppingToken); 34 | } 35 | } -------------------------------------------------------------------------------- /scripts/release.cmd: -------------------------------------------------------------------------------- 1 | @echo off 2 | 3 | set scriptsdir=%~dp0 4 | set root=%scriptsdir%\.. 5 | set deploydir=%root%\deploy 6 | set project=%1 7 | set version=%2 8 | 9 | if "%project%"=="" ( 10 | echo Please invoke the build script with a project name as its first argument. 11 | echo. 12 | goto exit_fail 13 | ) 14 | 15 | if "%version%"=="" ( 16 | echo Please invoke the build script with a version as its second argument. 17 | echo. 18 | goto exit_fail 19 | ) 20 | 21 | set Version=%version% 22 | 23 | if exist "%deploydir%" ( 24 | rd "%deploydir%" /s/q 25 | ) 26 | 27 | pushd %root% 28 | 29 | dotnet restore --interactive 30 | if %ERRORLEVEL% neq 0 ( 31 | popd 32 | goto exit_fail 33 | ) 34 | 35 | dotnet pack Freakout -c Release -o "%deploydir%" -p:PackageVersion=%version% --no-restore 36 | if %ERRORLEVEL% neq 0 ( 37 | popd 38 | goto exit_fail 39 | ) 40 | 41 | dotnet pack Freakout.MsSql -c Release -o "%deploydir%" -p:PackageVersion=%version% --no-restore 42 | if %ERRORLEVEL% neq 0 ( 43 | popd 44 | goto exit_fail 45 | ) 46 | 47 | dotnet pack Freakout.NpgSql -c Release -o "%deploydir%" -p:PackageVersion=%version% --no-restore 48 | if %ERRORLEVEL% neq 0 ( 49 | popd 50 | goto exit_fail 51 | ) 52 | 53 | dotnet pack Freakout.Testing -c Release -o "%deploydir%" -p:PackageVersion=%version% --no-restore 54 | if %ERRORLEVEL% neq 0 ( 55 | popd 56 | goto exit_fail 57 | ) 58 | 59 | call scripts\push.cmd "%version%" 60 | 61 | popd 62 | 63 | 64 | 65 | 66 | 67 | 68 | goto exit_success 69 | :exit_fail 70 | exit /b 1 71 | :exit_success -------------------------------------------------------------------------------- /Freakout.Tests/TestExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using Microsoft.Extensions.Hosting; 7 | 8 | namespace Freakout.Tests; 9 | 10 | public static class TestExtensions 11 | { 12 | public static async Task RunBackgroundWorkersAsync(this ServiceProvider serviceProvider, CancellationToken stoppingToken) 13 | { 14 | ArgumentNullException.ThrowIfNull(serviceProvider); 15 | 16 | var servicesToStop = new ConcurrentStack(); 17 | 18 | try 19 | { 20 | var hostedServices = serviceProvider.GetServices(); 21 | 22 | foreach (var service in hostedServices) 23 | { 24 | await service.StartAsync(stoppingToken); 25 | servicesToStop.Push(service); 26 | } 27 | 28 | await Task.Delay(-1, stoppingToken); 29 | } 30 | catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) 31 | { 32 | // it's ok 33 | } 34 | catch (Exception exception) 35 | { 36 | Console.WriteLine($"Failed to run background services: {exception}"); 37 | } 38 | finally 39 | { 40 | while (servicesToStop.TryPop(out var service)) 41 | { 42 | await service.StopAsync(CancellationToken.None); 43 | } 44 | } 45 | } 46 | } -------------------------------------------------------------------------------- /Freakout.Testing/Internals/InMemOutbox.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Collections.Generic; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously 7 | 8 | namespace Freakout.Testing.Internals; 9 | 10 | class InMemOutbox(IFreakoutContextAccessor freakoutContextAccessor, ConcurrentQueue commands) : IOutbox 11 | { 12 | public event Action CommandAddedToQueue; 13 | 14 | public void AddOutboxCommand(object command, Dictionary headers = null, CancellationToken cancellationToken = default) 15 | { 16 | Enqueue(command, headers); 17 | } 18 | 19 | public async Task AddOutboxCommandAsync(object command, Dictionary headers = null, CancellationToken cancellationToken = default) 20 | { 21 | Enqueue(command, headers); 22 | } 23 | 24 | void Enqueue(object command, Dictionary headers) 25 | { 26 | var context = freakoutContextAccessor.GetContext(); 27 | var inMemOutboxCommand = new InMemOutboxCommand(headers ?? new(), command); 28 | 29 | context.UnmountedCallback ??= enlistedCommands => 30 | { 31 | foreach (var cmd in enlistedCommands) 32 | { 33 | commands.Enqueue(cmd); 34 | CommandAddedToQueue?.Invoke(cmd); 35 | } 36 | }; 37 | 38 | context.Enlist(inMemOutboxCommand); 39 | } 40 | } -------------------------------------------------------------------------------- /Freakout/Freakout.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netstandard2.0;net462;net8.0;net9.0 5 | 13 6 | mookid8000 7 | Rebus FM ApS 8 | README.md 9 | https://github.com/rebus-org/Freakout 10 | MIT 11 | True 12 | snupkg 13 | transparent_2024-04-11T18-52-17_cropped.png 14 | True 15 | 16 | 17 | 18 | 19 | True 20 | \ 21 | 22 | 23 | True 24 | \ 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | -------------------------------------------------------------------------------- /Freakout.NpgSql.Tests/NpgSqlTestHelper.cs: -------------------------------------------------------------------------------- 1 | using Nito.AsyncEx.Synchronous; 2 | using Nito.Disposables; 3 | using Npgsql; 4 | using NUnit.Framework; 5 | using Testcontainers.PostgreSql; 6 | 7 | namespace Freakout.NpgSql.Tests; 8 | 9 | [SetUpFixture] 10 | class NpgsqlTestHelper 11 | { 12 | static readonly CollectionDisposable Disposables = new(); 13 | 14 | static readonly Lazy LazyConnectionString = new(() => 15 | { 16 | var connectionString = Environment.GetEnvironmentVariable("NPGSQL_TEST_CONNECTIONSTRING"); 17 | if (!string.IsNullOrWhiteSpace(connectionString)) return connectionString; 18 | 19 | var builder = new PostgreSqlBuilder(); 20 | var container = builder.Build(); 21 | 22 | container.StartAsync().WaitAndUnwrapException(); 23 | 24 | Disposables.Add(new Disposable(() => container.DisposeAsync())); 25 | 26 | return container.GetConnectionString(); 27 | }); 28 | 29 | public static string ConnectionString => LazyConnectionString.Value; 30 | 31 | [OneTimeTearDown] 32 | public void CleanUp() => Disposables.Dispose(); 33 | 34 | public static void DropTable(string tableName) => DropTable("dbo", tableName); 35 | 36 | public static void DropTable(string schemaName, string tableName) 37 | { 38 | using var connection = new NpgsqlConnection(ConnectionString); 39 | connection.Open(); 40 | 41 | using var command = connection.CreateCommand(); 42 | command.CommandText = $@"DROP TABLE IF EXISTS ""{schemaName}"".""{tableName}"";"; 43 | command.ExecuteNonQuery(); 44 | } 45 | 46 | } -------------------------------------------------------------------------------- /Freakout/PendingOutboxCommand.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace Freakout; 5 | 6 | /// 7 | /// Raw store command after having been persisted 8 | /// 9 | public record PendingOutboxCommand(Guid Id, DateTimeOffset Created, Dictionary Headers, byte[] Payload) 10 | : OutboxCommand(Headers, Payload) 11 | { 12 | /// 13 | /// Gets the command state 14 | /// 15 | public CommandState State { get; private set; } = new PendingCommandState(); 16 | 17 | /// 18 | /// Sets to 19 | /// 20 | public void SetState(CommandState state) => State = state ?? throw new ArgumentNullException(nameof(state)); 21 | } 22 | 23 | /// 24 | /// Abstract command state. Represents a state that a command can be in. 25 | /// 26 | public abstract record CommandState; 27 | 28 | /// 29 | /// Represents the state of the command when it has been fetched from the command store. 30 | /// 31 | public record PendingCommandState : CommandState; 32 | 33 | /// 34 | /// Represents the state of the command when it has been successfully executed and executing it took 35 | /// 36 | public record SuccessfullyExecutedCommandState(TimeSpan Elapsed) : CommandState; 37 | 38 | /// 39 | /// Represents the state of the command when executing it failed with and took 40 | /// 41 | public record FailedCommandState(TimeSpan Elapsed, Exception Exception) : CommandState; -------------------------------------------------------------------------------- /Freakout.MsSql/MsSqlFreakoutConfiguration.cs: -------------------------------------------------------------------------------- 1 | using Freakout.MsSql.Internals; 2 | using Microsoft.Extensions.DependencyInjection; 3 | 4 | namespace Freakout.MsSql; 5 | 6 | /// 7 | /// Freakout configuration for using Microsoft SQL Server as the store. 8 | /// 9 | /// Configures the connection string to use to connect to SQL Server 10 | public class MsSqlFreakoutConfiguration(string connectionString) : FreakoutConfiguration 11 | { 12 | /// 13 | /// Configures the store table schema name. Defaults to "dbo". 14 | /// 15 | public string SchemaName { get; set; } = "dbo"; 16 | 17 | /// 18 | /// Configures the store table name. Defaults to "OutboxCommands". 19 | /// 20 | public string TableName { get; set; } = "OutboxCommands"; 21 | 22 | /// 23 | /// Configures whether the schema should be created automatically 24 | /// 25 | public bool AutomaticallyCreateSchema { get; set; } = true; 26 | 27 | /// 28 | protected override void ConfigureServices(IServiceCollection services) 29 | { 30 | services.AddSingleton(_ => 31 | { 32 | var commandStore = new MsSqlOutboxCommandStore(connectionString, TableName, SchemaName); 33 | 34 | if (AutomaticallyCreateSchema) 35 | { 36 | commandStore.CreateSchema(); 37 | } 38 | 39 | return commandStore; 40 | }); 41 | 42 | services.AddScoped(p => new MsSqlOutbox(this, p.GetRequiredService())); 43 | } 44 | } -------------------------------------------------------------------------------- /Freakout.NpgSql/NpgSqlFreakoutConfiguration.cs: -------------------------------------------------------------------------------- 1 | using Freakout.NpgSql.Internals; 2 | using Microsoft.Extensions.DependencyInjection; 3 | 4 | namespace Freakout.NpgSql; 5 | 6 | /// 7 | /// Freakout configuration for using PostgreSQL as the store. 8 | /// 9 | /// Configures the connection string to use to connect to Postgres 10 | public class NpgsqlFreakoutConfiguration(string connectionString) : FreakoutConfiguration 11 | { 12 | /// 13 | /// Configures the store table schema name. Defaults to "public". 14 | /// 15 | public string SchemaName { get; set; } = "public"; 16 | 17 | /// 18 | /// Configures the store table name. Defaults to "OutboxCommands". 19 | /// 20 | public string TableName { get; set; } = "outbox_commands"; 21 | 22 | /// 23 | /// Configures whether the schema should be created automatically 24 | /// 25 | public bool AutomaticallyCreateSchema { get; set; } = true; 26 | 27 | /// 28 | protected override void ConfigureServices(IServiceCollection services) 29 | { 30 | services.AddSingleton(_ => 31 | { 32 | var commandStore = new NpgsqlOutboxCommandStore(connectionString, TableName, SchemaName); 33 | 34 | if (AutomaticallyCreateSchema) 35 | { 36 | commandStore.CreateSchema(); 37 | } 38 | 39 | return commandStore; 40 | }); 41 | 42 | services.AddScoped(p => new NpgsqlOutbox(this, p.GetRequiredService())); 43 | } 44 | } -------------------------------------------------------------------------------- /Freakout.MsSql.Tests/MsSqlTestHelper.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.Data.SqlClient; 2 | using Nito.AsyncEx.Synchronous; 3 | using Nito.Disposables; 4 | using NUnit.Framework; 5 | using Testcontainers.MsSql; 6 | 7 | namespace Freakout.MsSql.Tests; 8 | 9 | [SetUpFixture] 10 | class MsSqlTestHelper 11 | { 12 | static readonly CollectionDisposable Disposables = new(); 13 | 14 | static readonly Lazy LazyConnectionString = new(() => 15 | { 16 | var connectionString = Environment.GetEnvironmentVariable("MSSQL_TEST_CONNECTIONSTRING"); 17 | if (!string.IsNullOrWhiteSpace(connectionString)) return connectionString; 18 | 19 | var builder = new MsSqlBuilder(); 20 | var container = builder.Build(); 21 | 22 | container.StartAsync().WaitAndUnwrapException(); 23 | 24 | Disposables.Add(new Disposable(() => container.DisposeAsync())); 25 | 26 | return container.GetConnectionString(); 27 | }); 28 | 29 | public static string ConnectionString => LazyConnectionString.Value; 30 | 31 | [OneTimeTearDown] 32 | public void CleanUp() => Disposables.Dispose(); 33 | 34 | public static void DropTable(string tableName) => DropTable("dbo", tableName); 35 | 36 | public static void DropTable(string schemaName, string tableName) 37 | { 38 | using var connection = new SqlConnection(ConnectionString); 39 | connection.Open(); 40 | 41 | using var command = connection.CreateCommand(); 42 | command.CommandText = $@" 43 | IF EXISTS (SELECT TOP 1 * FROM sys.tables t JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE t.name = '{tableName}' AND s.name = '{schemaName}') 44 | BEGIN 45 | DROP TABLE [{schemaName}].[{tableName}] 46 | END 47 | "; 48 | command.ExecuteNonQuery(); 49 | } 50 | 51 | } -------------------------------------------------------------------------------- /Freakout.MsSql.Tests/MsSqlFreakoutSystemFactory.cs: -------------------------------------------------------------------------------- 1 | using Freakout.Config; 2 | using Freakout.Tests.Contracts; 3 | using Microsoft.Data.SqlClient; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using Testy.General; 6 | 7 | namespace Freakout.MsSql.Tests; 8 | 9 | public class MsSqlFreakoutSystemFactory : AbstractFreakoutSystemFactory 10 | { 11 | protected override void ConfigureServices(IServiceCollection services) 12 | { 13 | var tableName = $"outbox-{Guid.NewGuid():N}"; 14 | 15 | disposables.Add(new DisposableCallback(() => MsSqlTestHelper.DropTable(tableName))); 16 | 17 | var configuration = new MsSqlFreakoutConfiguration(MsSqlTestHelper.ConnectionString) 18 | { 19 | TableName = tableName, 20 | OutboxPollInterval = TimeSpan.FromSeconds(1) 21 | }; 22 | 23 | services.AddFreakout(configuration); 24 | } 25 | 26 | protected override FreakoutSystem GetFreakoutSystem(ServiceProvider provider) 27 | { 28 | IFreakoutContext ContextFactory() 29 | { 30 | var connection = new SqlConnection(MsSqlTestHelper.ConnectionString); 31 | connection.Open(); 32 | var transaction = connection.BeginTransaction(); 33 | return new MsSqlFreakoutContext(connection, transaction); 34 | } 35 | 36 | void CommitAction(IFreakoutContext context) 37 | { 38 | var ctx = (MsSqlFreakoutContext)context; 39 | ctx.Transaction.Commit(); 40 | } 41 | 42 | void DisposeAction(IFreakoutContext context) 43 | { 44 | var ctx = (MsSqlFreakoutContext)context; 45 | ctx.Transaction.Dispose(); 46 | ctx.Connection.Dispose(); 47 | } 48 | 49 | return new(provider, ContextFactory, CommitAction, DisposeAction); 50 | } 51 | } -------------------------------------------------------------------------------- /Freakout.NpgSql/Internals/NpgSqlOutbox.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | 6 | // ReSharper disable MethodHasAsyncOverloadWithCancellation 7 | 8 | namespace Freakout.NpgSql.Internals; 9 | 10 | class NpgsqlOutbox(NpgsqlFreakoutConfiguration configuration, IFreakoutContextAccessor freakoutContextAccessor) : IOutbox 11 | { 12 | public void AddOutboxCommand(object command, Dictionary headers = null, CancellationToken cancellationToken = default) 13 | { 14 | if (command == null) throw new ArgumentNullException(nameof(command)); 15 | 16 | var context = freakoutContextAccessor.GetContext(); 17 | var transaction = context.Transaction; 18 | 19 | transaction.AddOutboxCommand( 20 | schemaName: configuration.SchemaName, 21 | tableName: configuration.TableName, 22 | serializer: configuration.CommandSerializer, 23 | command: command, 24 | headers: headers 25 | ); 26 | } 27 | 28 | public async Task AddOutboxCommandAsync(object command, Dictionary headers = null, CancellationToken cancellationToken = default) 29 | { 30 | if (command == null) throw new ArgumentNullException(nameof(command)); 31 | 32 | var context = freakoutContextAccessor.GetContext(); 33 | var transaction = context.Transaction; 34 | 35 | await transaction.AddOutboxCommandAsync( 36 | schemaName: configuration.SchemaName, 37 | tableName: configuration.TableName, 38 | serializer: configuration.CommandSerializer, 39 | command: command, 40 | headers: headers, 41 | cancellationToken: cancellationToken 42 | ); 43 | } 44 | } -------------------------------------------------------------------------------- /Freakout/Internals/CommandDispatcher.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using Microsoft.Extensions.DependencyInjection; 6 | 7 | namespace Freakout.Internals; 8 | 9 | abstract class CommandDispatcher(ICommandSerializer commandSerializer, IServiceScopeFactory serviceScopeFactory) : ICommandDispatcher 10 | { 11 | readonly ConcurrentDictionary> _invokers = new(); 12 | 13 | public async Task ExecuteAsync(OutboxCommand outboxCommand, CancellationToken cancellationToken = default) 14 | { 15 | var command = commandSerializer.Deserialize(outboxCommand); 16 | var type = command.GetType(); 17 | 18 | var invoker = _invokers.GetOrAdd(type, CreateInvoker); 19 | 20 | await invoker(command, cancellationToken); 21 | } 22 | 23 | protected abstract Func CreateInvoker(Type commandType); 24 | 25 | protected async Task ExecuteOutboxCommandGeneric(TCommand command, CancellationToken cancellationToken) 26 | { 27 | using var scope = serviceScopeFactory.CreateScope(); 28 | 29 | var handler = Resolve(scope.ServiceProvider); 30 | 31 | await handler.HandleAsync(command, cancellationToken); 32 | 33 | static ICommandHandler Resolve(IServiceProvider serviceProvider) 34 | { 35 | try 36 | { 37 | return serviceProvider.GetRequiredService>(); 38 | } 39 | catch (Exception exception) 40 | { 41 | throw new InvalidOperationException( 42 | $"Could not resolve ICommandHandler<{typeof(TCommand)}> from the container", exception); 43 | } 44 | } 45 | } 46 | 47 | } -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## 0.0.1 4 | * Playing around 5 | 6 | ## 0.0.2 7 | * Package metadata 8 | 9 | ## 0.0.3 10 | * It's actually functional now! 11 | 12 | ## 0.0.4 13 | * Add XML docs 14 | 15 | ## 0.0.5 16 | * Write the XML docs ;) 17 | 18 | ## 0.0.6 19 | * Pull dispatcher out, so it can be decorated (e.g. to implement Polly-based retries) 20 | 21 | ## 0.0.7 22 | * Add `IOutbox` interface so outbox commands can be stored in a tech agnostic way 23 | 24 | ## 0.0.8 25 | * NpgSQL outbox! 26 | 27 | ## 0.0.9 28 | * Change the way connections are provided by introducing ambient contexts 29 | 30 | ## 0.0.10 31 | * New artwork 32 | 33 | ## 0.0.11 34 | * Execute command handlers in Freakout context too 35 | 36 | ## 0.0.12 37 | * Update readme 38 | 39 | ## 0.0.13 40 | * Even better readme 41 | 42 | ## 0.0.14 43 | * Move internals into internals folders 44 | 45 | ## 0.0.15 46 | * Lose the dependency on Microsoft.CSharp when it isn't necessary 47 | 48 | ## 0.0.16 49 | * Update readme 50 | 51 | ## 0.0.17 52 | * Update readme 53 | 54 | ## 0.0.18 55 | * Generate sequential GUIDs 56 | 57 | ## 0.0.19 58 | * Change to use common format for stored commands 59 | 60 | ## 0.0.21 61 | * Factor batch dispatch out into separate service to enable plugging in other batch dispatch strategies 62 | * Make options for individually marking commands as succeeded/failed more sophisticated 63 | 64 | ## 0.0.22 65 | * Add IL emit-based command dispatcher and make it the new default - thanks [Danielovich] 66 | 67 | ## 0.0.23 68 | * Dodge `OperationCanceledException` in just the right way during shutdown 69 | 70 | ## 0.0.24 71 | * Add support for interfaceless command handlers 72 | 73 | ## 0.0.30 74 | * Remove need for globals 75 | 76 | ## 0.0.31 77 | * Fix tests and update some packages 78 | 79 | ## 0.0.32 80 | * Remove misleading parameter default 81 | * Update the deps 82 | 83 | 84 | [Danielovich]: https://github.com/Danielovich -------------------------------------------------------------------------------- /Freakout.NpgSql.Tests/NpgSqlFreakoutSystemFactory.cs: -------------------------------------------------------------------------------- 1 | using Freakout.Config; 2 | using Freakout.Tests.Contracts; 3 | using Microsoft.Extensions.DependencyInjection; 4 | using Npgsql; 5 | using Testy.General; 6 | 7 | #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously 8 | 9 | namespace Freakout.NpgSql.Tests; 10 | 11 | public class NpgsqlFreakoutSystemFactory : AbstractFreakoutSystemFactory 12 | { 13 | protected override void ConfigureServices(IServiceCollection services) 14 | { 15 | var tableName = $"outbox-{Guid.NewGuid():N}"; 16 | 17 | disposables.Add(new DisposableCallback(() => NpgsqlTestHelper.DropTable(tableName))); 18 | 19 | var configuration = new NpgsqlFreakoutConfiguration(NpgsqlTestHelper.ConnectionString) 20 | { 21 | OutboxPollInterval = TimeSpan.FromSeconds(1), 22 | TableName = tableName 23 | }; 24 | 25 | services.AddFreakout(configuration); 26 | } 27 | 28 | protected override FreakoutSystem GetFreakoutSystem(ServiceProvider provider) 29 | { 30 | IFreakoutContext ContextFactory() 31 | { 32 | var connection = new NpgsqlConnection(NpgsqlTestHelper.ConnectionString); 33 | connection.Open(); 34 | var transaction = connection.BeginTransaction(); 35 | return new NpgsqlFreakoutContext(connection, transaction); 36 | } 37 | 38 | void CommitAction(IFreakoutContext context) 39 | { 40 | var ctx = (NpgsqlFreakoutContext)context; 41 | ctx.Transaction.Commit(); 42 | } 43 | 44 | void DisposeAction(IFreakoutContext context) 45 | { 46 | var ctx = (NpgsqlFreakoutContext)context; 47 | ctx.Transaction.Dispose(); 48 | ctx.Connection.Dispose(); 49 | } 50 | 51 | return new(provider, ContextFactory, CommitAction, DisposeAction); 52 | } 53 | } -------------------------------------------------------------------------------- /Freakout/FreakoutConfiguration.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Freakout.Config; 3 | using Freakout.Serialization; 4 | using Microsoft.Extensions.DependencyInjection; 5 | 6 | namespace Freakout; 7 | 8 | /// 9 | /// Base configuration class. Use a derived configuration class (e.g. "MsSqlFreakoutConfiguration" from the "Freakout.MsSql" NuGet package) to 10 | /// pass to . 11 | /// 12 | public abstract class FreakoutConfiguration 13 | { 14 | internal void InvokeConfigureServices(IServiceCollection services) => ConfigureServices(services); 15 | 16 | /// 17 | /// Must carry out the necessary registrations in to be able to work. 18 | /// At a minimum, this includes registrations for 19 | /// 20 | /// 21 | /// 22 | /// 23 | /// and then whatever stuff the selected type of persistence needs to do its thing. 24 | /// 25 | protected abstract void ConfigureServices(IServiceCollection services); 26 | 27 | /// 28 | /// Configures the poll interval, i.e. how long to wait between polling the store for pending commands. 29 | /// 30 | public TimeSpan OutboxPollInterval { get; set; } = TimeSpan.FromMinutes(1); 31 | 32 | /// 33 | /// Configures the command processing batch size, i.e. how many outbox commands to fetch, execute, and complete per batch. 34 | /// 35 | public int CommandProcessingBatchSize { get; set; } = 1; 36 | 37 | /// 38 | /// Configures the command serializer. Defaults to which uses System.Text.Json to serialize commands. 39 | /// 40 | public ICommandSerializer CommandSerializer { get; set; } = new SystemTextJsonCommandSerializer(); 41 | } -------------------------------------------------------------------------------- /Freakout.MsSql/Internals/MsSqlOutbox.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | 6 | // ReSharper disable UseAwaitUsing 7 | 8 | namespace Freakout.MsSql.Internals; 9 | 10 | /// 11 | /// This one should probably be changed into one that does not manage its own connection/transaction 12 | /// 13 | class MsSqlOutbox(MsSqlFreakoutConfiguration configuration, IFreakoutContextAccessor freakoutContextAccessor) : IOutbox 14 | { 15 | public void AddOutboxCommand(object command, Dictionary headers = null, CancellationToken cancellationToken = default) 16 | { 17 | if (command == null) throw new ArgumentNullException(nameof(command)); 18 | 19 | var context = freakoutContextAccessor.GetContext(); 20 | var transaction = context.Transaction; 21 | 22 | transaction.AddOutboxCommand( 23 | serializer: configuration.CommandSerializer, 24 | schemaName: configuration.SchemaName, 25 | tableName: configuration.TableName, 26 | command: command, 27 | headers: headers 28 | ); 29 | } 30 | 31 | public async Task AddOutboxCommandAsync(object command, Dictionary headers = null, CancellationToken cancellationToken = default) 32 | { 33 | if (command == null) throw new ArgumentNullException(nameof(command)); 34 | 35 | var context = freakoutContextAccessor.GetContext(); 36 | var transaction = context.Transaction; 37 | 38 | await transaction.AddOutboxCommandAsync( 39 | serializer: configuration.CommandSerializer, 40 | schemaName: configuration.SchemaName, 41 | tableName: configuration.TableName, 42 | command: command, 43 | headers: headers, 44 | cancellationToken: cancellationToken 45 | ); 46 | } 47 | } -------------------------------------------------------------------------------- /Freakout.Tests/TestFreakoutContextScope.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using Freakout.Internals; 3 | using NUnit.Framework; 4 | using Testy; 5 | 6 | namespace Freakout.Tests; 7 | 8 | [TestFixture] 9 | public class TestFreakoutContextScope : FixtureBase 10 | { 11 | [Test] 12 | public void ItWorks_SameThreadSameContext() 13 | { 14 | var context1 = new MyLittleContextThing(); 15 | var context2 = new MyLittleContextThing(); 16 | 17 | using (new FreakoutContextScope(context1)) 18 | { 19 | var ambientContext = new AsyncLocalFreakoutContextAccessor().GetContext(); 20 | 21 | Assert.That(ambientContext, Is.SameAs(context1)); 22 | } 23 | 24 | using (new FreakoutContextScope(context2)) 25 | { 26 | var ambientContext = new AsyncLocalFreakoutContextAccessor().GetContext(); 27 | 28 | Assert.That(ambientContext, Is.SameAs(context2)); 29 | } 30 | } 31 | 32 | [Test] 33 | public async Task ItWorks_ParallelAction() 34 | { 35 | var context1 = new MyLittleContextThing(); 36 | var context2 = new MyLittleContextThing(); 37 | 38 | async Task CheckTheContext(MyLittleContextThing context) 39 | { 40 | using (new FreakoutContextScope(context)) 41 | { 42 | await Task.Delay(millisecondsDelay: 200); 43 | 44 | var ambientContext = new AsyncLocalFreakoutContextAccessor().GetContext(); 45 | 46 | return ambientContext == context; 47 | } 48 | } 49 | 50 | var t1 = CheckTheContext(context1); 51 | var t2 = CheckTheContext(context2); 52 | 53 | var result1 = await t1; 54 | var result2 = await t2; 55 | 56 | Assert.That(result1, Is.True); 57 | Assert.That(result2, Is.True); 58 | } 59 | 60 | record MyLittleContextThing : IFreakoutContext; 61 | } -------------------------------------------------------------------------------- /Freakout/FreakoutContextScope.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using Freakout.Internals; 3 | 4 | namespace Freakout; 5 | 6 | /// 7 | /// Disposable scope struct that helps with managing an ambient Freakout context (i.e. an implementation of that comes with the chosen form of persistence). 8 | /// It may be used like this: 9 | /// 10 | /// 11 | /// var context = (...); 12 | /// 13 | /// using (new FreakoutContextScope(context)) 14 | /// { 15 | /// // in here IOutbox will work and enlist 16 | /// // its outbox commands in the ambient context 17 | /// } 18 | /// 19 | /// 20 | /// One possible use is within an ASP.NET Core middleware that manages an SqlConnection/SqlTransaction pair, this way enabling that 21 | /// they get passed to the IOutbox implementation that comes with Freakout.MsSql. 22 | /// 23 | public readonly struct FreakoutContextScope : IDisposable 24 | { 25 | readonly IFreakoutContext _previous = AsyncLocalFreakoutContextAccessor.Instance.Value; 26 | 27 | /// 28 | /// Creates the scope and establishes as the current ambient Freakout context. Any existing context will be remembered and restored when the scope is disposed. 29 | /// 30 | public FreakoutContextScope(IFreakoutContext context) 31 | { 32 | AsyncLocalFreakoutContextAccessor.Instance.Value = context ?? throw new ArgumentNullException(nameof(context)); 33 | 34 | if (context is IContextHooks contextHooks) 35 | { 36 | contextHooks.Mounted(); 37 | } 38 | } 39 | 40 | /// 41 | /// Removes the ambient context again and restores the previous scope. 42 | /// 43 | public void Dispose() 44 | { 45 | var context = AsyncLocalFreakoutContextAccessor.Instance.Value; 46 | 47 | AsyncLocalFreakoutContextAccessor.Instance.Value = _previous; 48 | 49 | if (context is IContextHooks contextHooks) 50 | { 51 | contextHooks.Unmounted(); 52 | } 53 | } 54 | } -------------------------------------------------------------------------------- /Freakout/Internals/Dispatchers/CompiledExpressionCommandDispatcher.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq.Expressions; 3 | using System.Reflection; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Microsoft.Extensions.DependencyInjection; 7 | 8 | namespace Freakout.Internals.Dispatchers; 9 | 10 | class CompiledExpressionCommandDispatcher(ICommandSerializer commandSerializer, IServiceScopeFactory serviceScopeFactory) : CommandDispatcher(commandSerializer, serviceScopeFactory) 11 | { 12 | /// 13 | /// This is how you build an invoker for a generic method using expression trees 14 | /// 15 | protected override Func CreateInvoker(Type commandType) 16 | { 17 | const string methodName = nameof(ExecuteOutboxCommandGeneric); 18 | 19 | var type = GetType(); 20 | 21 | // get method to call 22 | var methodInfo = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic) 23 | ?? throw new ArgumentException($"Could not get non-public instance method '{methodName}' from {type}"); 24 | 25 | // close the generic method 26 | var genericMethod = methodInfo.MakeGenericMethod(commandType); 27 | 28 | // get reference to this 29 | var instance = Expression.Constant(this); 30 | 31 | // get parameters 32 | var commandParameter = Expression.Parameter(typeof(object), "command"); 33 | var cancellationTokenParameter = Expression.Parameter(typeof(CancellationToken), "cancellationToken"); 34 | 35 | // and convert the System.Object input to commandType 36 | var commandConversion = Expression.Convert(commandParameter, commandType); 37 | 38 | // build the call 39 | var call = Expression.Call(instance, genericMethod, commandConversion, cancellationTokenParameter); 40 | 41 | // and wrap it in a lambda with a signature we can use 42 | var lambda = Expression.Lambda>(call, commandParameter, cancellationTokenParameter); 43 | 44 | return lambda.Compile(); 45 | } 46 | } -------------------------------------------------------------------------------- /Freakout.Marten/MartenOutboxExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using Freakout.Serialization; 5 | using Marten; 6 | using SequentialGuid; 7 | #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously 8 | 9 | namespace Freakout.Marten; 10 | 11 | public static class MartenOutboxExtensions 12 | { 13 | static readonly SystemTextJsonCommandSerializer FreakoutCommandSerializer = new(); 14 | 15 | public static IOutbox GetOutbox(this IDocumentSession session) 16 | { 17 | return new MartenOutboxWrapper(session, FreakoutCommandSerializer); 18 | } 19 | 20 | class MartenOutboxWrapper(IDocumentSession session, ICommandSerializer commandSerializer) : IOutbox 21 | { 22 | const string sql = """ 23 | INSERT INTO "public"."outbox_commands" ("id", "created_at", "headers", "payload") VALUES (?, CURRENT_TIMESTAMP, ?::jsonb, ?); 24 | """; 25 | 26 | public async Task AddOutboxCommandAsync(object command, Dictionary headers = null, CancellationToken cancellationToken = default) 27 | { 28 | AddOutboxCommand(command, headers, cancellationToken); 29 | } 30 | 31 | public void AddOutboxCommand(object command, Dictionary headers = null, CancellationToken cancellationToken = default) 32 | { 33 | var (headersToUse, payload) = commandSerializer.Serialize(command); 34 | 35 | if (headers != null) 36 | { 37 | InsertInto(headers, headersToUse); 38 | } 39 | 40 | var serializedHeaders = HeaderSerializer.SerializeToString(headersToUse); 41 | 42 | session.QueueSqlCommand(sql, SequentialGuidGenerator.Instance.NewGuid(), serializedHeaders, payload); 43 | } 44 | 45 | static void InsertInto(Dictionary source, Dictionary target) 46 | { 47 | foreach (var kvp in source) 48 | { 49 | target[kvp.Key] = kvp.Value; 50 | } 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /Freakout/Internals/TypeExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Concurrent; 2 | using System; 3 | using System.Text; 4 | 5 | namespace Freakout.Internals; 6 | 7 | static class TypeExtensions 8 | { 9 | static readonly ConcurrentDictionary SimpleAssemblyQualifiedTypeNameCache = new(); 10 | 11 | public static string GetSimpleAssemblyQualifiedName(this Type type) 12 | { 13 | if (type == null) throw new ArgumentNullException(nameof(type)); 14 | 15 | return SimpleAssemblyQualifiedTypeNameCache.GetOrAdd(type, GetSimpleAssemblyQualifiedNameInternal); 16 | } 17 | 18 | static string GetSimpleAssemblyQualifiedNameInternal(Type type) 19 | { 20 | if (type == null) throw new ArgumentNullException(nameof(type)); 21 | 22 | var simpleAssemblyQualifiedName = BuildSimpleAssemblyQualifiedName(type, new StringBuilder()).ToString(); 23 | 24 | // type lookups apparently accept "mscorlib" as an alias for System.Private.CoreLib, so we can make the "simple assembly-qualified type name" consistent across platforms like this 25 | return simpleAssemblyQualifiedName.Replace("System.Private.CoreLib", "mscorlib"); 26 | } 27 | 28 | static StringBuilder BuildSimpleAssemblyQualifiedName(Type type, StringBuilder sb) 29 | { 30 | if (!type.IsGenericType) 31 | { 32 | sb.Append($"{type.FullName}, {type.Assembly.GetName().Name}"); 33 | return sb; 34 | } 35 | 36 | if (!type.IsConstructedGenericType) 37 | { 38 | return sb; 39 | } 40 | 41 | var fullName = type.FullName ?? "???"; 42 | var requiredPosition = fullName.IndexOf("[", StringComparison.Ordinal); 43 | var name = fullName.Substring(0, requiredPosition); 44 | sb.Append($"{name}["); 45 | 46 | var arguments = type.GetGenericArguments(); 47 | for (var i = 0; i < arguments.Length; i++) 48 | { 49 | sb.Append(i == 0 ? "[" : ", ["); 50 | BuildSimpleAssemblyQualifiedName(arguments[i], sb); 51 | sb.Append("]"); 52 | } 53 | 54 | sb.Append($"], {type.Assembly.GetName().Name}"); 55 | 56 | return sb; 57 | } 58 | } -------------------------------------------------------------------------------- /Freakout.Testing/InMemFreakoutConfiguration.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using Freakout.Testing.Internals; 4 | using Microsoft.Extensions.DependencyInjection; 5 | // ReSharper disable RedundantTypeArgumentsOfMethod 6 | 7 | namespace Freakout.Testing; 8 | 9 | /// 10 | /// Freakout configuration for using an in-mem queue as the outbox. Useful in unit and integration testing scenarios 11 | /// where it would be nice to be able to inspect enqueued outbox commands. 12 | /// 13 | public class InMemFreakoutConfiguration : FreakoutConfiguration 14 | { 15 | /// 16 | /// Specifies whether serialization should be checked. Since this is pure in-mem messaging, it's entirely 17 | /// possible to bypass serializing/deserializing commands, but that would be less useful for testing, because 18 | /// serialization roundtripping errors would not be discovered. 19 | /// When TRUE, commands are roundtripped before being enqueued in the internal command queue. 20 | /// When FALSE, commands are just passed directly to the queue without any serialization. 21 | /// 22 | public bool CheckSerialization { get; set; } = true; 23 | 24 | /// 25 | /// Gets the in-mem command queue. 26 | /// 27 | public ConcurrentQueue Commands { get; } = new(); 28 | 29 | /// 30 | /// Raised when a command is enqueued. 31 | /// 32 | public event Action CommandAdded; 33 | 34 | /// 35 | protected override void ConfigureServices(IServiceCollection services) 36 | { 37 | services.AddSingleton(Commands); 38 | services.AddSingleton(); 39 | 40 | if (CheckSerialization) 41 | { 42 | services.AddScoped(p => new InMemOutboxDecorator(CommandSerializer, CreateInMemOutbox(p.GetRequiredService()))); 43 | } 44 | else 45 | { 46 | services.AddScoped(p => CreateInMemOutbox(p.GetRequiredService())); 47 | } 48 | 49 | InMemOutbox CreateInMemOutbox(IFreakoutContextAccessor fremFreakoutContextAccessor) 50 | { 51 | var inMemOutbox = new InMemOutbox(fremFreakoutContextAccessor, Commands); 52 | inMemOutbox.CommandAddedToQueue += cmd => CommandAdded?.Invoke(cmd); 53 | return inMemOutbox; 54 | } 55 | } 56 | } -------------------------------------------------------------------------------- /Freakout/OutboxCommandBatch.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections; 3 | using System.Collections.Generic; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Freakout.Internals; 7 | 8 | namespace Freakout; 9 | 10 | /// 11 | /// Represents a batch of store commands to be processed. When completed without errors, Freakout will call its completion method, which 12 | /// should cause the store to mark the contained commands as handled. 13 | /// 14 | public class OutboxCommandBatch(IFreakoutContext freakoutContext, IReadOnlyList outboxCommands, Func completeAsync, Action dispose) : IEnumerable, IDisposable 15 | { 16 | /// 17 | /// Gets an empty 18 | /// 19 | public static readonly OutboxCommandBatch Empty = new(new EmptyFreakoutContext(), Array.Empty(), _ => Task.CompletedTask, () => { }); 20 | 21 | /// 22 | /// "Completes" the batch, whatever that means ;) In most cases this will either mark the successfully executed commands as successfully executed, 23 | /// or simply delete all the executed commands, but it is entirely up to the implementation to provide the logic 24 | /// of what it means to complete the . 25 | /// This method is internal (at least for now), because it is called by . If one wants to affect completion logic, 26 | /// there's plenty of ways to do that, e.g. by decorating and wrap the in a customized 27 | /// with additional or different logic. 28 | /// 29 | internal Task CompleteAsync(CancellationToken cancellationToken = default) => completeAsync(cancellationToken); 30 | 31 | /// 32 | /// Gets the Freakout context that wraps the transaction that this command batch does its work in 33 | /// 34 | public IFreakoutContext FreakoutContext => freakoutContext; 35 | 36 | /// 37 | /// Gets an enumerator for the contained store commands 38 | /// 39 | public IEnumerator GetEnumerator() => outboxCommands.GetEnumerator(); 40 | 41 | IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); 42 | 43 | /// 44 | /// Disposes and cleans up whatever resources need to be cleaned up 45 | /// 46 | public void Dispose() => dispose(); 47 | } -------------------------------------------------------------------------------- /Freakout.MsSql.Tests/ThisIsWhatWeWant.cs: -------------------------------------------------------------------------------- 1 | using Freakout.Config; 2 | using Freakout.Tests; 3 | using Microsoft.Data.SqlClient; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using NUnit.Framework; 6 | using Testy; 7 | 8 | // ReSharper disable ClassNeverInstantiated.Local 9 | #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously 10 | 11 | namespace Freakout.MsSql.Tests; 12 | 13 | [TestFixture] 14 | public class ThisIsWhatWeWant : FixtureBase 15 | { 16 | string _connectionString; 17 | 18 | protected override void SetUp() 19 | { 20 | base.SetUp(); 21 | 22 | // we have a database 23 | _connectionString = MsSqlTestHelper.ConnectionString; 24 | } 25 | 26 | [Test] 27 | public async Task MakeItLookPrettyLikeThis() 28 | { 29 | var configuration = new MsSqlFreakoutConfiguration(_connectionString); 30 | 31 | // and a modern .NET app 32 | var services = new ServiceCollection(); 33 | services.AddFreakout(configuration); 34 | services.AddCommandHandler(); 35 | 36 | await using var provider = services.BuildServiceProvider(); 37 | 38 | using var cancellationTokenSource = new CancellationTokenSource(); 39 | _ = provider.RunBackgroundWorkersAsync(cancellationTokenSource.Token); 40 | 41 | // in our app we sometimes execute SQL stuff like this 42 | await AddOutboxCommandAsync(configuration, new PrintTextOutboxCommand(Text: "Howdy!")); 43 | 44 | await cancellationTokenSource.CancelAsync(); 45 | } 46 | 47 | async Task AddOutboxCommandAsync(MsSqlFreakoutConfiguration configuration, object command) 48 | { 49 | await using var connection = new SqlConnection(_connectionString); 50 | await connection.OpenAsync(); 51 | 52 | await using var transaction = await connection.BeginTransactionAsync(); 53 | await transaction.AddOutboxCommandAsync(configuration.CommandSerializer, configuration.SchemaName, configuration.TableName, command); 54 | await transaction.CommitAsync(); 55 | } 56 | 57 | /// 58 | /// This is a command 59 | /// 60 | record PrintTextOutboxCommand(string Text); 61 | 62 | /// 63 | /// This is a command handler 64 | /// 65 | class PrintTextOutboxCommandHandler : ICommandHandler 66 | { 67 | public async Task HandleAsync(PrintTextOutboxCommand command, CancellationToken cancellationToken) 68 | { 69 | Console.WriteLine(command.Text); 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /Freakout/Internals/Dispatchers/IlEmitCommandDispatcher.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Reflection; 3 | using System.Reflection.Emit; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Microsoft.Extensions.DependencyInjection; 7 | 8 | namespace Freakout.Internals.Dispatchers; 9 | 10 | class IlEmitCommandDispatcher(ICommandSerializer commandSerializer, IServiceScopeFactory serviceScopeFactory) : CommandDispatcher(commandSerializer, serviceScopeFactory) 11 | { 12 | /// 13 | /// This is how you build the same invoker using IL 14 | /// 15 | protected override Func CreateInvoker(Type commandType) 16 | { 17 | const string methodName = nameof(ExecuteOutboxCommandGeneric); 18 | 19 | // get method to call 20 | var type = GetType(); 21 | var methodInfo = type.GetMethod(methodName, BindingFlags.Instance | BindingFlags.NonPublic) 22 | ?? throw new ArgumentException($"Could not get non-public instance method '{methodName}' from {type}"); 23 | 24 | // close the generic method 25 | var genericMethod = methodInfo.MakeGenericMethod(commandType); 26 | 27 | // create dynamic method 28 | var dynamicMethod = new DynamicMethod( 29 | name: "DynamicInvoker", 30 | returnType: typeof(Task), 31 | parameterTypes: [typeof(object), typeof(object), typeof(CancellationToken)], 32 | m: type.Module 33 | ); 34 | 35 | // get IL generator 36 | var il = dynamicMethod.GetILGenerator(); 37 | 38 | // declare a local to store the converted command parameter 39 | il.DeclareLocal(commandType); 40 | 41 | // load "this" (first argument) onto the evaluation stack 42 | il.Emit(OpCodes.Ldarg_0); 43 | 44 | // load the command parameter (second argument) and convert it 45 | il.Emit(OpCodes.Ldarg_1); 46 | il.Emit(OpCodes.Stloc_0); // store converted parameter into local 47 | il.Emit(OpCodes.Ldloc_0); // load converted parameter from local 48 | 49 | // load the CancellationToken parameter (third argument) 50 | il.Emit(OpCodes.Ldarg_2); 51 | 52 | // call the generic method 53 | il.Emit(OpCodes.Callvirt, genericMethod); 54 | 55 | // return the result of the call 56 | il.Emit(OpCodes.Ret); 57 | 58 | // create a delegate from the dynamic method 59 | var invoker = (Func)dynamicMethod.CreateDelegate( 60 | typeof(Func)); 61 | 62 | // create a wrapper to match the required Func signature 63 | return (obj, token) => invoker(this, obj, token); 64 | } 65 | } -------------------------------------------------------------------------------- /Freakout/Internals/FreakoutBackgroundService.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using Microsoft.Extensions.Hosting; 5 | using Microsoft.Extensions.Logging; 6 | using Nito.AsyncEx; 7 | using Timer = System.Timers.Timer; 8 | // ReSharper disable UseAwaitUsing 9 | 10 | namespace Freakout.Internals; 11 | 12 | class FreakoutBackgroundService(FreakoutConfiguration configuration, IBatchDispatcher dispatcher, IOutboxCommandStore store, ILogger logger) : BackgroundService 13 | { 14 | readonly AsyncAutoResetEvent AutoResetEvent = new(); 15 | 16 | protected override async Task ExecuteAsync(CancellationToken stoppingToken) 17 | { 18 | using var _ = stoppingToken.Register(() => logger.LogInformation("Detected stop signal")); 19 | 20 | logger.LogInformation("Starting Freakout background worker with {interval} poll interval", configuration.OutboxPollInterval); 21 | 22 | using var timer = new Timer(configuration.OutboxPollInterval.TotalMilliseconds); 23 | 24 | timer.Elapsed += (_, _) => 25 | { 26 | logger.LogDebug("Triggering store poll"); 27 | AutoResetEvent.Set(); 28 | }; 29 | timer.Start(); 30 | 31 | try 32 | { 33 | while (!stoppingToken.IsCancellationRequested) 34 | { 35 | await AutoResetEvent.WaitAsync(stoppingToken); 36 | 37 | logger.LogDebug("Polling store"); 38 | 39 | try 40 | { 41 | using var batch = await store.GetPendingOutboxCommandsAsync(configuration.CommandProcessingBatchSize, stoppingToken); 42 | using var scope = new FreakoutContextScope(batch.FreakoutContext); 43 | 44 | await dispatcher.ExecuteAsync(batch, stoppingToken); 45 | 46 | await batch.CompleteAsync(stoppingToken); 47 | } 48 | catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) 49 | { 50 | // don't catch this 51 | throw; 52 | } 53 | catch (Exception exception) 54 | { 55 | logger.LogError(exception, "Error when executing store commands"); 56 | } 57 | } 58 | } 59 | catch (OperationCanceledException) when (stoppingToken.IsCancellationRequested) 60 | { 61 | // great 62 | } 63 | catch (Exception exception) 64 | { 65 | logger.LogError(exception, "Unhandled error in Frekout background worker"); 66 | } 67 | finally 68 | { 69 | logger.LogInformation("Freakout background worker stopped"); 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /Freakout/Serialization/SystemTextJsonCommandSerializer.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Text.Json; 4 | using Freakout.Internals; 5 | 6 | namespace Freakout.Serialization; 7 | 8 | /// 9 | /// Implementation of that uses System.Text.Json to do its thing. 10 | /// 11 | public class SystemTextJsonCommandSerializer : ICommandSerializer 12 | { 13 | const string JsonContentType = "application/json; encoding=utf-8"; 14 | 15 | /// 16 | /// Serializes the command as UTF8-encoded JSON and inserts the "short, assembly-qualified type name" as 17 | /// the header. Moreover, the header 18 | /// will be set to 'application/json; encoding=utf-8'. 19 | /// 20 | public OutboxCommand Serialize(object command) 21 | { 22 | if (command == null) throw new ArgumentNullException(nameof(command)); 23 | 24 | var type = command.GetType().GetSimpleAssemblyQualifiedName(); 25 | var payload = JsonSerializer.SerializeToUtf8Bytes(command); 26 | var headers = new Dictionary 27 | { 28 | [HeaderKeys.CommandType] = type, 29 | [HeaderKeys.ContentType] = JsonContentType, 30 | }; 31 | 32 | return new(headers, Payload: payload); 33 | } 34 | 35 | /// 36 | /// Deserializes the , assuming it contains JSON. Will check that the 37 | /// header has the value 'application/json; encoding=utf-8' and 38 | /// will determine which type to deserialize as by loading the .NET type specified by the 39 | /// header. 40 | /// 41 | public object Deserialize(OutboxCommand outboxCommand) 42 | { 43 | if (outboxCommand == null) throw new ArgumentNullException(nameof(outboxCommand)); 44 | 45 | var contentTypeHeader = outboxCommand.Headers.GetValueOrThrow(HeaderKeys.ContentType); 46 | 47 | if (!string.Equals(contentTypeHeader, JsonContentType)) 48 | { 49 | throw new FormatException($"The '{HeaderKeys.ContentType}' was '{contentTypeHeader}' and not '{JsonContentType}' as expected"); 50 | } 51 | 52 | var typeName = outboxCommand.Headers.GetValueOrThrow(HeaderKeys.CommandType); 53 | 54 | var type = LoadType(); 55 | 56 | var commandObject = JsonSerializer.Deserialize(outboxCommand.Payload, type); 57 | 58 | return commandObject; 59 | 60 | Type LoadType() 61 | { 62 | try 63 | { 64 | return Type.GetType(typeName) ?? throw new ArgumentException("Type was not found in the current app domain"); 65 | } 66 | catch (Exception exception) 67 | { 68 | throw new ArgumentException($"Could not load type '{typeName}'", exception); 69 | } 70 | } 71 | } 72 | } -------------------------------------------------------------------------------- /Freakout.Tests/Contracts/ExtensibilityTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading; 3 | using System.Threading.Tasks; 4 | using Freakout.Config; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using NUnit.Framework; 7 | using Polly; 8 | using Polly.Retry; 9 | using Testy; 10 | using Testy.Extensions; 11 | using Testy.General; 12 | // ReSharper disable ClassNeverInstantiated.Local 13 | // ReSharper disable AccessToDisposedClosure 14 | #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously 15 | 16 | namespace Freakout.Tests.Contracts; 17 | 18 | public abstract class ExtensibilityTests : FixtureBase where TFreakoutSystemFactory : IFreakoutSystemFactory, new() 19 | { 20 | TFreakoutSystemFactory _factory; 21 | 22 | protected override void SetUp() 23 | { 24 | base.SetUp(); 25 | 26 | _factory = Using(new TFreakoutSystemFactory()); 27 | } 28 | 29 | [Test] 30 | public async Task WorksWithPolly() 31 | { 32 | using var done = new ManualResetEvent(initialState: false); 33 | 34 | var stop = Using(new CancellationTokenSource()); 35 | 36 | Using(new DisposableCallback(stop.Cancel)); 37 | 38 | var system = _factory.Create(after: services => 39 | { 40 | services.AddSingleton(); 41 | services.AddSingleton(done); 42 | services.Decorate(); 43 | services.AddCommandHandler(); 44 | }); 45 | 46 | _ = system.StartCommandProcessorAsync(stop.Token); 47 | 48 | using (var scope = system.CreateScope()) 49 | { 50 | await system.Outbox.AddOutboxCommandAsync("HEJ", cancellationToken: stop.Token); 51 | scope.Complete(); 52 | } 53 | 54 | if (!done.WaitOne(TimeSpan.FromSeconds(5))) 55 | { 56 | throw new AssertionException("command was not handled within 5 s"); 57 | } 58 | } 59 | 60 | class CommandDispatchState 61 | { 62 | public int Count { get; set; } 63 | } 64 | 65 | class ThrowingCommandHandler(CommandDispatchState state, ManualResetEvent done) : ICommandHandler 66 | { 67 | public async Task HandleAsync(string command, CancellationToken cancellationToken) 68 | { 69 | state.Count++; 70 | 71 | if (state.Count < 4) 72 | { 73 | throw new AccessViolationException($"COUNT WAS {state.Count}"); 74 | } 75 | 76 | done.Set(); 77 | } 78 | } 79 | 80 | class PollyCommandDispatcher(ICommandDispatcher commandDispatcher) : ICommandDispatcher 81 | { 82 | readonly AsyncRetryPolicy RetryPolicy = Policy.Handle(e => e is not OperationCanceledException) 83 | .WaitAndRetryAsync(10, n => TimeSpan.FromMilliseconds(n * 2)); 84 | 85 | public async Task ExecuteAsync(OutboxCommand outboxCommand, CancellationToken cancellationToken = default) 86 | { 87 | await RetryPolicy.ExecuteAsync( 88 | action: token => commandDispatcher.ExecuteAsync(outboxCommand, token), 89 | cancellationToken: cancellationToken 90 | ); 91 | } 92 | } 93 | } -------------------------------------------------------------------------------- /Freakout.Tests/Contracts/OutboxCommandStoreTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using NUnit.Framework; 6 | using Testy; 7 | 8 | namespace Freakout.Tests.Contracts; 9 | 10 | public abstract class OutboxCommandStoreTests : FixtureBase where TFreakoutSystemFactory : IFreakoutSystemFactory, new() 11 | { 12 | FreakoutSystem _system; 13 | IOutboxCommandStore _store; 14 | ICommandSerializer _serializer; 15 | 16 | protected override void SetUp() 17 | { 18 | base.SetUp(); 19 | 20 | var factory = Using(new TFreakoutSystemFactory()); 21 | 22 | _system = factory.Create(); 23 | _store = _system.OutboxCommandStore; 24 | _serializer = _system.CommandSerializer; 25 | } 26 | 27 | [Test] 28 | public async Task CanGetEmptyBatch() 29 | { 30 | using var batch = await _store.GetPendingOutboxCommandsAsync(commandProcessingBatchSize: 100); 31 | 32 | await batch.CompleteAsync(); 33 | 34 | Assert.That(batch.Count(), Is.EqualTo(0)); 35 | } 36 | 37 | [Test] 38 | public async Task CanRoundtripCommand() 39 | { 40 | await AppendCommand(new LittleBittleCommand("hej")); 41 | 42 | using var batch = await _store.GetPendingOutboxCommandsAsync(commandProcessingBatchSize: 100); 43 | 44 | Assert.That(batch.Count(), Is.EqualTo(1)); 45 | 46 | var commandObject = _serializer.Deserialize(batch.First()); 47 | 48 | Assert.That(commandObject, Is.TypeOf()); 49 | 50 | var command = (LittleBittleCommand)commandObject; 51 | 52 | Assert.That(command.Text, Is.EqualTo("hej")); 53 | } 54 | 55 | [Test] 56 | public async Task OnlyCompletesSuccessfulCommands() 57 | { 58 | // append two commands 59 | await AppendCommand(new LittleBittleCommand("MUST SUCCEED"), new() { ["special"] = "must succeed" }); 60 | await AppendCommand(new LittleBittleCommand("MUST FAIL"), new() { ["special"] = "must fail" }); 61 | 62 | // get the batch 63 | using var batch1 = await _store.GetPendingOutboxCommandsAsync(commandProcessingBatchSize: 100); 64 | 65 | foreach (var cmd in batch1) 66 | { 67 | if (!cmd.Headers.TryGetValue("special", out var special)) continue; 68 | 69 | // find special mark and mark the commands accordingly 70 | switch (special) 71 | { 72 | case "must succeed": cmd.SetState(new SuccessfullyExecutedCommandState(TimeSpan.FromSeconds(1))); break; 73 | case "must fail": cmd.SetState(new FailedCommandState(TimeSpan.FromSeconds(1), new ArgumentException("blah"))); break; 74 | } 75 | } 76 | 77 | await batch1.CompleteAsync(); 78 | 79 | // get another batch - should only contain the command marked as failed before 80 | using var batch2 = await _store.GetPendingOutboxCommandsAsync(commandProcessingBatchSize: 100); 81 | 82 | Assert.That(batch2.Count(), Is.EqualTo(1)); 83 | 84 | var commandObject = _serializer.Deserialize(batch2.First()); 85 | Assert.That(commandObject, Is.TypeOf()); 86 | var command = (LittleBittleCommand)commandObject; 87 | Assert.That(command.Text, Is.EqualTo("MUST FAIL")); 88 | 89 | } 90 | 91 | record LittleBittleCommand(string Text); 92 | 93 | async Task AppendCommand(object command, Dictionary headers = null) 94 | { 95 | using var scope = _system.CreateScope(); 96 | await _system.Outbox.AddOutboxCommandAsync(command, headers: headers); 97 | scope.Complete(); 98 | } 99 | } -------------------------------------------------------------------------------- /Freakout.Testing.Tests/CheckHowItLooks.cs: -------------------------------------------------------------------------------- 1 | using System.Runtime.Serialization; 2 | using Freakout.Config; 3 | using Freakout.Serialization; 4 | using Microsoft.Extensions.DependencyInjection; 5 | using NUnit.Framework; 6 | using Testy; 7 | using Testy.Extensions; 8 | 9 | namespace Freakout.Testing.Tests; 10 | 11 | [TestFixture] 12 | public class CheckHowItLooks : FixtureBase 13 | { 14 | [Test] 15 | public async Task ThisIsHowItShouldWOrk_Callback() 16 | { 17 | var taskCompletionSource = new TaskCompletionSource(); 18 | 19 | var configuration = new InMemFreakoutConfiguration(); 20 | configuration.CommandAdded += cmd => Task.Run(() => taskCompletionSource.SetResult(cmd)); 21 | 22 | var services = new ServiceCollection(); 23 | 24 | services.AddFreakout(configuration); 25 | 26 | await using var provider = services.BuildServiceProvider(); 27 | 28 | using (new FreakoutContextScope(new InMemFreakoutContext())) 29 | { 30 | await provider.GetRequiredService().AddOutboxCommandAsync(new MyCommand("hello there 🙂")); 31 | } 32 | 33 | await taskCompletionSource.Task.WaitAsync(TimeSpan.FromSeconds(1)); 34 | } 35 | 36 | [Test] 37 | public async Task ThisIsHowItShouldWOrk_BuiltInQueue() 38 | { 39 | var configuration = new InMemFreakoutConfiguration(); 40 | var commands = configuration.Commands; 41 | 42 | var services = new ServiceCollection(); 43 | 44 | services.AddFreakout(configuration); 45 | 46 | await using var provider = services.BuildServiceProvider(); 47 | 48 | using (new FreakoutContextScope(new InMemFreakoutContext())) 49 | { 50 | await provider.GetRequiredService().AddOutboxCommandAsync(new MyCommand("hello there 🙂")); 51 | } 52 | 53 | Assert.That(commands.Count, Is.EqualTo(1)); 54 | 55 | var command = commands.First(); 56 | Assert.That(command.Command, Is.TypeOf()); 57 | Assert.That(command.Command, Is.EqualTo(new MyCommand("hello there 🙂"))); 58 | } 59 | 60 | record MyCommand(string Text); 61 | 62 | [Test] 63 | public async Task SerializationError_CanBeDetected() 64 | { 65 | var services = new ServiceCollection(); 66 | 67 | services.AddFreakout(new InMemFreakoutConfiguration()); 68 | 69 | await using var provider = services.BuildServiceProvider(); 70 | 71 | using var _ = new FreakoutContextScope(new InMemFreakoutContext()); 72 | 73 | var exception = Assert.ThrowsAsync(() => 74 | provider.GetRequiredService().AddOutboxCommandAsync(new CannotBeRoundtripped(123))); 75 | 76 | Console.WriteLine(exception); 77 | 78 | var details = exception!.ToString(); 79 | 80 | Assert.That(details, Contains.Substring(nameof(SystemTextJsonCommandSerializer))); 81 | Assert.That(details, Contains.Substring("Serialization check failed")); 82 | } 83 | 84 | [Test] 85 | public async Task SerializationError_SerializationCanBeBypassed() 86 | { 87 | var services = new ServiceCollection(); 88 | 89 | services.AddFreakout(new InMemFreakoutConfiguration { CheckSerialization = false }); 90 | 91 | await using var provider = services.BuildServiceProvider(); 92 | 93 | using (new FreakoutContextScope(new InMemFreakoutContext())) 94 | { 95 | await provider.GetRequiredService().AddOutboxCommandAsync(new CannotBeRoundtripped(123)); 96 | } 97 | } 98 | 99 | class CannotBeRoundtripped(int wrongType) 100 | { 101 | public string WrongType { get; } = wrongType.ToString(); 102 | } 103 | } -------------------------------------------------------------------------------- /Freakout.MsSql/FreakoutSqlConnectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Data.Common; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Freakout.MsSql.Internals; 7 | using Freakout.Serialization; 8 | using Microsoft.Data.SqlClient; 9 | using SequentialGuid; 10 | 11 | // ReSharper disable UseAwaitUsing 12 | 13 | namespace Freakout.MsSql; 14 | 15 | /// 16 | /// Relevant extension methods for working with store commands in Microsoft SQL Server 17 | /// 18 | public static class FreakoutSqlConnectionExtensions 19 | { 20 | /// 21 | /// Adds the given to the store as part of the SQL transaction. The command will be added to the store 22 | /// when the transaction is committed. 23 | /// 24 | public static void AddOutboxCommand(this DbTransaction transaction, ICommandSerializer serializer, string schemaName, string tableName, object command, Dictionary headers = null) 25 | { 26 | if (transaction == null) throw new ArgumentNullException(nameof(transaction)); 27 | if (serializer == null) throw new ArgumentNullException(nameof(serializer)); 28 | if (command == null) throw new ArgumentNullException(nameof(command)); 29 | 30 | var serializedCommand = serializer.Serialize(command); 31 | 32 | var payload = serializedCommand.Payload; 33 | var headersToUse = serializedCommand.Headers; 34 | 35 | headers?.InsertInto(headersToUse); 36 | 37 | Insert(schemaName, tableName, transaction, HeaderSerializer.SerializeToString(headersToUse), payload); 38 | } 39 | 40 | /// 41 | /// Adds the given to the store as part of the SQL transaction. The command will be added to the store 42 | /// when the transaction is committed. 43 | /// 44 | public static async Task AddOutboxCommandAsync(this DbTransaction transaction, ICommandSerializer serializer, string schemaName, string tableName, object command, Dictionary headers = null, CancellationToken cancellationToken = default) 45 | { 46 | if (transaction == null) throw new ArgumentNullException(nameof(transaction)); 47 | if (serializer == null) throw new ArgumentNullException(nameof(serializer)); 48 | if (command == null) throw new ArgumentNullException(nameof(command)); 49 | 50 | var serializedCommand = serializer.Serialize(command); 51 | 52 | var payload = serializedCommand.Payload; 53 | var headersToUse = serializedCommand.Headers; 54 | 55 | headers?.InsertInto(headersToUse); 56 | 57 | await InsertAsync(schemaName, tableName, transaction, HeaderSerializer.SerializeToString(headersToUse), payload, cancellationToken); 58 | } 59 | 60 | static void Insert(string schemaName, string tableName, DbTransaction transaction, string headers, byte[] bytes) 61 | { 62 | var connection = transaction.Connection ?? throw new ArgumentException($"The {transaction} did not have a DbConnection on it!"); 63 | 64 | using var cmd = connection.CreateCommand(); 65 | 66 | cmd.Transaction = transaction; 67 | 68 | SetQueryAndParameters(schemaName, tableName, headers, bytes, cmd); 69 | 70 | cmd.ExecuteNonQuery(); 71 | } 72 | 73 | static async Task InsertAsync(string schemaName, string tableName, DbTransaction transaction, string headers, byte[] bytes, CancellationToken cancellationToken) 74 | { 75 | var connection = transaction.Connection ?? throw new ArgumentException($"The {transaction} did not have a DbConnection on it!"); 76 | 77 | using var cmd = connection.CreateCommand(); 78 | 79 | cmd.Transaction = transaction; 80 | 81 | SetQueryAndParameters(schemaName, tableName, headers, bytes, cmd); 82 | 83 | await cmd.ExecuteNonQueryAsync(cancellationToken); 84 | } 85 | 86 | static void SetQueryAndParameters(string schemaName, string tableName, string headers, byte[] bytes, DbCommand cmd) 87 | { 88 | cmd.CommandText = $"INSERT INTO [{schemaName}].[{tableName}] ([Id], [CreatedAt], [Headers], [Payload]) VALUES (@id, SYSDATETIMEOFFSET(), @headers, @payload)"; 89 | cmd.Parameters.Add(new SqlParameter("id", SequentialGuidGenerator.Instance.NewGuid())); 90 | cmd.Parameters.Add(new SqlParameter("headers", headers)); 91 | cmd.Parameters.Add(new SqlParameter("payload", bytes)); 92 | } 93 | } -------------------------------------------------------------------------------- /Freakout.NpgSql/FreakoutNpgsqlConnectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Data.Common; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Freakout.NpgSql.Internals; 7 | using Freakout.Serialization; 8 | using Npgsql; 9 | using NpgsqlTypes; 10 | using SequentialGuid; 11 | // ReSharper disable UseAwaitUsing 12 | 13 | namespace Freakout.NpgSql; 14 | 15 | /// 16 | /// Relevant extension methods for working with store commands in Microsoft SQL Server 17 | /// 18 | public static class FreakoutNpgsqlConnectionExtensions 19 | { 20 | /// 21 | /// Adds the given to the store as part of the SQL transaction. The command will be added to the store 22 | /// when the transaction is committed. 23 | /// 24 | public static void AddOutboxCommand(this DbTransaction transaction, string schemaName, string tableName, ICommandSerializer serializer, object command, Dictionary headers = null) 25 | { 26 | if (transaction == null) throw new ArgumentNullException(nameof(transaction)); 27 | if (serializer == null) throw new ArgumentNullException(nameof(serializer)); 28 | if (command == null) throw new ArgumentNullException(nameof(command)); 29 | 30 | var serializedCommand = serializer.Serialize(command); 31 | 32 | var payload = serializedCommand.Payload; 33 | var headersToUse = serializedCommand.Headers; 34 | 35 | headers?.InsertInto(headersToUse); 36 | 37 | Insert(schemaName, tableName, transaction, HeaderSerializer.SerializeToString(headersToUse), payload); 38 | } 39 | 40 | /// 41 | /// Adds the given to the store as part of the SQL transaction. The command will be added to the store 42 | /// when the transaction is committed. 43 | /// 44 | public static async Task AddOutboxCommandAsync(this DbTransaction transaction, string schemaName, string tableName, ICommandSerializer serializer, object command, Dictionary headers = null, CancellationToken cancellationToken = default) 45 | { 46 | if (transaction == null) throw new ArgumentNullException(nameof(transaction)); 47 | if (serializer == null) throw new ArgumentNullException(nameof(serializer)); 48 | if (command == null) throw new ArgumentNullException(nameof(command)); 49 | 50 | var serializedCommand = serializer.Serialize(command); 51 | 52 | var payload = serializedCommand.Payload; 53 | var headersToUse = serializedCommand.Headers; 54 | 55 | headers?.InsertInto(headersToUse); 56 | 57 | await InsertAsync(schemaName, tableName, transaction, HeaderSerializer.SerializeToString(headersToUse), payload, cancellationToken); 58 | } 59 | 60 | static void Insert(string schemaName, string tableName, DbTransaction transaction, string headers, byte[] bytes) 61 | { 62 | var connection = transaction.Connection ?? throw new ArgumentException($"The {transaction} did not have a DbConnection on it!"); 63 | 64 | using var cmd = connection.CreateCommand(); 65 | 66 | cmd.Transaction = transaction; 67 | 68 | SetQueryAndParameters(schemaName, tableName, headers, bytes, cmd); 69 | 70 | cmd.ExecuteNonQuery(); 71 | } 72 | 73 | static async Task InsertAsync(string schemaName, string tableName, DbTransaction transaction, string headers, byte[] bytes, CancellationToken cancellationToken) 74 | { 75 | var connection = transaction.Connection ?? throw new ArgumentException($"The {transaction} did not have a DbConnection on it!"); 76 | 77 | using var cmd = connection.CreateCommand(); 78 | 79 | cmd.Transaction = transaction; 80 | 81 | SetQueryAndParameters(schemaName, tableName, headers, bytes, cmd); 82 | 83 | await cmd.ExecuteNonQueryAsync(cancellationToken); 84 | } 85 | 86 | static void SetQueryAndParameters(string schemaName, string tableName, string headers, byte[] bytes, DbCommand cmd) 87 | { 88 | cmd.CommandText = $@"INSERT INTO ""{schemaName}"".""{tableName}"" (""id"", ""created_at"", ""headers"", ""payload"") VALUES (@id, CURRENT_TIMESTAMP, @headers, @payload);"; 89 | 90 | cmd.Parameters.Add(new NpgsqlParameter("id", NpgsqlDbType.Uuid) { Value = SequentialGuidGenerator.Instance.NewGuid() }); 91 | cmd.Parameters.Add(new NpgsqlParameter("headers", NpgsqlDbType.Jsonb) { Value = headers }); 92 | cmd.Parameters.Add(new NpgsqlParameter("payload", NpgsqlDbType.Bytea) { Value = bytes }); 93 | } 94 | } -------------------------------------------------------------------------------- /Freakout.NpgSql/Internals/NpgSqlOutboxCommandStore.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Freakout.Serialization; 7 | using Nito.Disposables; 8 | using Npgsql; 9 | // ReSharper disable MethodHasAsyncOverloadWithCancellation 10 | // ReSharper disable UseAwaitUsing 11 | 12 | namespace Freakout.NpgSql.Internals; 13 | 14 | class NpgsqlOutboxCommandStore(string connectionString, string tableName, string schemaName) : IOutboxCommandStore 15 | { 16 | public async Task GetPendingOutboxCommandsAsync(int commandProcessingBatchSize, CancellationToken cancellationToken = default) 17 | { 18 | // Collect disposables in this one 👇 Remember to consider disposal in all possible exit paths from this method!! 19 | var disposables = new CollectionDisposable(); 20 | 21 | var connection = new NpgsqlConnection(connectionString); 22 | disposables.Add(connection); 23 | 24 | try 25 | { 26 | await connection.OpenAsync(cancellationToken); 27 | 28 | var transaction = connection.BeginTransaction(); 29 | disposables.Add(transaction); 30 | 31 | using var command = connection.CreateCommand(); 32 | 33 | var query = $"""SELECT * FROM "{schemaName}"."{tableName}" WHERE "completed" = FALSE ORDER BY "id" FOR UPDATE SKIP LOCKED LIMIT {commandProcessingBatchSize};"""; 34 | 35 | command.CommandText = query; 36 | command.Transaction = transaction; 37 | 38 | await using var reader = await command.ExecuteReaderAsync(cancellationToken); 39 | 40 | var outboxCommands = new List(); 41 | 42 | while (await reader.ReadAsync(cancellationToken)) 43 | { 44 | var id = (Guid)reader["id"]; 45 | var createdAt = new DateTimeOffset((DateTime)reader["created_at"]); 46 | var headers = HeaderSerializer.DeserializeFromString((string)reader["headers"]); 47 | var payload = (byte[])reader["payload"]; 48 | 49 | outboxCommands.Add(new PendingOutboxCommand(id, createdAt, headers, payload)); 50 | } 51 | 52 | if (!outboxCommands.Any()) 53 | { 54 | // EXIT - dispose 55 | disposables.Dispose(); 56 | return OutboxCommandBatch.Empty; 57 | } 58 | 59 | // EXIT - dispose deferred to dispose callback 60 | return new OutboxCommandBatch( 61 | freakoutContext: new NpgsqlFreakoutContext(connection, transaction), 62 | outboxCommands: outboxCommands, 63 | completeAsync: token => CompleteAsync(connection, transaction, outboxCommands, token), 64 | dispose: disposables.Dispose 65 | ); 66 | } 67 | catch (Exception exception) 68 | { 69 | // EXIT - dispose 70 | disposables.Dispose(); 71 | throw new ApplicationException("Could not get store tasks", exception); 72 | } 73 | } 74 | 75 | async Task CompleteAsync(NpgsqlConnection connection, NpgsqlTransaction transaction, List outboxCommands, CancellationToken cancellationToken) 76 | { 77 | var completedIds = outboxCommands 78 | .Where(c => c.State is SuccessfullyExecutedCommandState) 79 | .Select(c => $"'{c.Id}'") 80 | .ToList(); 81 | 82 | if (!completedIds.Any()) return; 83 | 84 | var idString = string.Join(",", completedIds); 85 | 86 | using var command = connection.CreateCommand(); 87 | command.CommandText = $@"UPDATE ""{schemaName}"".""{tableName}"" SET ""completed"" = TRUE, ""executed_at"" = CURRENT_TIMESTAMP WHERE ""id"" IN ({idString});"; 88 | command.Transaction = transaction; 89 | await command.ExecuteNonQueryAsync(cancellationToken); 90 | 91 | await transaction.CommitAsync(cancellationToken); 92 | } 93 | 94 | public void CreateSchema() 95 | { 96 | using var connection = new NpgsqlConnection(connectionString); 97 | 98 | connection.Open(); 99 | 100 | using var command = connection.CreateCommand(); 101 | 102 | command.CommandText = $@" 103 | 104 | CREATE TABLE IF NOT EXISTS ""{schemaName}"".""{tableName}"" ( 105 | ""id"" UUID PRIMARY KEY, 106 | ""created_at"" TIMESTAMPTZ NOT NULL, 107 | ""headers"" JSONB, 108 | ""payload"" BYTEA, 109 | ""completed"" BOOLEAN NOT NULL DEFAULT FALSE, 110 | ""executed_at"" TIMESTAMPTZ NULL 111 | ); 112 | 113 | "; 114 | 115 | command.ExecuteNonQuery(); 116 | 117 | } 118 | } -------------------------------------------------------------------------------- /Freakout.MsSql/Internals/MsSqlOutboxCommandStore.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Freakout.Serialization; 7 | using Microsoft.Data.SqlClient; 8 | using Nito.Disposables; 9 | // ReSharper disable AccessToDisposedClosure 10 | // ReSharper disable UseAwaitUsing 11 | 12 | namespace Freakout.MsSql.Internals; 13 | 14 | class MsSqlOutboxCommandStore(string connectionString, string tableName, string schemaName) : IOutboxCommandStore 15 | { 16 | public async Task GetPendingOutboxCommandsAsync(int commandProcessingBatchSize, CancellationToken cancellationToken = default) 17 | { 18 | // Collect disposables in this one 👇 Remember to consider disposal in all possible exit paths from this method!! 19 | var disposables = new CollectionDisposable(); 20 | 21 | var connection = new SqlConnection(connectionString); 22 | disposables.Add(connection); 23 | 24 | try 25 | { 26 | await connection.OpenAsync(cancellationToken); 27 | 28 | var transaction = connection.BeginTransaction(); 29 | disposables.Add(transaction); 30 | 31 | using var command = connection.CreateCommand(); 32 | 33 | var query = $"SELECT TOP {commandProcessingBatchSize} * FROM [{schemaName}].[{tableName}] WITH (ROWLOCK, UPDLOCK, READPAST) WHERE [Completed] = 0 ORDER BY [Id]"; 34 | 35 | command.CommandText = query; 36 | command.Transaction = transaction; 37 | 38 | using var reader = await command.ExecuteReaderAsync(cancellationToken); 39 | 40 | var outboxCommands = new List(); 41 | 42 | while (await reader.ReadAsync(cancellationToken)) 43 | { 44 | var id = (Guid)reader["Id"]; 45 | var createdAt = (DateTimeOffset)reader["CreatedAt"]; 46 | var headers = HeaderSerializer.DeserializeFromString((string)reader["Headers"]); 47 | var payload = (byte[])reader["Payload"]; 48 | 49 | outboxCommands.Add(new PendingOutboxCommand(id, createdAt, headers, payload)); 50 | } 51 | 52 | if (!outboxCommands.Any()) 53 | { 54 | // EXIT - dispose 55 | disposables.Dispose(); 56 | return OutboxCommandBatch.Empty; 57 | } 58 | 59 | // EXIT - disposal deferred to dispose callback 60 | return new OutboxCommandBatch( 61 | freakoutContext: new MsSqlFreakoutContext(connection, transaction), 62 | outboxCommands: outboxCommands, 63 | completeAsync: token => CompleteAsync(connection, transaction, outboxCommands, token), 64 | dispose: disposables.Dispose 65 | ); 66 | } 67 | catch (Exception exception) 68 | { 69 | // EXIT - dispose 70 | disposables.Dispose(); 71 | throw new ApplicationException("Could not get store tasks", exception); 72 | } 73 | } 74 | 75 | async Task CompleteAsync(SqlConnection connection, SqlTransaction transaction, List outboxCommands, CancellationToken cancellationToken) 76 | { 77 | var completedIds = outboxCommands 78 | .Where(c => c.State is SuccessfullyExecutedCommandState) 79 | .Select(c => $"'{c.Id}'") 80 | .ToList(); 81 | 82 | if (!completedIds.Any()) return; 83 | 84 | var idString = string.Join(",", completedIds); 85 | 86 | using var command = connection.CreateCommand(); 87 | command.CommandText = $"UPDATE [{schemaName}].[{tableName}] SET [Completed] = 1, [ExecutedAt] = SYSDATETIMEOFFSET() WHERE [Id] IN ({idString})"; 88 | command.Transaction = transaction; 89 | await command.ExecuteNonQueryAsync(cancellationToken); 90 | 91 | transaction.Commit(); 92 | } 93 | 94 | public void CreateSchema() 95 | { 96 | using var connection = new SqlConnection(connectionString); 97 | 98 | connection.Open(); 99 | 100 | using var command = connection.CreateCommand(); 101 | 102 | command.CommandText = $@" 103 | 104 | IF NOT EXISTS (SELECT TOP 1 * FROM sys.tables t JOIN sys.schemas s ON t.schema_id = s.schema_id WHERE t.name = '{tableName}' AND s.name = '{schemaName}') 105 | BEGIN 106 | CREATE TABLE [{schemaName}].[{tableName}] ( 107 | [Id] UNIQUEIDENTIFIER, 108 | [CreatedAt] DATETIMEOFFSET(3) NOT NULL, 109 | [Headers] NVARCHAR(MAX), 110 | [Payload] VARBINARY(MAX), 111 | [Completed] BIT NOT NULL DEFAULT(0), 112 | [ExecutedAt] DATETIMEOFFSET(3) NULL, 113 | 114 | PRIMARY KEY ([Id]) 115 | ) 116 | END 117 | 118 | "; 119 | 120 | command.ExecuteNonQuery(); 121 | } 122 | } -------------------------------------------------------------------------------- /Freakout/Config/FreakoutServiceCollectionExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Linq; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using Freakout.Internals; 6 | using Freakout.Internals.Dispatchers; 7 | using Microsoft.Extensions.DependencyInjection; 8 | using Microsoft.Extensions.DependencyInjection.Extensions; 9 | // ReSharper disable SimplifyLinqExpressionUseAll 10 | 11 | namespace Freakout.Config; 12 | 13 | /// 14 | /// Configuration extensions for Freakout 15 | /// 16 | public static class FreakoutServiceCollectionExtensions 17 | { 18 | /// 19 | /// Adds Freakout to the container, using the given . 20 | /// 21 | public static void AddFreakout(this IServiceCollection services, FreakoutConfiguration configuration) 22 | { 23 | if (services == null) throw new ArgumentNullException(nameof(services)); 24 | if (configuration == null) throw new ArgumentNullException(nameof(configuration)); 25 | 26 | services.AddSingleton(configuration); 27 | 28 | services.AddHostedService(p => 29 | { 30 | var freakoutBackgroundService = new FreakoutBackgroundService( 31 | configuration: configuration, 32 | dispatcher: p.GetRequiredService(), 33 | store: p.GetRequiredService(), 34 | logger: p.GetLoggerFor() 35 | ); 36 | return freakoutBackgroundService; 37 | }); 38 | 39 | services.AddSingleton(configuration.CommandSerializer); 40 | 41 | services.TryAddSingleton(); 42 | 43 | services.TryAddSingleton(p => new DefaultBatchDispatcher( 44 | commandDispatcher: p.GetRequiredService(), 45 | logger: p.GetLoggerFor() 46 | )); 47 | 48 | // let the concrete configuration make its registrations 49 | configuration.InvokeConfigureServices(services); 50 | 51 | // must have IOutboxCommandStore implementation now 52 | if (!services.Any(s => s.ServiceType == typeof(IOutboxCommandStore))) 53 | { 54 | throw new ApplicationException($"The configuration {configuration} did not make the necessary IOutboxCommandStore registration"); 55 | } 56 | 57 | // must have IOutbox implementation now 58 | if (!services.Any(s => s.ServiceType == typeof(IOutbox))) 59 | { 60 | throw new ApplicationException($"The configuration {configuration} did not make the necessary IOutbox registration"); 61 | } 62 | 63 | services.TryAddSingleton(); 64 | } 65 | 66 | /// 67 | /// Adds the type as a Freakout command handler, using the invoker function to invoke it. 68 | /// 69 | public static void AddCommandHandler(this IServiceCollection services, Func invoker) where TCommandHandler : class 70 | { 71 | if (services == null) throw new ArgumentNullException(nameof(services)); 72 | if (invoker == null) throw new ArgumentNullException(nameof(invoker)); 73 | 74 | var serviceType = typeof(ICommandHandler<>).MakeGenericType(typeof(TCommand)); 75 | 76 | services.AddScoped(); 77 | services.AddScoped(serviceType, p => new DelegatingCommandHandler((cmd, token) => invoker(p.GetRequiredService(), cmd, token))); 78 | } 79 | 80 | /// 81 | /// Adds the type as a Freakout command handler. 82 | /// It is required that implements one or more closed with a compatible type. 83 | /// 84 | public static void AddCommandHandler(this IServiceCollection services) where TCommandHandler : class, ICommandHandler 85 | { 86 | if (services == null) throw new ArgumentNullException(nameof(services)); 87 | 88 | var commandTypes = typeof(TCommandHandler) 89 | .GetInterfaces() 90 | .Where(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(ICommandHandler<>)) 91 | .Select(i => i.GetGenericArguments().First()) 92 | .ToList(); 93 | 94 | if (!commandTypes.Any()) 95 | { 96 | throw new ArgumentException( 97 | $"The type {typeof(TCommandHandler)} cannot be registered as a command handler, because it doesn't implement ICommandHandler"); 98 | } 99 | 100 | foreach (var commandType in commandTypes) 101 | { 102 | var serviceType = typeof(ICommandHandler<>).MakeGenericType(commandType); 103 | 104 | services.AddScoped(serviceType, typeof(TCommandHandler)); 105 | } 106 | } 107 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Freakout 2 | 3 | 📤 Just a general outbox thing 4 | 5 | 6 | ## What do you mean by "outbox"? 7 | 8 | An outbox is a simple mechanism that allows you to store outbound messages in an atomic ☢️ way as part of your normal transaction. 9 | 10 | It addresses the ubiquitous dilemma of which to do first: (1) commit your database transaction OR (2) whatever else you want to do. 11 | 12 | The problem is that (1) is generally inherently risky (database deadlocks, key violations, etc etc), and (2) is also generally risky (writing to a file share, communicating with a message broker, calling an external web service, etc.). 13 | 14 | Two risky things combined are very likely to fail! - which will leave your system in a state where either (1) or (2) was not carried out (depending on the order you chose to execute them in), because it failed. 15 | 16 | 🆘 This problem can be addressed in many ways, e.g. by retrying, using a middleman with higher availability (e.g. a message broker), etc. etc. 17 | 18 | OR you could simply store information about which actions to perform in (2) as part of (1)!! 🤯 That's what the outbox does! 🤓 19 | 20 | 21 | ## Until now, I have only seen outboxes built into messaging libraries - why now a separate library? 22 | 23 | Why? Because the "outbox" is closer to your chosen type of persistence (SQL Server, Postgres, etc.) than to anything else, and it can do much more than sending messages. 24 | 25 | 26 | ## Which types of persistence does it support? 27 | 28 | 1. Microsoft SQL Server (for when you're working with the "Microsoft.Data.SqlClient" NuGet package and `SqlConnection`/`SqlTransaction`) 29 | 1. PostgreSQL (for when you're working with "Npgsql" NuGet package and `NpgsqlConnection`/`NpgsqlTransaction`) 30 | 31 | and that's it for now. 😅 32 | 33 | 34 | ## How? 35 | 36 | First, pull in the relevant NuGet package for your chosen type of persistence - e.g. the "Freakout.MsSql" NuGet package. 37 | 38 | Next, enable Freakout in your app: 39 | 40 | ```csharp 41 | services.AddFreakout(new MsSqlFreakoutConfiguration(connectionString)); 42 | ``` 43 | 44 | It will register a couple of things, e.g. a background worker that will poll the outbox for pending commands. `AddFreakout` can only be called once. 45 | 46 | Then, add your handlers: 47 | 48 | ```csharp 49 | services.AddCommandHandler(); 50 | services.AddCommandHandler(); 51 | services.AddCommandHandler(); 52 | ``` 53 | 54 | which will of course be resolved from the container, each in their own service scope. 55 | 56 | Now it's fully configured - what's missing is putting something in the outbox. 57 | 58 | ## Two ways of adding commands to the outbox 59 | 60 | ### First way: By using `IOutbox` 61 | 62 | This is the neat way to do it: You can manage your unit of work with your `SqlConnection` and `SqlTransaction` somewhere 63 | and then make them available to Freakout by using a `FreakoutContextScope` like this: 64 | 65 | ```csharp 66 | var context = new MsSqlFreakoutContext(connection, transaction); 67 | 68 | using (new FreakoutContextScope(context)) 69 | { 70 | // there's an ambient context now! 🙂 71 | } 72 | ``` 73 | 74 | Inside the scope, `IOutbox` can be resolved, which then provides a technology-agnostic way of putting commands in the outbox! 75 | 76 | A cool place to create/dispose `FreakoutContextScope` would be in your ASP.NET Core request handler pipeline, e.g. like this: 77 | 78 | ```csharp 79 | app.Use(async (context, next) => { 80 | var provider = context.Request.RequestServices; 81 | 82 | // let's just assume we can get these from the request-scoped services: 83 | var connection = provider.GetRequiredService(); 84 | var transaction = provider.GetRequiredService(); 85 | 86 | var freakoutContext = new MsSqlFreakoutContext(connection, transaction); 87 | 88 | using (new FreakoutContextScope(freakoutContext)) 89 | { 90 | // there's an ambient context now! 🙂 91 | // 92 | // ASP.NET Core controllers can have IOutbox injected if they want! 93 | await next(); 94 | } 95 | }); 96 | 97 | ``` 98 | 99 | Having `IOutbox` injected is pretty neat, because it allows you to put commands in the outbox simply by going: 100 | 101 | ```csharp 102 | await outbox.AddOutboxCommandAsync(command); 103 | ``` 104 | 105 | without having to bother with thinking about which type of persistence is being used. 106 | 107 | 108 | ### Second way: Directly on the database transaction 109 | 110 | This way is more involved, because it's closer to the metal. 111 | 112 | Since this example is for SQL Server, and we're pretending to be working with `Microsoft.Data.SqlClient` and `SqlConnection`, it's natural to 113 | provide the outbox functionality as an extension method to `DbTransaction`. This way, your code can do stuff like this: 114 | 115 | ```csharp 116 | await using var connection = new SqlConnection(_connectionString); 117 | await connection.OpenAsync(); 118 | 119 | await using var transaction = await connection.BeginTransactionAsync(); 120 | 121 | // do your own work with connection+transaction here 122 | // (...) 123 | 124 | // possibly call this bad boy a couple of times 125 | await transaction.AddOutboxCommandAsync(serializer, "dbo", "OutboxCommands", new PublishJournalEntryAddedCommand(Id: journalEntryId)); 126 | 127 | // do more of your own work 128 | // (...) 129 | 130 | // commit it all atomically 131 | await transaction.CommitAsync(); 132 | ``` 133 | 134 | which in this case would result in publishing a couple of `JournalEntryAdded` events using Rebus. 135 | 136 | 137 | ## What does a command handler look like? 138 | 139 | Outbox commands are dispatched to handlers. Handlers are classes that implement `ICommandHandler` and are registered in the 140 | container using the `AddCommandHandler` extension method shown above. 141 | 142 | A command handler to publish the aforementioned Rebus event could look like this (assuming [Rebus](https://github.com/rebus-org/Rebus) has also been configured in the given container): 143 | 144 | ```csharp 145 | public class PublishJournalEntryAddedCommandHandler(IBus bus) : ICommandHandler 146 | { 147 | public async Task HandleAsync(PublishJournalEntryAddedCommand command, CancellationToken token) 148 | { 149 | await bus.Publish(new JournalEntryAdded(command.Id)); 150 | } 151 | } 152 | ``` 153 | 154 | 155 | -------------------------------------------------------------------------------- /Freakout.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio Version 17 4 | VisualStudioVersion = 17.9.34723.18 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "0 Solution Items", "0 Solution Items", "{EE9D4A73-88E4-4CB9-9F82-2750987AAF32}" 7 | ProjectSection(SolutionItems) = preProject 8 | CHANGELOG.md = CHANGELOG.md 9 | README.md = README.md 10 | EndProjectSection 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Freakout", "Freakout\Freakout.csproj", "{F05D67A1-BB63-45C1-A0D6-BAEE6A61F43C}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Freakout.Tests", "Freakout.Tests\Freakout.Tests.csproj", "{43EB5546-050B-4F97-9356-68500A979C49}" 15 | EndProject 16 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "core", "core", "{C6A7537C-30C3-4485-A507-5E7FF778B93D}" 17 | EndProject 18 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "tech", "tech", "{73164D95-FB6C-4389-B86D-38DB4F55A0B9}" 19 | EndProject 20 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Freakout.MsSql", "Freakout.MsSql\Freakout.MsSql.csproj", "{42137DB3-C2AE-4AC3-A8E6-87BCE3550F1A}" 21 | EndProject 22 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Freakout.MsSql.Tests", "Freakout.MsSql.Tests\Freakout.MsSql.Tests.csproj", "{D482AFB4-040E-4051-B53E-8E4CF87BC0ED}" 23 | EndProject 24 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Freakout.NpgSql", "Freakout.NpgSql\Freakout.NpgSql.csproj", "{9575F233-B377-425D-8CE2-25062F72B7C9}" 25 | EndProject 26 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Freakout.NpgSql.Tests", "Freakout.NpgSql.Tests\Freakout.NpgSql.Tests.csproj", "{28D3D355-BA16-42D6-896C-76652C40EF7B}" 27 | EndProject 28 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "mssql", "mssql", "{B7B5DCE0-89BE-4D1D-AE60-4B39503E6673}" 29 | EndProject 30 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "npgsql", "npgsql", "{D391FDEC-F778-4755-BA2E-C36A5E0DD79B}" 31 | EndProject 32 | Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "testing", "testing", "{AF67CBB0-753B-4305-9811-B6AEB63B07AF}" 33 | EndProject 34 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Freakout.Testing", "Freakout.Testing\Freakout.Testing.csproj", "{BE073F72-0AE8-4A1F-9F0D-4EBBB4200C1B}" 35 | EndProject 36 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Freakout.Testing.Tests", "Freakout.Testing.Tests\Freakout.Testing.Tests.csproj", "{6CC84A87-40A1-4DE4-9D03-D06D026F6834}" 37 | EndProject 38 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Freakout.Marten", "Freakout.Marten\Freakout.Marten.csproj", "{E2431672-4643-464C-83A1-AD651801DFAD}" 39 | EndProject 40 | Global 41 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 42 | Debug|Any CPU = Debug|Any CPU 43 | Release|Any CPU = Release|Any CPU 44 | EndGlobalSection 45 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 46 | {F05D67A1-BB63-45C1-A0D6-BAEE6A61F43C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 47 | {F05D67A1-BB63-45C1-A0D6-BAEE6A61F43C}.Debug|Any CPU.Build.0 = Debug|Any CPU 48 | {F05D67A1-BB63-45C1-A0D6-BAEE6A61F43C}.Release|Any CPU.ActiveCfg = Release|Any CPU 49 | {F05D67A1-BB63-45C1-A0D6-BAEE6A61F43C}.Release|Any CPU.Build.0 = Release|Any CPU 50 | {43EB5546-050B-4F97-9356-68500A979C49}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 51 | {43EB5546-050B-4F97-9356-68500A979C49}.Debug|Any CPU.Build.0 = Debug|Any CPU 52 | {43EB5546-050B-4F97-9356-68500A979C49}.Release|Any CPU.ActiveCfg = Release|Any CPU 53 | {43EB5546-050B-4F97-9356-68500A979C49}.Release|Any CPU.Build.0 = Release|Any CPU 54 | {42137DB3-C2AE-4AC3-A8E6-87BCE3550F1A}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 55 | {42137DB3-C2AE-4AC3-A8E6-87BCE3550F1A}.Debug|Any CPU.Build.0 = Debug|Any CPU 56 | {42137DB3-C2AE-4AC3-A8E6-87BCE3550F1A}.Release|Any CPU.ActiveCfg = Release|Any CPU 57 | {42137DB3-C2AE-4AC3-A8E6-87BCE3550F1A}.Release|Any CPU.Build.0 = Release|Any CPU 58 | {D482AFB4-040E-4051-B53E-8E4CF87BC0ED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 59 | {D482AFB4-040E-4051-B53E-8E4CF87BC0ED}.Debug|Any CPU.Build.0 = Debug|Any CPU 60 | {D482AFB4-040E-4051-B53E-8E4CF87BC0ED}.Release|Any CPU.ActiveCfg = Release|Any CPU 61 | {D482AFB4-040E-4051-B53E-8E4CF87BC0ED}.Release|Any CPU.Build.0 = Release|Any CPU 62 | {9575F233-B377-425D-8CE2-25062F72B7C9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 63 | {9575F233-B377-425D-8CE2-25062F72B7C9}.Debug|Any CPU.Build.0 = Debug|Any CPU 64 | {9575F233-B377-425D-8CE2-25062F72B7C9}.Release|Any CPU.ActiveCfg = Release|Any CPU 65 | {9575F233-B377-425D-8CE2-25062F72B7C9}.Release|Any CPU.Build.0 = Release|Any CPU 66 | {28D3D355-BA16-42D6-896C-76652C40EF7B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 67 | {28D3D355-BA16-42D6-896C-76652C40EF7B}.Debug|Any CPU.Build.0 = Debug|Any CPU 68 | {28D3D355-BA16-42D6-896C-76652C40EF7B}.Release|Any CPU.ActiveCfg = Release|Any CPU 69 | {28D3D355-BA16-42D6-896C-76652C40EF7B}.Release|Any CPU.Build.0 = Release|Any CPU 70 | {BE073F72-0AE8-4A1F-9F0D-4EBBB4200C1B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 71 | {BE073F72-0AE8-4A1F-9F0D-4EBBB4200C1B}.Debug|Any CPU.Build.0 = Debug|Any CPU 72 | {BE073F72-0AE8-4A1F-9F0D-4EBBB4200C1B}.Release|Any CPU.ActiveCfg = Release|Any CPU 73 | {BE073F72-0AE8-4A1F-9F0D-4EBBB4200C1B}.Release|Any CPU.Build.0 = Release|Any CPU 74 | {6CC84A87-40A1-4DE4-9D03-D06D026F6834}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 75 | {6CC84A87-40A1-4DE4-9D03-D06D026F6834}.Debug|Any CPU.Build.0 = Debug|Any CPU 76 | {6CC84A87-40A1-4DE4-9D03-D06D026F6834}.Release|Any CPU.ActiveCfg = Release|Any CPU 77 | {6CC84A87-40A1-4DE4-9D03-D06D026F6834}.Release|Any CPU.Build.0 = Release|Any CPU 78 | {E2431672-4643-464C-83A1-AD651801DFAD}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 79 | {E2431672-4643-464C-83A1-AD651801DFAD}.Debug|Any CPU.Build.0 = Debug|Any CPU 80 | {E2431672-4643-464C-83A1-AD651801DFAD}.Release|Any CPU.ActiveCfg = Release|Any CPU 81 | {E2431672-4643-464C-83A1-AD651801DFAD}.Release|Any CPU.Build.0 = Release|Any CPU 82 | EndGlobalSection 83 | GlobalSection(SolutionProperties) = preSolution 84 | HideSolutionNode = FALSE 85 | EndGlobalSection 86 | GlobalSection(NestedProjects) = preSolution 87 | {F05D67A1-BB63-45C1-A0D6-BAEE6A61F43C} = {C6A7537C-30C3-4485-A507-5E7FF778B93D} 88 | {43EB5546-050B-4F97-9356-68500A979C49} = {C6A7537C-30C3-4485-A507-5E7FF778B93D} 89 | {42137DB3-C2AE-4AC3-A8E6-87BCE3550F1A} = {B7B5DCE0-89BE-4D1D-AE60-4B39503E6673} 90 | {D482AFB4-040E-4051-B53E-8E4CF87BC0ED} = {B7B5DCE0-89BE-4D1D-AE60-4B39503E6673} 91 | {9575F233-B377-425D-8CE2-25062F72B7C9} = {D391FDEC-F778-4755-BA2E-C36A5E0DD79B} 92 | {28D3D355-BA16-42D6-896C-76652C40EF7B} = {D391FDEC-F778-4755-BA2E-C36A5E0DD79B} 93 | {B7B5DCE0-89BE-4D1D-AE60-4B39503E6673} = {73164D95-FB6C-4389-B86D-38DB4F55A0B9} 94 | {D391FDEC-F778-4755-BA2E-C36A5E0DD79B} = {73164D95-FB6C-4389-B86D-38DB4F55A0B9} 95 | {AF67CBB0-753B-4305-9811-B6AEB63B07AF} = {73164D95-FB6C-4389-B86D-38DB4F55A0B9} 96 | {BE073F72-0AE8-4A1F-9F0D-4EBBB4200C1B} = {AF67CBB0-753B-4305-9811-B6AEB63B07AF} 97 | {6CC84A87-40A1-4DE4-9D03-D06D026F6834} = {AF67CBB0-753B-4305-9811-B6AEB63B07AF} 98 | {E2431672-4643-464C-83A1-AD651801DFAD} = {D391FDEC-F778-4755-BA2E-C36A5E0DD79B} 99 | EndGlobalSection 100 | GlobalSection(ExtensibilityGlobals) = postSolution 101 | SolutionGuid = {764C61BA-C31B-4E87-BBAF-DCD4EF573CD9} 102 | EndGlobalSection 103 | EndGlobal 104 | -------------------------------------------------------------------------------- /Freakout.MsSql.Tests/SimpleSqlServerPoc.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Concurrent; 2 | using Freakout.Config; 3 | using Freakout.Tests; 4 | using Microsoft.Data.SqlClient; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using Microsoft.Extensions.Logging; 7 | using NUnit.Framework; 8 | using Testy; 9 | using Testy.Extensions; 10 | using Testy.General; 11 | using CancellationTokenSource = System.Threading.CancellationTokenSource; 12 | // ReSharper disable AccessToDisposedClosure 13 | // ReSharper disable ClassNeverInstantiated.Local 14 | #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed 15 | #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously 16 | 17 | namespace Freakout.MsSql.Tests; 18 | 19 | [TestFixture] 20 | public class SimpleSqlServerPoc : FixtureBase 21 | { 22 | string _connectionString; 23 | CancellationTokenSource _cancellationTokenSource; 24 | 25 | protected override void SetUp() 26 | { 27 | Using(new DisposableCallback(() => MsSqlTestHelper.DropTable("OutboxCommands"))); 28 | 29 | base.SetUp(); 30 | 31 | _connectionString = MsSqlTestHelper.ConnectionString; 32 | 33 | MsSqlTestHelper.DropTable("OutboxCommands"); 34 | 35 | _cancellationTokenSource = Using(new CancellationTokenSource()); 36 | 37 | Using(new DisposableCallback(_cancellationTokenSource.Cancel)); 38 | } 39 | 40 | [Test] 41 | public async Task CanAppendTextInBackground() 42 | { 43 | var texts = new ConcurrentQueue(); 44 | 45 | var services = new ServiceCollection(); 46 | 47 | // normal stuff 48 | services.AddLogging(l => l.AddConsole()); 49 | services.AddSingleton(texts); 50 | 51 | // freakout stuff 52 | var configuration = new MsSqlFreakoutConfiguration(_connectionString) { OutboxPollInterval = TimeSpan.FromSeconds(1) }; 53 | services.AddFreakout(configuration); 54 | services.AddCommandHandler(); 55 | 56 | await using var provider = services.BuildServiceProvider(); 57 | 58 | provider.RunBackgroundWorkersAsync(_cancellationTokenSource.Token); 59 | 60 | // pretend something happens somewhere else 61 | Task.Run(async () => await AddOutboxCommandAsync(configuration, new AppendTextOutboxCommand(Text: "Howdy!"))); 62 | 63 | await texts.WaitOrDie( 64 | completionExpression: t => t.Count == 1, 65 | failExpression: t => t.Count > 1, 66 | failureDetailsFunction: () => 67 | $"Text was not appended as expected - expected one single 'Howdy!' to have been appended, but we got this: {string.Join(", ", texts)}" 68 | ); 69 | 70 | Assert.That(texts, Is.EqualTo(new[] { "Howdy!" })); 71 | } 72 | 73 | [Test] 74 | public async Task CanAppendTextInBackground_AlternativeHandler() 75 | { 76 | var texts = new ConcurrentQueue(); 77 | 78 | var services = new ServiceCollection(); 79 | 80 | // normal stuff 81 | services.AddLogging(l => l.AddConsole()); 82 | services.AddSingleton(texts); 83 | 84 | // freakout stuff 85 | var configuration = new MsSqlFreakoutConfiguration(_connectionString) { OutboxPollInterval = TimeSpan.FromSeconds(1) }; 86 | services.AddFreakout(configuration); 87 | services.AddCommandHandler((handler, cmd, token) => handler.HandleAsync(cmd, token)); 88 | 89 | await using var provider = services.BuildServiceProvider(); 90 | 91 | provider.RunBackgroundWorkersAsync(_cancellationTokenSource.Token); 92 | 93 | // pretend something happens somewhere else 94 | Task.Run(async () => await AddOutboxCommandAsync(configuration, new AppendTextOutboxCommand(Text: "Howdy!"))); 95 | 96 | await texts.WaitOrDie( 97 | completionExpression: t => t.Count == 1, 98 | failExpression: t => t.Count > 1, 99 | failureDetailsFunction: () => 100 | $"Text was not appended as expected - expected one single 'Howdy!' to have been appended, but we got this: {string.Join(", ", texts)}" 101 | ); 102 | 103 | Assert.That(texts, Is.EqualTo(new[] { "Howdy!" })); 104 | } 105 | 106 | [Test] 107 | public async Task CanAppendTextInBackground_UsingScopedOutboxAppender() 108 | { 109 | var texts = new ConcurrentQueue(); 110 | 111 | var services = new ServiceCollection(); 112 | 113 | // normal stuff 114 | services.AddLogging(l => l.AddConsole()); 115 | services.AddSingleton(texts); 116 | 117 | // freakout stuff 118 | services.AddFreakout(new MsSqlFreakoutConfiguration(_connectionString) { OutboxPollInterval = TimeSpan.FromSeconds(1) }); 119 | services.AddCommandHandler(); 120 | 121 | await using var provider = services.BuildServiceProvider(); 122 | 123 | provider.RunBackgroundWorkersAsync(_cancellationTokenSource.Token); 124 | 125 | // pretend something happens somewhere else 126 | Task.Run(async () => 127 | { 128 | try 129 | { 130 | await AddOutboxCommandUsingScopedOutboxAppenderAsync(provider, 131 | new AppendTextOutboxCommand(Text: "Howdy!")); 132 | } 133 | catch (Exception exception) 134 | { 135 | Console.WriteLine(exception); 136 | } 137 | }); 138 | 139 | await texts.WaitOrDie( 140 | timeoutSeconds: 3, 141 | completionExpression: t => t.Count == 1, 142 | failExpression: t => t.Count > 1, 143 | failureDetailsFunction: () => 144 | $"Text was not appended as expected - expected one single 'Howdy!' to have been appended, but we got this: {string.Join(", ", texts)}" 145 | ); 146 | 147 | Assert.That(texts, Is.EqualTo(new[] { "Howdy!" })); 148 | } 149 | 150 | static async Task AddOutboxCommandUsingScopedOutboxAppenderAsync(ServiceProvider provider, object command) 151 | { 152 | // create connection/transaction 153 | await using var connection = new SqlConnection(MsSqlTestHelper.ConnectionString); 154 | await connection.OpenAsync(); 155 | await using var transaction = connection.BeginTransaction(); 156 | 157 | // pass them via ambient context 158 | var msSqlFreakoutContext = new MsSqlFreakoutContext(connection, transaction); 159 | 160 | using (new FreakoutContextScope(msSqlFreakoutContext)) 161 | { 162 | using var scope = provider.GetRequiredService().CreateScope(); 163 | 164 | var outbox = scope.ServiceProvider.GetRequiredService(); 165 | 166 | await outbox.AddOutboxCommandAsync(command); 167 | } 168 | 169 | await transaction.CommitAsync(); 170 | } 171 | 172 | async Task AddOutboxCommandAsync(MsSqlFreakoutConfiguration configuration, object command) 173 | { 174 | await using var connection = new SqlConnection(_connectionString); 175 | await connection.OpenAsync(); 176 | 177 | await using var transaction = await connection.BeginTransactionAsync(); 178 | await transaction.AddOutboxCommandAsync(configuration.CommandSerializer, configuration.SchemaName, configuration.TableName, command); 179 | await transaction.CommitAsync(); 180 | } 181 | 182 | /// 183 | /// This is a command 184 | /// 185 | record AppendTextOutboxCommand(string Text); 186 | 187 | /// 188 | /// This is a command handler 189 | /// 190 | class AppendTextOutboxCommandHandler(ConcurrentQueue texts) : ICommandHandler 191 | { 192 | public async Task HandleAsync(AppendTextOutboxCommand command, CancellationToken cancellationToken) => texts.Enqueue(command.Text); 193 | } 194 | 195 | /// 196 | /// This is an interfaceless command handler 197 | /// 198 | class AlternativeAppendTextOutboxCommandHandler(ConcurrentQueue texts) 199 | { 200 | public async Task HandleAsync(AppendTextOutboxCommand command, CancellationToken cancellationToken) => texts.Enqueue(command.Text); 201 | } 202 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | ## 4 | ## Get latest from https://github.com/github/gitignore/blob/main/VisualStudio.gitignore 5 | 6 | # User-specific files 7 | *.rsuser 8 | *.suo 9 | *.user 10 | *.userosscache 11 | *.sln.docstates 12 | 13 | # User-specific files (MonoDevelop/Xamarin Studio) 14 | *.userprefs 15 | 16 | # Mono auto generated files 17 | mono_crash.* 18 | 19 | # Build results 20 | [Dd]ebug/ 21 | [Dd]ebugPublic/ 22 | [Rr]elease/ 23 | [Rr]eleases/ 24 | x64/ 25 | x86/ 26 | [Ww][Ii][Nn]32/ 27 | [Aa][Rr][Mm]/ 28 | [Aa][Rr][Mm]64/ 29 | bld/ 30 | [Bb]in/ 31 | [Oo]bj/ 32 | [Ll]og/ 33 | [Ll]ogs/ 34 | 35 | # Visual Studio 2015/2017 cache/options directory 36 | .vs/ 37 | # Uncomment if you have tasks that create the project's static files in wwwroot 38 | #wwwroot/ 39 | 40 | # Visual Studio 2017 auto generated files 41 | Generated\ Files/ 42 | 43 | # MSTest test Results 44 | [Tt]est[Rr]esult*/ 45 | [Bb]uild[Ll]og.* 46 | 47 | # NUnit 48 | *.VisualState.xml 49 | TestResult.xml 50 | nunit-*.xml 51 | 52 | # Build Results of an ATL Project 53 | [Dd]ebugPS/ 54 | [Rr]eleasePS/ 55 | dlldata.c 56 | 57 | # Benchmark Results 58 | BenchmarkDotNet.Artifacts/ 59 | 60 | # .NET Core 61 | project.lock.json 62 | project.fragment.lock.json 63 | artifacts/ 64 | 65 | # ASP.NET Scaffolding 66 | ScaffoldingReadMe.txt 67 | 68 | # StyleCop 69 | StyleCopReport.xml 70 | 71 | # Files built by Visual Studio 72 | *_i.c 73 | *_p.c 74 | *_h.h 75 | *.ilk 76 | *.meta 77 | *.obj 78 | *.iobj 79 | *.pch 80 | *.pdb 81 | *.ipdb 82 | *.pgc 83 | *.pgd 84 | *.rsp 85 | *.sbr 86 | *.tlb 87 | *.tli 88 | *.tlh 89 | *.tmp 90 | *.tmp_proj 91 | *_wpftmp.csproj 92 | *.log 93 | *.tlog 94 | *.vspscc 95 | *.vssscc 96 | .builds 97 | *.pidb 98 | *.svclog 99 | *.scc 100 | 101 | # Chutzpah Test files 102 | _Chutzpah* 103 | 104 | # Visual C++ cache files 105 | ipch/ 106 | *.aps 107 | *.ncb 108 | *.opendb 109 | *.opensdf 110 | *.sdf 111 | *.cachefile 112 | *.VC.db 113 | *.VC.VC.opendb 114 | 115 | # Visual Studio profiler 116 | *.psess 117 | *.vsp 118 | *.vspx 119 | *.sap 120 | 121 | # Visual Studio Trace Files 122 | *.e2e 123 | 124 | # TFS 2012 Local Workspace 125 | $tf/ 126 | 127 | # Guidance Automation Toolkit 128 | *.gpState 129 | 130 | # ReSharper is a .NET coding add-in 131 | _ReSharper*/ 132 | *.[Rr]e[Ss]harper 133 | *.DotSettings.user 134 | 135 | # TeamCity is a build add-in 136 | _TeamCity* 137 | 138 | # DotCover is a Code Coverage Tool 139 | *.dotCover 140 | 141 | # AxoCover is a Code Coverage Tool 142 | .axoCover/* 143 | !.axoCover/settings.json 144 | 145 | # Coverlet is a free, cross platform Code Coverage Tool 146 | coverage*.json 147 | coverage*.xml 148 | coverage*.info 149 | 150 | # Visual Studio code coverage results 151 | *.coverage 152 | *.coveragexml 153 | 154 | # NCrunch 155 | _NCrunch_* 156 | .*crunch*.local.xml 157 | nCrunchTemp_* 158 | 159 | # MightyMoose 160 | *.mm.* 161 | AutoTest.Net/ 162 | 163 | # Web workbench (sass) 164 | .sass-cache/ 165 | 166 | # Installshield output folder 167 | [Ee]xpress/ 168 | 169 | # DocProject is a documentation generator add-in 170 | DocProject/buildhelp/ 171 | DocProject/Help/*.HxT 172 | DocProject/Help/*.HxC 173 | DocProject/Help/*.hhc 174 | DocProject/Help/*.hhk 175 | DocProject/Help/*.hhp 176 | DocProject/Help/Html2 177 | DocProject/Help/html 178 | 179 | # Click-Once directory 180 | publish/ 181 | 182 | # Publish Web Output 183 | *.[Pp]ublish.xml 184 | *.azurePubxml 185 | # Note: Comment the next line if you want to checkin your web deploy settings, 186 | # but database connection strings (with potential passwords) will be unencrypted 187 | *.pubxml 188 | *.publishproj 189 | 190 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 191 | # checkin your Azure Web App publish settings, but sensitive information contained 192 | # in these scripts will be unencrypted 193 | PublishScripts/ 194 | 195 | # NuGet Packages 196 | *.nupkg 197 | # NuGet Symbol Packages 198 | *.snupkg 199 | # The packages folder can be ignored because of Package Restore 200 | **/[Pp]ackages/* 201 | # except build/, which is used as an MSBuild target. 202 | !**/[Pp]ackages/build/ 203 | # Uncomment if necessary however generally it will be regenerated when needed 204 | #!**/[Pp]ackages/repositories.config 205 | # NuGet v3's project.json files produces more ignorable files 206 | *.nuget.props 207 | *.nuget.targets 208 | 209 | # Microsoft Azure Build Output 210 | csx/ 211 | *.build.csdef 212 | 213 | # Microsoft Azure Emulator 214 | ecf/ 215 | rcf/ 216 | 217 | # Windows Store app package directories and files 218 | AppPackages/ 219 | BundleArtifacts/ 220 | Package.StoreAssociation.xml 221 | _pkginfo.txt 222 | *.appx 223 | *.appxbundle 224 | *.appxupload 225 | 226 | # Visual Studio cache files 227 | # files ending in .cache can be ignored 228 | *.[Cc]ache 229 | # but keep track of directories ending in .cache 230 | !?*.[Cc]ache/ 231 | 232 | # Others 233 | ClientBin/ 234 | ~$* 235 | *~ 236 | *.dbmdl 237 | *.dbproj.schemaview 238 | *.jfm 239 | *.pfx 240 | *.publishsettings 241 | orleans.codegen.cs 242 | 243 | # Including strong name files can present a security risk 244 | # (https://github.com/github/gitignore/pull/2483#issue-259490424) 245 | #*.snk 246 | 247 | # Since there are multiple workflows, uncomment next line to ignore bower_components 248 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 249 | #bower_components/ 250 | 251 | # RIA/Silverlight projects 252 | Generated_Code/ 253 | 254 | # Backup & report files from converting an old project file 255 | # to a newer Visual Studio version. Backup files are not needed, 256 | # because we have git ;-) 257 | _UpgradeReport_Files/ 258 | Backup*/ 259 | UpgradeLog*.XML 260 | UpgradeLog*.htm 261 | ServiceFabricBackup/ 262 | *.rptproj.bak 263 | 264 | # SQL Server files 265 | *.mdf 266 | *.ldf 267 | *.ndf 268 | 269 | # Business Intelligence projects 270 | *.rdl.data 271 | *.bim.layout 272 | *.bim_*.settings 273 | *.rptproj.rsuser 274 | *- [Bb]ackup.rdl 275 | *- [Bb]ackup ([0-9]).rdl 276 | *- [Bb]ackup ([0-9][0-9]).rdl 277 | 278 | # Microsoft Fakes 279 | FakesAssemblies/ 280 | 281 | # GhostDoc plugin setting file 282 | *.GhostDoc.xml 283 | 284 | # Node.js Tools for Visual Studio 285 | .ntvs_analysis.dat 286 | node_modules/ 287 | 288 | # Visual Studio 6 build log 289 | *.plg 290 | 291 | # Visual Studio 6 workspace options file 292 | *.opt 293 | 294 | # Visual Studio 6 auto-generated workspace file (contains which files were open etc.) 295 | *.vbw 296 | 297 | # Visual Studio 6 auto-generated project file (contains which files were open etc.) 298 | *.vbp 299 | 300 | # Visual Studio 6 workspace and project file (working project files containing files to include in project) 301 | *.dsw 302 | *.dsp 303 | 304 | # Visual Studio 6 technical files 305 | *.ncb 306 | *.aps 307 | 308 | # Visual Studio LightSwitch build output 309 | **/*.HTMLClient/GeneratedArtifacts 310 | **/*.DesktopClient/GeneratedArtifacts 311 | **/*.DesktopClient/ModelManifest.xml 312 | **/*.Server/GeneratedArtifacts 313 | **/*.Server/ModelManifest.xml 314 | _Pvt_Extensions 315 | 316 | # Paket dependency manager 317 | .paket/paket.exe 318 | paket-files/ 319 | 320 | # FAKE - F# Make 321 | .fake/ 322 | 323 | # CodeRush personal settings 324 | .cr/personal 325 | 326 | # Python Tools for Visual Studio (PTVS) 327 | __pycache__/ 328 | *.pyc 329 | 330 | # Cake - Uncomment if you are using it 331 | # tools/** 332 | # !tools/packages.config 333 | 334 | # Tabs Studio 335 | *.tss 336 | 337 | # Telerik's JustMock configuration file 338 | *.jmconfig 339 | 340 | # BizTalk build output 341 | *.btp.cs 342 | *.btm.cs 343 | *.odx.cs 344 | *.xsd.cs 345 | 346 | # OpenCover UI analysis results 347 | OpenCover/ 348 | 349 | # Azure Stream Analytics local run output 350 | ASALocalRun/ 351 | 352 | # MSBuild Binary and Structured Log 353 | *.binlog 354 | 355 | # NVidia Nsight GPU debugger configuration file 356 | *.nvuser 357 | 358 | # MFractors (Xamarin productivity tool) working folder 359 | .mfractor/ 360 | 361 | # Local History for Visual Studio 362 | .localhistory/ 363 | 364 | # Visual Studio History (VSHistory) files 365 | .vshistory/ 366 | 367 | # BeatPulse healthcheck temp database 368 | healthchecksdb 369 | 370 | # Backup folder for Package Reference Convert tool in Visual Studio 2017 371 | MigrationBackup/ 372 | 373 | # Ionide (cross platform F# VS Code tools) working folder 374 | .ionide/ 375 | 376 | # Fody - auto-generated XML schema 377 | FodyWeavers.xsd 378 | 379 | # VS Code files for those working on multiple tools 380 | .vscode/* 381 | !.vscode/settings.json 382 | !.vscode/tasks.json 383 | !.vscode/launch.json 384 | !.vscode/extensions.json 385 | *.code-workspace 386 | 387 | # Local History for Visual Studio Code 388 | .history/ 389 | 390 | # Windows Installer files from build outputs 391 | *.cab 392 | *.msi 393 | *.msix 394 | *.msm 395 | *.msp 396 | 397 | # JetBrains Rider 398 | *.sln.iml 399 | -------------------------------------------------------------------------------- /Freakout.Tests/Dispatch/TestCommandDispatcher.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Threading; 4 | using System.Threading.Tasks; 5 | using Freakout.Config; 6 | using Freakout.Internals.Dispatchers; 7 | using Freakout.Serialization; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using NUnit.Framework; 10 | using Testy; 11 | // ReSharper disable ClassNeverInstantiated.Local 12 | #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously 13 | 14 | namespace Freakout.Tests.Dispatch; 15 | 16 | [TestFixture] 17 | public class TestCommandDispatcher : FixtureBase 18 | { 19 | SystemTextJsonCommandSerializer _serializer; 20 | 21 | protected override void SetUp() 22 | { 23 | base.SetUp(); 24 | 25 | _serializer = new SystemTextJsonCommandSerializer(); 26 | } 27 | 28 | [Test] 29 | public async Task CanDispatchStuff_GetNiceErrorWhenCommandHandlerIsNotRegistered() 30 | { 31 | var services = new ServiceCollection(); 32 | 33 | await using var provider = services.BuildServiceProvider(); 34 | 35 | var dispatcher = new CompiledExpressionCommandDispatcher(_serializer, provider.GetRequiredService()); 36 | 37 | var ex = Assert.ThrowsAsync(() => dispatcher.ExecuteAsync(GetOutboxCommand(new SomeCommand()))); 38 | 39 | Console.WriteLine(ex); 40 | } 41 | 42 | [Test] 43 | public async Task CanDispatchStuff() 44 | { 45 | var events = new ConcurrentQueue(); 46 | var services = new ServiceCollection(); 47 | 48 | services.AddSingleton(events); 49 | services.AddCommandHandler(); 50 | services.AddCommandHandler(); 51 | 52 | await using var provider = services.BuildServiceProvider(); 53 | 54 | var dispatcher = new CompiledExpressionCommandDispatcher(_serializer, provider.GetRequiredService()); 55 | 56 | await dispatcher.ExecuteAsync(GetOutboxCommand(new AnotherCommand("hej"))); 57 | await dispatcher.ExecuteAsync(GetOutboxCommand(new AnotherCommand("hej med dig"))); 58 | await dispatcher.ExecuteAsync(GetOutboxCommand(new ThirdCommand("hej"))); 59 | await dispatcher.ExecuteAsync(GetOutboxCommand(new ThirdCommand("hej med dig"))); 60 | 61 | Assert.That(events, Is.EqualTo(new[] 62 | { 63 | "AnotherCommandHandler called - text: hej", 64 | "AnotherCommandHandler called - text: hej med dig", 65 | "ThirdCommandHandler called - text: hej", 66 | "ThirdCommandHandler called - text: hej med dig", 67 | })); 68 | } 69 | 70 | /* 71 | 72 | Initial runs: 73 | 74 | SCOPE 'Dispatch 1000 commands' completed in 31,196 ms | 0,031196 ms/item | 32,05539171688678 items/ms 75 | SCOPE 'Dispatch 1000000 commands' completed in 4438,4956 ms | 0,0044384956 ms/item | 225,30156389025146 items/ms 76 | SCOPE 'Dispatch 10000000 commands' completed in 37986,2051 ms | 0,00379862051 ms/item | 263,2534619784907 items/ms 77 | 78 | After storing closed generic methods in concurrent dictionary: 79 | 80 | SCOPE 'Dispatch 1000 commands' completed in 37,0173 ms | 0,037017299999999996 ms/item | 27,014395971613272 items/ms 81 | SCOPE 'Dispatch 1000000 commands' completed in 3731,3556 ms | 0,0037313555999999998 ms/item | 267,99911538851995 items/ms 82 | SCOPE 'Dispatch 10000000 commands' completed in 31529,0346 ms | 0,0031529034599999998 ms/item | 317,1679731671835 items/ms 83 | 84 | After using ChatGPT to convert into expression tree: 85 | 86 | SCOPE 'Dispatch 1000 commands' completed in 29,3767 ms | 0,0293767 ms/item | 34,040583183271096 items/ms 87 | SCOPE 'Dispatch 1000000 commands' completed in 3405,0657 ms | 0,0034050657 ms/item | 293,68008963821165 items/ms 88 | SCOPE 'Dispatch 10000000 commands' completed in 30029,7693 ms | 0,00300297693 ms/item | 333,0028912343326 items/ms 89 | 90 | After waiting for the 29th of May 2024 to come by: 91 | 92 | SCOPE 'Dispatch 1000 commands' completed in 31,8928 ms | 0,0318928 ms/item | 31,355039381929462 items/ms 93 | SCOPE 'Dispatch 1000000 commands' completed in 3675,3738 ms | 0,0036753738 ms/item | 272,08116899565425 items/ms 94 | SCOPE 'Dispatch 10000000 commands' completed in 29720,7503 ms | 0,00297207503 ms/item | 336,46526077102436 items/ms 95 | 96 | After switching to IL-generated dispatcher: 97 | 98 | SCOPE 'Dispatch 1000 commands' completed in 34,6107 ms | 0,0346107 ms/item | 28,89279904769334 items/ms 99 | SCOPE 'Dispatch 1000000 commands' completed in 3900,4479 ms | 0,0039004479 ms/item | 256,3808120600714 items/ms 100 | SCOPE 'Dispatch 10000000 commands' completed in 35641,9579 ms | 0,00356419579 ms/item | 280,5682007721579 items/ms 101 | 102 | NOT IMPRESSED!! Switching back to the expression tree-based invoker. 103 | 104 | 105 | New run with expression-based invoker (this time in Release mode): 106 | 107 | SCOPE 'Dispatch 1000 commands' completed in 30,0883 ms | 0,030088300000000002 ms/item | 33,23551014846302 items/ms 108 | SCOPE 'Dispatch 1000000 commands' completed in 2730,8531 ms | 0,0027308530999999997 ms/item | 366,18593654854595 items/ms 109 | SCOPE 'Dispatch 10000000 commands' completed in 23148,3344 ms | 0,00231483344 ms/item | 431,9965241214072 items/ms 110 | 111 | New run with Danielovich's modded IL invoker (also in Release mode): 112 | 113 | SCOPE 'Dispatch 1000 commands' completed in 38,6496 ms | 0,0386496 ms/item | 25,873488988243086 items/ms 114 | SCOPE 'Dispatch 1000000 commands' completed in 2650,4597 ms | 0,0026504596999999998 ms/item | 377,29304090154625 items/ms 115 | SCOPE 'Dispatch 10000000 commands' completed in 22555,6927 ms | 0,00225556927 ms/item | 443,34705801343 items/ms 116 | 117 | 118 | 119 | * 120 | */ 121 | [TestCase(1000, CommandDispatcherImplementation.CompiledExpression)] 122 | [TestCase(1000000, CommandDispatcherImplementation.CompiledExpression, Explicit = true)] 123 | [TestCase(10000000, CommandDispatcherImplementation.CompiledExpression, Explicit = true)] 124 | [TestCase(1000, CommandDispatcherImplementation.IlEmit)] 125 | [TestCase(1000000, CommandDispatcherImplementation.IlEmit, Explicit = true)] 126 | [TestCase(10000000, CommandDispatcherImplementation.IlEmit, Explicit = true)] 127 | [Repeat(5)] 128 | public async Task TakeTime(int count, CommandDispatcherImplementation impl) 129 | { 130 | var services = new ServiceCollection(); 131 | 132 | services.AddCommandHandler(); 133 | 134 | await using var provider = services.BuildServiceProvider(); 135 | 136 | var serviceScopeFactory = provider.GetRequiredService(); 137 | 138 | var dispatcher = impl switch 139 | { 140 | CommandDispatcherImplementation.CompiledExpression => (ICommandDispatcher)new CompiledExpressionCommandDispatcher( 141 | commandSerializer: _serializer, 142 | serviceScopeFactory: serviceScopeFactory 143 | ), 144 | 145 | CommandDispatcherImplementation.IlEmit => new IlEmitCommandDispatcher( 146 | commandSerializer: _serializer, 147 | serviceScopeFactory: serviceScopeFactory 148 | ), 149 | 150 | _ => throw new ArgumentOutOfRangeException(nameof(impl), impl, "Unknown command dispatcher implementation") 151 | }; 152 | 153 | using var _ = TimerScope($"Dispatch {count} commands", count); 154 | 155 | for (var counter = 0; counter < count; counter++) 156 | { 157 | var command = new SomeCommand(); 158 | var outboxCommand = GetOutboxCommand(command); 159 | 160 | await dispatcher.ExecuteAsync(outboxCommand, CancellationToken.None); 161 | } 162 | } 163 | 164 | public enum CommandDispatcherImplementation 165 | { 166 | CompiledExpression, 167 | IlEmit 168 | } 169 | 170 | OutboxCommand GetOutboxCommand(object command) => _serializer.Serialize(command); 171 | 172 | record SomeCommand; 173 | 174 | class SomeCommandHandler : ICommandHandler 175 | { 176 | public Task HandleAsync(SomeCommand command, CancellationToken cancellationToken) => Task.CompletedTask; 177 | } 178 | 179 | record AnotherCommand(string Text); 180 | 181 | class AnotherCommandHandler(ConcurrentQueue events) : ICommandHandler 182 | { 183 | public async Task HandleAsync(AnotherCommand command, CancellationToken cancellationToken) => events.Enqueue($"{GetType().Name} called - text: {command.Text}"); 184 | } 185 | 186 | record ThirdCommand(string Text); 187 | 188 | class ThirdCommandHandler(ConcurrentQueue events) : ICommandHandler 189 | { 190 | public async Task HandleAsync(ThirdCommand command, CancellationToken cancellationToken) => events.Enqueue($"{GetType().Name} called - text: {command.Text}"); 191 | } 192 | } -------------------------------------------------------------------------------- /Freakout.Tests/Contracts/NormalTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Concurrent; 3 | using System.Linq; 4 | using System.Threading; 5 | using System.Threading.Tasks; 6 | using Freakout.Config; 7 | using Freakout.Internals; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Nito.AsyncEx; 10 | using NUnit.Framework; 11 | using Testy; 12 | using Testy.Extensions; 13 | using Testy.General; 14 | // ReSharper disable ClassNeverInstantiated.Local 15 | // ReSharper disable ConvertToUsingDeclaration 16 | // ReSharper disable MethodSupportsCancellation 17 | #pragma warning disable CS1998 // Async method lacks 'await' operators and will run synchronously 18 | 19 | namespace Freakout.Tests.Contracts; 20 | 21 | public abstract class NormalTests : FixtureBase where TFreakoutSystemFactory : IFreakoutSystemFactory, new() 22 | { 23 | TFreakoutSystemFactory _factory; 24 | 25 | protected override void SetUp() 26 | { 27 | base.SetUp(); 28 | 29 | _factory = Using(new TFreakoutSystemFactory()); 30 | } 31 | 32 | [Test] 33 | public void CanStartUpAndShutDown() => _ = _factory.Create(); 34 | 35 | [Test] 36 | public async Task CanSendAndReceiveSingleCommand() 37 | { 38 | var system = _factory.Create(); 39 | var commandStore = system.OutboxCommandStore; 40 | var outbox = system.Outbox; 41 | 42 | using (var scope = system.CreateScope()) 43 | { 44 | await outbox.AddOutboxCommandAsync(new SomeKindOfCommand()); 45 | scope.Complete(); 46 | } 47 | 48 | using var batch = await commandStore.GetPendingOutboxCommandsAsync(commandProcessingBatchSize: 1); 49 | 50 | Assert.That(batch.Count(), Is.EqualTo(1), 51 | "The command store must return a batch containing exactly 1 command at this point, because one single command has been added"); 52 | 53 | var command = batch.First(); 54 | 55 | Assert.That(command.Headers, Contains.Key(HeaderKeys.CommandType).WithValue(typeof(SomeKindOfCommand).GetSimpleAssemblyQualifiedName()), 56 | "The headers must contain type information"); 57 | } 58 | 59 | [Test] 60 | public async Task CanSendAndReceiveMultipleCommands() 61 | { 62 | var system = _factory.Create(); 63 | var commandStore = system.OutboxCommandStore; 64 | var outbox = system.Outbox; 65 | 66 | using (var scope = system.CreateScope()) 67 | { 68 | await outbox.AddOutboxCommandAsync(new SomeKindOfCommand()); 69 | await outbox.AddOutboxCommandAsync(new SomeKindOfCommand()); 70 | scope.Complete(); 71 | } 72 | 73 | using var batch1 = await commandStore.GetPendingOutboxCommandsAsync(commandProcessingBatchSize: 1); 74 | await batch1.CompleteAsync(); 75 | 76 | using var batch2 = await commandStore.GetPendingOutboxCommandsAsync(commandProcessingBatchSize: 1); 77 | await batch2.CompleteAsync(); 78 | 79 | using var batch3 = await commandStore.GetPendingOutboxCommandsAsync(commandProcessingBatchSize: 1); 80 | await batch3.CompleteAsync(); 81 | 82 | Assert.That(batch1.Count(), Is.EqualTo(1), "Expected batch to contain 1 command (because processing batch size = 1 and we added 2 commands)"); 83 | Assert.That(batch2.Count(), Is.EqualTo(1), "Expected batch to contain 1 command (because processing batch size = 1 and we added 2 commands and have removed 1)"); 84 | Assert.That(batch3.Count(), Is.EqualTo(0), "Expected batch to contain 0 commands (because processing batch size = 1 and we added 2 commands and have removed 2)"); 85 | } 86 | 87 | [Test] 88 | public async Task CanSendAndReceiveMultipleCommands_ChangeOrderToDetectLockingIssues() 89 | { 90 | var system = _factory.Create(); 91 | var commandStore = system.OutboxCommandStore; 92 | var outbox = system.Outbox; 93 | 94 | using (var scope = system.CreateScope()) 95 | { 96 | await outbox.AddOutboxCommandAsync(new SomeKindOfCommand()); 97 | await outbox.AddOutboxCommandAsync(new SomeKindOfCommand()); 98 | scope.Complete(); 99 | } 100 | 101 | using var batch1 = await commandStore.GetPendingOutboxCommandsAsync(commandProcessingBatchSize: 1); 102 | using var batch2 = await commandStore.GetPendingOutboxCommandsAsync(commandProcessingBatchSize: 1); 103 | using var batch3 = await commandStore.GetPendingOutboxCommandsAsync(commandProcessingBatchSize: 1); 104 | 105 | await batch3.CompleteAsync(); 106 | await batch2.CompleteAsync(); 107 | await batch1.CompleteAsync(); 108 | 109 | Assert.That(batch1.Count(), Is.EqualTo(1), "Expected batch to contain 1 command (because processing batch size = 1 and we added 2 commands)"); 110 | Assert.That(batch2.Count(), Is.EqualTo(1), "Expected batch to contain 1 command (because processing batch size = 1 and we added 2 commands and have removed 1)"); 111 | Assert.That(batch3.Count(), Is.EqualTo(0), "Expected batch to contain 0 commands (because processing batch size = 1 and we added 2 commands and have removed 2)"); 112 | } 113 | 114 | record SomeKindOfCommand; 115 | 116 | [Test] 117 | public async Task HandlersAreExecutedInScope() 118 | { 119 | var events = new ConcurrentQueue(); 120 | 121 | var system = _factory.Create(before: services => 122 | { 123 | services.AddCommandHandler(); 124 | services.AddSingleton(events); 125 | }); 126 | 127 | var cts = Using(new CancellationTokenSource()); 128 | Using(new DisposableCallback(cts.Cancel)); 129 | 130 | _ = system.StartCommandProcessorAsync(cts.Token); 131 | 132 | var expectedNameOfContext = ""; 133 | 134 | _ = Task.Run(async () => 135 | { 136 | using var scope = system.CreateScope(); 137 | 138 | expectedNameOfContext = new AsyncLocalFreakoutContextAccessor().GetContext()?.GetType().Name; 139 | 140 | var outbox = system.Outbox; 141 | 142 | await outbox.AddOutboxCommandAsync(new SomeKindOfCommand(), cancellationToken: CancellationToken.None); 143 | 144 | scope.Complete(); 145 | }, CancellationToken.None); 146 | 147 | await events.WaitOrDie( 148 | completionExpression: q => q.Count == 1, 149 | failExpression: q => q.Count > 1, 150 | timeoutSeconds: 5, 151 | failureDetailsFunction: () => "Expected that the command handler would have appended a text like 'Got this context: '" 152 | ); 153 | 154 | var text = events.First(); 155 | 156 | Assert.That(text, Is.EqualTo($"Got this context: {expectedNameOfContext}"), 157 | $"This was the event text generated by the command handler. If the type name was different from '{expectedNameOfContext}', it's a sign that the context accessor did not find the expected Freakout context"); 158 | } 159 | 160 | class SomeKindOfCommandHandler(IFreakoutContextAccessor contextAccessor, ConcurrentQueue events) : ICommandHandler 161 | { 162 | public async Task HandleAsync(SomeKindOfCommand command, CancellationToken cancellationToken) 163 | { 164 | var context = contextAccessor.GetContext(throwIfNull: false); 165 | 166 | events.Enqueue($"Got this context: {context?.GetType().Name}"); 167 | } 168 | } 169 | 170 | [Test] 171 | public void CommandHandlerCanUseOutboxToo() 172 | { 173 | var done = new AsyncManualResetEvent(); 174 | var events = new ConcurrentQueue(); 175 | 176 | var system = _factory.Create(before: services => 177 | { 178 | services.AddSingleton(done); 179 | services.AddSingleton(events); 180 | services.AddCommandHandler(); 181 | }); 182 | 183 | var cts = Using(new CancellationTokenSource()); 184 | Using(new DisposableCallback(cts.Cancel)); 185 | 186 | _ = system.StartCommandProcessorAsync(cts.Token); 187 | 188 | using (var scope = system.CreateScope()) 189 | { 190 | var outbox = system.Outbox; 191 | outbox.AddOutboxCommand(new InitiatingCommand()); 192 | scope.Complete(); 193 | } 194 | 195 | done.Wait(CancelAfter(TimeSpan.FromSeconds(5))); 196 | 197 | Assert.That(events, Is.EqualTo(new[] 198 | { 199 | "Got InitiatingCommand", 200 | "Sent FinishingCommand", 201 | "Got FinishingCommand", 202 | "Signaling done!" 203 | })); 204 | } 205 | 206 | record InitiatingCommand; 207 | 208 | class CommandHandler(IOutbox outbox, ConcurrentQueue events, AsyncManualResetEvent done) : ICommandHandler, ICommandHandler 209 | { 210 | public async Task HandleAsync(InitiatingCommand command, CancellationToken cancellationToken) 211 | { 212 | events.Enqueue("Got InitiatingCommand"); 213 | await outbox.AddOutboxCommandAsync(new FinishingCommand(), cancellationToken: cancellationToken); 214 | events.Enqueue("Sent FinishingCommand"); 215 | } 216 | 217 | public async Task HandleAsync(FinishingCommand command, CancellationToken cancellationToken) 218 | { 219 | events.Enqueue("Got FinishingCommand"); 220 | events.Enqueue("Signaling done!"); 221 | done.Set(); 222 | } 223 | } 224 | 225 | record FinishingCommand; 226 | } --------------------------------------------------------------------------------