|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using NServiceBus; |
| 5 | +using NServiceBus.Features; |
| 6 | +using NServiceBus.Settings; |
| 7 | + |
| 8 | +class ManifestOutput : Feature |
| 9 | +{ |
| 10 | + public ManifestOutput() |
| 11 | + { |
| 12 | + EnableByDefault(); |
| 13 | + Defaults(s => s.Set(new PersistenceManifest { Prefix = s.Get<string>("NServiceBus.Routing.EndpointName") })); |
| 14 | + } |
| 15 | + |
| 16 | + [System.Diagnostics.CodeAnalysis.SuppressMessage("Performance", "CA1822:Mark members as static", Justification = "Used exclusively for serialization")] |
| 17 | + internal class PersistenceManifest |
| 18 | + { |
| 19 | + Lazy<OutboxManifest> outboxManifest; |
| 20 | + Lazy<SagaManifest[]> sagaManifests; |
| 21 | + Lazy<SubscriptionManifest> subscriptionManifest; |
| 22 | + |
| 23 | + public string Prefix { get; init; } |
| 24 | + public string Dialect { get; set; } |
| 25 | + public OutboxManifest Outbox => outboxManifest?.Value; |
| 26 | + public SagaManifest[] Sagas => sagaManifests?.Value; |
| 27 | + public SubscriptionManifest SqlSubscriptions => subscriptionManifest?.Value; |
| 28 | + public string[] StorageTypes { get; set; } |
| 29 | + |
| 30 | + //initialising lazy so that any reflection/calculations don't slow down endpoint startup |
| 31 | + public void SetOutbox(Func<OutboxManifest> lazyOutbox) => outboxManifest = new Lazy<OutboxManifest>(lazyOutbox); |
| 32 | + public void SetSagas(Func<SagaManifest[]> lazySagas) => sagaManifests = new Lazy<SagaManifest[]>(lazySagas); |
| 33 | + public void SetSqlSubscriptions(Func<SubscriptionManifest> lazySubscription) => subscriptionManifest = new Lazy<SubscriptionManifest>(lazySubscription); |
| 34 | + |
| 35 | + //NOTE Some values are hardcoded so if the outbox script (Create_MsSqlServer.sql in ScriptBuilder/Outbox) changes this needs to be updated |
| 36 | + public record OutboxManifest |
| 37 | + { |
| 38 | + public string TableName { get; init; } |
| 39 | + public string PrimaryKey => "MessageId"; |
| 40 | + public IndexProperty[] Indexes => [ |
| 41 | + new() { Name = "Index_DispatchedAt", Columns = "DispatchedAt" }, |
| 42 | + new() { Name = "Index_Dispatched", Columns = "Dispatched" } |
| 43 | + ]; |
| 44 | + //object to allow for serialization of multiple subtypes. They will never be deserialized |
| 45 | + public object[] TableColumns => [ |
| 46 | + new VarcharSchemaProperty { Name = "MessageId", Length = "200", Mandatory = true }, |
| 47 | + new BitSchemaProperty { Name = "Dispatched", Mandatory = true, Default = false }, |
| 48 | + new SchemaProperty { Name = "DispatchedAt", Type = "datetime", Mandatory = false }, |
| 49 | + new VarcharSchemaProperty { Name = "PersistenceVersion", Length = "23", Mandatory = true }, |
| 50 | + new VarcharSchemaProperty { Name = "Operations", Length = "max", Mandatory = true } |
| 51 | + ]; |
| 52 | + } |
| 53 | + |
| 54 | + //NOTE Some values are hardcoded so if the saga script (MsSqlServerSagaScriptWriter.cs in ScriptBuilder/Saga) changes this needs to be updated |
| 55 | + public record SagaManifest |
| 56 | + { |
| 57 | + public string Name { get; init; } |
| 58 | + public string PrimaryKey => "Id"; |
| 59 | + public string TableName { get; init; } |
| 60 | + public IndexProperty[] Indexes { get; init; } |
| 61 | + //object to allow for serialization of multiple subtypes. They will never be deserialized |
| 62 | + public object[] TableColumns => [ |
| 63 | + new SchemaProperty { Name = "Id", Type = "guid", Mandatory = true }, |
| 64 | + new VarcharSchemaProperty { Name = "Metadata", Length = "max", Mandatory = true }, |
| 65 | + new VarcharSchemaProperty { Name = "Data", Length = "max", Mandatory = true }, |
| 66 | + new VarcharSchemaProperty { Name = "PersistenceVersion", Length = "23", Mandatory = true }, |
| 67 | + new VarcharSchemaProperty { Name = "SagaTypeVersion", Length = "23", Mandatory = true }, |
| 68 | + new SchemaProperty { Name = "Concurrency", Type = "integer", Mandatory = true } |
| 69 | + ]; |
| 70 | + } |
| 71 | + |
| 72 | + //NOTE Some values are hardcoded so if the subscription script (Create_MsSqlServer.sq in ScriptBuilder/Subscriptions) changes this needs to be updated |
| 73 | + public record SubscriptionManifest |
| 74 | + { |
| 75 | + public string TableName { get; init; } |
| 76 | + public string PrimaryKey => "Subscriber, MessageType"; |
| 77 | + public IndexProperty[] Indexes => []; |
| 78 | + //object to allow for serialization of multiple subtypes. They will never be deserialized |
| 79 | + public object[] TableColumns => [ |
| 80 | + new VarcharSchemaProperty { Name = "Subscriber", Length = "200", Mandatory = true }, |
| 81 | + new VarcharSchemaProperty { Name = "Endpoint", Length = "200", Mandatory = true }, |
| 82 | + new VarcharSchemaProperty { Name = "MessageType", Length = "200", Mandatory = true }, |
| 83 | + new VarcharSchemaProperty { Name = "PersistenceVersion", Length = "23", Mandatory = true } |
| 84 | + ]; |
| 85 | + } |
| 86 | + |
| 87 | + public record SchemaProperty |
| 88 | + { |
| 89 | + public string Name { get; init; } |
| 90 | + public virtual string Type { get; set; } |
| 91 | + public bool Mandatory { get; init; } |
| 92 | + } |
| 93 | + |
| 94 | + public record VarcharSchemaProperty : SchemaProperty |
| 95 | + { |
| 96 | + public override string Type => "string"; |
| 97 | + public string Length { get; init; } |
| 98 | + } |
| 99 | + |
| 100 | + public record BitSchemaProperty : SchemaProperty |
| 101 | + { |
| 102 | + public override string Type => "boolean"; |
| 103 | + public bool Default { get; init; } |
| 104 | + } |
| 105 | + |
| 106 | + public record IndexProperty |
| 107 | + { |
| 108 | + public string Name { get; init; } |
| 109 | + public string Columns { get; init; } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + protected override void Setup(FeatureConfigurationContext context) |
| 114 | + { |
| 115 | + var settings = context.Settings; |
| 116 | + if (!settings.HasSetting("SqlPersistence.SqlDialect")) |
| 117 | + { |
| 118 | + return; |
| 119 | + } |
| 120 | + |
| 121 | + settings.AddStartupDiagnosticsSection("Manifest-Persistence", () => GenerateManifest(context.Settings)); |
| 122 | + } |
| 123 | + |
| 124 | + static PersistenceManifest GenerateManifest(IReadOnlySettings settings) |
| 125 | + { |
| 126 | + var manifest = settings.Get<PersistenceManifest>(); |
| 127 | + manifest.Dialect = settings.GetSqlDialect().Name; |
| 128 | + |
| 129 | + if (settings.TryGet("ResultingSupportedStorages", out List<Type> supportedStorageTypes)) |
| 130 | + { |
| 131 | + manifest.StorageTypes = supportedStorageTypes.Select(storageType => storageType.Name).ToArray(); |
| 132 | + } |
| 133 | + |
| 134 | + return manifest; |
| 135 | + } |
| 136 | +} |
0 commit comments