Added abstractions
Internal service bus support enabled Added quartz support Reconfigured extensions
This commit is contained in:
@@ -0,0 +1,10 @@
|
|||||||
|
namespace LiteCharms.Abstractions;
|
||||||
|
|
||||||
|
public static class Constants
|
||||||
|
{
|
||||||
|
public const int QueueBounds = 100000;
|
||||||
|
|
||||||
|
public const string EmailServiceBus = nameof(EmailServiceBus);
|
||||||
|
public const string GeneralServiceBus = nameof(GeneralServiceBus);
|
||||||
|
public const string SalesServiceBus = nameof(SalesServiceBus);
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace LiteCharms.Abstractions;
|
||||||
|
|
||||||
|
public abstract class EventBusQueueBase
|
||||||
|
{
|
||||||
|
protected readonly Channel<IEvent> channel = Channel.CreateBounded<IEvent>(Constants.QueueBounds);
|
||||||
|
|
||||||
|
public ChannelWriter<IEvent> Outgoing => channel.Writer;
|
||||||
|
|
||||||
|
public ChannelReader<IEvent> Incoming => channel.Reader;
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
namespace LiteCharms.Abstractions;
|
||||||
|
|
||||||
|
public interface IEvent : INotification
|
||||||
|
{
|
||||||
|
Guid Id { get; set; }
|
||||||
|
|
||||||
|
string Name { get; set; }
|
||||||
|
|
||||||
|
DateTimeOffset EnqueueAt { get; set; }
|
||||||
|
|
||||||
|
string CorrelationId { get; set; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
namespace LiteCharms.Abstractions;
|
||||||
|
|
||||||
|
public interface IEventBus
|
||||||
|
{
|
||||||
|
Task<Result> PublishAsync<TEvent>(TEvent notification, CancellationToken cancellationToken = default)
|
||||||
|
where TEvent : class, IEvent;
|
||||||
|
}
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
namespace LiteCharms.Abstractions;
|
||||||
|
|
||||||
|
public interface IEventBusQueue
|
||||||
|
{
|
||||||
|
ChannelWriter<IEvent> Outgoing { get; }
|
||||||
|
|
||||||
|
ChannelReader<IEvent> Incoming { get; }
|
||||||
|
}
|
||||||
@@ -0,0 +1,10 @@
|
|||||||
|
namespace LiteCharms.Abstractions;
|
||||||
|
|
||||||
|
public interface IJobOrchestrator
|
||||||
|
{
|
||||||
|
Task SendAsync<TNotification>(TNotification notification, CancellationToken cancellationToken = default)
|
||||||
|
where TNotification : IEvent;
|
||||||
|
|
||||||
|
Task ScheduleAsync<TNotification>(TNotification notification, string cronExpression, CancellationToken cancellationToken = default)
|
||||||
|
where TNotification : IEvent;
|
||||||
|
}
|
||||||
@@ -0,0 +1,40 @@
|
|||||||
|
<Project Sdk="Microsoft.NET.Sdk">
|
||||||
|
|
||||||
|
<PropertyGroup>
|
||||||
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
|
<Nullable>enable</Nullable>
|
||||||
|
<SignAssembly>True</SignAssembly>
|
||||||
|
<AssemblyOriginatorKeyFile>..\LiteCharms.snk</AssemblyOriginatorKeyFile>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<!-- Nuget Package Details -->
|
||||||
|
<PropertyGroup>
|
||||||
|
<PackageId>LiteCharms.Abstractions</PackageId>
|
||||||
|
<Version>1.0.0</Version>
|
||||||
|
<Authors>Khwezi Mngoma</Authors>
|
||||||
|
<Company>Lite Charms (PTY) Ltd</Company>
|
||||||
|
<Description>Shared abstractions for Lite Charms applications.</Description>
|
||||||
|
<PackageProjectUrl>https://gitea.khongisa.co.za/litecharms/components</PackageProjectUrl>
|
||||||
|
<RepositoryUrl>https://gitea.khongisa.co.za/litecharms/components.git</RepositoryUrl>
|
||||||
|
<RepositoryType>git</RepositoryType>
|
||||||
|
<PackageLicenseFile>LICENSE</PackageLicenseFile>
|
||||||
|
<PackageTags>utility;dotnet</PackageTags>
|
||||||
|
<PackageIcon>icon.png</PackageIcon>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<None Include="..\LICENSE" Pack="true" PackagePath="\" />
|
||||||
|
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="FluentResults" Version="4.0.0" />
|
||||||
|
<PackageReference Include="Mediator.Abstractions" Version="3.0.2" />
|
||||||
|
|
||||||
|
<Using Include="Mediator" />
|
||||||
|
<Using Include="FluentResults" />
|
||||||
|
<Using Include="System.Threading.Channels" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
</Project>
|
||||||
@@ -0,0 +1,27 @@
|
|||||||
|
namespace LiteCharms.Abstractions;
|
||||||
|
|
||||||
|
public static class Timezones
|
||||||
|
{
|
||||||
|
public static TimeZoneInfo SouthAfricanTimeZone => TimeZoneInfo.FindSystemTimeZoneById("South Africa Standard Time");
|
||||||
|
|
||||||
|
public static string? LocaliseDateTime(this DateTime dateTime, TimeSpan offset) => offset.Hours > 0
|
||||||
|
? $"{dateTime:yyyy-MM-ddTHH:mm:ss.fff}+{offset.Hours:00}:{offset.Minutes:00}"
|
||||||
|
: $"{dateTime:yyyy-MM-ddTHH:mm:ss.fff}{offset.Hours:00}:{offset.Minutes:00}";
|
||||||
|
|
||||||
|
public static string? LocaliseDateTimeOffset(this DateTimeOffset dateTime, TimeSpan offset) => LocaliseDateTime(dateTime.DateTime, offset);
|
||||||
|
|
||||||
|
public static DateTimeOffset ToDateTimeWithTimeZone(this DateTime source, TimeZoneInfo? timezone = null)
|
||||||
|
{
|
||||||
|
DateTime sourceDateAdjusted = source.Kind != DateTimeKind.Utc
|
||||||
|
? new(source.Ticks, DateTimeKind.Utc)
|
||||||
|
: source;
|
||||||
|
|
||||||
|
var localised = timezone is null
|
||||||
|
? new DateTimeOffset(sourceDateAdjusted.Ticks, SouthAfricanTimeZone.BaseUtcOffset).LocaliseDateTimeOffset(SouthAfricanTimeZone.BaseUtcOffset)
|
||||||
|
: new DateTimeOffset(sourceDateAdjusted.Ticks, timezone!.BaseUtcOffset).LocaliseDateTimeOffset(timezone.BaseUtcOffset);
|
||||||
|
|
||||||
|
return DateTimeOffset.Parse(localised!);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static DateTimeOffset UtcNow(this TimeZoneInfo timezone) => ToDateTimeWithTimeZone(DateTime.Now, timezone);
|
||||||
|
}
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
using LiteCharms.Models.Configuraton.Email;
|
||||||
|
|
||||||
|
namespace LiteCharms.Extensions;
|
||||||
|
|
||||||
|
public static class Email
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddEmailServices(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
services.Configure<SmtpSettings>(configuration.GetSection("Email"));
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,34 @@
|
|||||||
|
using LiteCharms.Infrastructure.HealthChecks;
|
||||||
|
|
||||||
|
namespace LiteCharms.Extensions;
|
||||||
|
|
||||||
|
public static class HealthChecks
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddQuartzHealtchCheck(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddHealthChecks().AddCheck<QuartzHealthCheck>("Quartz");
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddPostgresHealtchCheck(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.AddHealthChecks().AddCheck<PostgresHealthCheck>("PostgreSQL");
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static IServiceCollection AddHealthChecksSupport(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
services.AddHealthChecks()
|
||||||
|
.AddCheck("Self", () => HealthCheckResult.Healthy());
|
||||||
|
|
||||||
|
//services.AddHealthChecksUI(setup =>
|
||||||
|
//{
|
||||||
|
// setup.AddHealthCheckEndpoint("Lead Generator", $"{configuration["ASPNETCORE_URLS"]}/health");
|
||||||
|
// setup.SetEvaluationTimeInSeconds(15);
|
||||||
|
//}).AddInMemoryStorage(databaseName: "healthuidb");
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -6,8 +6,19 @@
|
|||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<SignAssembly>True</SignAssembly>
|
<SignAssembly>True</SignAssembly>
|
||||||
<AssemblyOriginatorKeyFile>..\LiteCharms.snk</AssemblyOriginatorKeyFile>
|
<AssemblyOriginatorKeyFile>..\LiteCharms.snk</AssemblyOriginatorKeyFile>
|
||||||
|
<JsonSerializerIsReflectionEnabledByDefault>true</JsonSerializerIsReflectionEnabledByDefault>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
|
<!-- Warnings And Exclusions -->
|
||||||
|
<PropertyGroup>
|
||||||
|
<NoWarn>$(NoWarn);MA0004</NoWarn>
|
||||||
|
<!-- https://github.com/dotnet/aspnetcore/issues/50836 -->
|
||||||
|
<NoWarn>$(NoWarn);AD0001</NoWarn>
|
||||||
|
<PublishTrimmed>true</PublishTrimmed>
|
||||||
|
<NoWarn>$(NoWarn);IL2080;IL2065;IL2075;IL2087;IL2057;IL2060;IL2070;IL2067;IL2072;IL2026;IL2104</NoWarn>
|
||||||
|
<NoWarn>$(NoWarn);IL2110;IL2111</NoWarn>
|
||||||
|
</PropertyGroup>
|
||||||
|
|
||||||
<!-- Nuget Package Details -->
|
<!-- Nuget Package Details -->
|
||||||
<PropertyGroup>
|
<PropertyGroup>
|
||||||
<PackageId>LiteCharms.Extensions</PackageId>
|
<PackageId>LiteCharms.Extensions</PackageId>
|
||||||
@@ -28,14 +39,6 @@
|
|||||||
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- Shared Usings -->
|
|
||||||
<ItemGroup>
|
|
||||||
<Using Include="Microsoft.AspNetCore.Builder" />
|
|
||||||
<Using Include="Microsoft.Extensions.Configuration" />
|
|
||||||
<Using Include="Microsoft.Extensions.DependencyInjection" />
|
|
||||||
<Using Include="Microsoft.Extensions.Logging" />
|
|
||||||
</ItemGroup>
|
|
||||||
|
|
||||||
<!-- Health Checks -->
|
<!-- Health Checks -->
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="9.0.0" />
|
<PackageReference Include="AspNetCore.HealthChecks.UI.Client" Version="9.0.0" />
|
||||||
@@ -44,7 +47,8 @@
|
|||||||
<PackageReference Include="AspNetCore.HealthChecks.UI.Data" Version="9.0.0" />
|
<PackageReference Include="AspNetCore.HealthChecks.UI.Data" Version="9.0.0" />
|
||||||
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="9.0.0" />
|
<PackageReference Include="AspNetCore.HealthChecks.UI.InMemory.Storage" Version="9.0.0" />
|
||||||
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.7" />
|
<PackageReference Include="Microsoft.EntityFrameworkCore.InMemory" Version="10.0.7" />
|
||||||
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.7" />
|
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="10.0.7" />
|
||||||
|
<PackageReference Include="Quartz.AspNetCore" Version="3.18.1" />
|
||||||
|
|
||||||
<!-- Global Usings -->
|
<!-- Global Usings -->
|
||||||
<Using Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
|
<Using Include="Microsoft.Extensions.Diagnostics.HealthChecks" />
|
||||||
@@ -58,6 +62,7 @@
|
|||||||
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.15.2" />
|
<PackageReference Include="OpenTelemetry.Instrumentation.AspNetCore" Version="1.15.2" />
|
||||||
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.15.1" />
|
<PackageReference Include="OpenTelemetry.Instrumentation.Http" Version="1.15.1" />
|
||||||
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.15.1" />
|
<PackageReference Include="OpenTelemetry.Instrumentation.Runtime" Version="1.15.1" />
|
||||||
|
<PackageReference Include="OpenTelemetry.Exporter.Console" Version="1.15.3" />
|
||||||
|
|
||||||
<!-- Global Usings -->
|
<!-- Global Usings -->
|
||||||
<Using Include="OpenTelemetry.Resources" />
|
<Using Include="OpenTelemetry.Resources" />
|
||||||
@@ -88,6 +93,16 @@
|
|||||||
<Using Include="Microsoft.EntityFrameworkCore.Metadata.Builders" />
|
<Using Include="Microsoft.EntityFrameworkCore.Metadata.Builders" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!-- Shared Usings -->
|
||||||
|
<ItemGroup>
|
||||||
|
<Using Include="Quartz" />
|
||||||
|
<Using Include="Microsoft.AspNetCore.Builder" />
|
||||||
|
<Using Include="Microsoft.Extensions.Configuration" />
|
||||||
|
<Using Include="Microsoft.Extensions.DependencyInjection" />
|
||||||
|
<Using Include="Microsoft.Extensions.Logging" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!-- Project References -->
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<ProjectReference Include="..\LiteCharms.Entities\LiteCharms.Entities.csproj" />
|
<ProjectReference Include="..\LiteCharms.Entities\LiteCharms.Entities.csproj" />
|
||||||
<ProjectReference Include="..\LiteCharms.Infrastructure\LiteCharms.Infrastructure.csproj" />
|
<ProjectReference Include="..\LiteCharms.Infrastructure\LiteCharms.Infrastructure.csproj" />
|
||||||
|
|||||||
@@ -1,41 +1,7 @@
|
|||||||
using LiteCharms.Infrastructure.Database;
|
namespace LiteCharms.Extensions;
|
||||||
using LiteCharms.Infrastructure.HealthChecks;
|
|
||||||
using LiteCharms.Models.Configuraton.Email;
|
|
||||||
|
|
||||||
namespace LiteCharms.Extensions;
|
public static class Monitoring
|
||||||
|
|
||||||
public static class Services
|
|
||||||
{
|
{
|
||||||
public static IServiceCollection AddEmailServices(this IServiceCollection services, IConfiguration configuration)
|
|
||||||
{
|
|
||||||
services.Configure<SmtpSettings>(configuration.GetSection("Email"));
|
|
||||||
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IServiceCollection AddHealthChecksSupport(this IServiceCollection services, IConfiguration configuration)
|
|
||||||
{
|
|
||||||
services.AddHealthChecks()
|
|
||||||
.AddCheck("Self", () => HealthCheckResult.Healthy())
|
|
||||||
.AddCheck<PostgresHealthCheck>("PostgreSQL");
|
|
||||||
|
|
||||||
//services.AddHealthChecksUI(setup =>
|
|
||||||
//{
|
|
||||||
// setup.AddHealthCheckEndpoint("Lead Generator", $"{configuration["ASPNETCORE_URLS"]}/health");
|
|
||||||
// setup.SetEvaluationTimeInSeconds(15);
|
|
||||||
//}).AddInMemoryStorage(databaseName: "healthuidb");
|
|
||||||
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static IServiceCollection AddLeadGeneratorDatabase(this IServiceCollection services, IConfiguration configuration)
|
|
||||||
{
|
|
||||||
services.AddPooledDbContextFactory<LeadGeneratorDbContext>(options =>
|
|
||||||
options.UseNpgsql(configuration.GetConnectionString("PostgresLeadGenerator")));
|
|
||||||
|
|
||||||
return services;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static WebApplicationBuilder AddMonitoring(this WebApplicationBuilder builder)
|
public static WebApplicationBuilder AddMonitoring(this WebApplicationBuilder builder)
|
||||||
{
|
{
|
||||||
var serviceName = builder.Configuration.GetValue<string>("Monitoring:ServiceName") ?? "LiteCharms";
|
var serviceName = builder.Configuration.GetValue<string>("Monitoring:ServiceName") ?? "LiteCharms";
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
using LiteCharms.Infrastructure.Database;
|
||||||
|
|
||||||
|
namespace LiteCharms.Extensions;
|
||||||
|
|
||||||
|
public static class Postgres
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddLeadGeneratorDatabase(this IServiceCollection services, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
services.AddPooledDbContextFactory<LeadGeneratorDbContext>(options =>
|
||||||
|
options.UseNpgsql(configuration.GetConnectionString("PostgresLeadGenerator")));
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,64 @@
|
|||||||
|
using LiteCharms.Abstractions;
|
||||||
|
using LiteCharms.Infrastructure.Quartz;
|
||||||
|
|
||||||
|
namespace LiteCharms.Extensions;
|
||||||
|
|
||||||
|
public static class Quartz
|
||||||
|
{
|
||||||
|
private const string databaseConfigName = "PostgresScheduler";
|
||||||
|
|
||||||
|
public static IServiceCollection AddQuartzScheduler(this IServiceCollection services, string schedulerName, string schedulerId, IConfiguration configuration)
|
||||||
|
{
|
||||||
|
var connectionString = configuration.GetConnectionString(databaseConfigName);
|
||||||
|
|
||||||
|
services.ConfigureCommon();
|
||||||
|
|
||||||
|
services.AddQuartz(config =>
|
||||||
|
{
|
||||||
|
config.SchedulerName = schedulerName;
|
||||||
|
config.SchedulerId = schedulerId;
|
||||||
|
config.InterruptJobsOnShutdown = true;
|
||||||
|
config.InterruptJobsOnShutdownWithWait = true;
|
||||||
|
config.MaxBatchSize = 5;
|
||||||
|
|
||||||
|
config.UseSimpleTypeLoader();
|
||||||
|
config.UseDefaultThreadPool(options => options.MaxConcurrency = 1);
|
||||||
|
config.UseTimeZoneConverter();
|
||||||
|
|
||||||
|
config.UsePersistentStore(storage =>
|
||||||
|
{
|
||||||
|
storage.PerformSchemaValidation = false;
|
||||||
|
|
||||||
|
storage.UseSystemTextJsonSerializer();
|
||||||
|
storage.SetProperty("quartz.jobStore.clustered", "true");
|
||||||
|
storage.SetProperty("quartz.jobStore.tablePrefix", "quartz_");
|
||||||
|
|
||||||
|
storage.UsePostgres(connectionString!);
|
||||||
|
storage.UseClustering(cluster =>
|
||||||
|
{
|
||||||
|
cluster.CheckinInterval = TimeSpan.FromSeconds(30);
|
||||||
|
cluster.CheckinMisfireThreshold = TimeSpan.FromSeconds(2);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static IServiceCollection ConfigureCommon(this IServiceCollection services)
|
||||||
|
{
|
||||||
|
services.Configure<QuartzOptions>(options =>
|
||||||
|
{
|
||||||
|
options.Scheduling.IgnoreDuplicates = true;
|
||||||
|
options.Scheduling.OverWriteExistingData = true;
|
||||||
|
options["quartz.plugin.jobHistory.type"] = "Quartz.Plugin.History.LoggingJobHistoryPlugin, Quartz.Plugins";
|
||||||
|
options["quartz.plugin.triggerHistory.type"] = "Quartz.Plugin.History.LoggingTriggerHistoryPlugin, Quartz.Plugins";
|
||||||
|
});
|
||||||
|
|
||||||
|
services.AddTransient<RetryJobListener>();
|
||||||
|
services.AddTransient<IJobOrchestrator, JobOrchestrator>();
|
||||||
|
services.AddQuartzHostedService(options => options.WaitForJobsToComplete = true);
|
||||||
|
|
||||||
|
return services;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,26 @@
|
|||||||
|
using LiteCharms.Abstractions;
|
||||||
|
using LiteCharms.Infrastructure.ServiceBus;
|
||||||
|
using LiteCharms.Infrastructure.ServiceBus.Exchanges;
|
||||||
|
|
||||||
|
namespace LiteCharms.Extensions;
|
||||||
|
|
||||||
|
public static class ServiceBus
|
||||||
|
{
|
||||||
|
public static IServiceCollection AddGeneralServiceBus(this IServiceCollection services) => services
|
||||||
|
.AddSingleton<GeneralServiceBus>()
|
||||||
|
.AddHostedService<GeneralExchange>()
|
||||||
|
.AddKeyedTransient<IEventBus, GeneralServiceBus>(Constants.GeneralServiceBus)
|
||||||
|
.AddMemoryCache();
|
||||||
|
|
||||||
|
public static IServiceCollection AddEmailServiceBus(this IServiceCollection services) => services
|
||||||
|
.AddSingleton<EmailServiceBus>()
|
||||||
|
.AddHostedService<EmailExchange>()
|
||||||
|
.AddKeyedTransient<IEventBus, EmailServiceBus>(Constants.EmailServiceBus)
|
||||||
|
.AddMemoryCache();
|
||||||
|
|
||||||
|
public static IServiceCollection AddSalesServiceBus(this IServiceCollection services) => services
|
||||||
|
.AddSingleton<SalesServiceBus>()
|
||||||
|
.AddHostedService<SalesExchange>()
|
||||||
|
.AddKeyedTransient<IEventBus, SalesServiceBus>(Constants.SalesServiceBus)
|
||||||
|
.AddMemoryCache();
|
||||||
|
}
|
||||||
@@ -0,0 +1,23 @@
|
|||||||
|
namespace LiteCharms.Infrastructure.HealthChecks;
|
||||||
|
|
||||||
|
public class QuartzHealthCheck(ISchedulerFactory schedulerFactory) : IHealthCheck
|
||||||
|
{
|
||||||
|
public async Task<HealthCheckResult> CheckHealthAsync(HealthCheckContext context, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var scheduler = await schedulerFactory.GetScheduler(cancellationToken);
|
||||||
|
|
||||||
|
if (!scheduler.IsStarted)
|
||||||
|
return HealthCheckResult.Unhealthy("Quartz scheduler is not running");
|
||||||
|
|
||||||
|
await scheduler.CheckExists(new JobKey(Guid.NewGuid().ToString()), cancellationToken);
|
||||||
|
|
||||||
|
return HealthCheckResult.Healthy("Quartz scheduler is ready");
|
||||||
|
}
|
||||||
|
catch (SchedulerException)
|
||||||
|
{
|
||||||
|
return HealthCheckResult.Unhealthy("Quartz scheduler cannot connect to the store");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -28,6 +28,19 @@
|
|||||||
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
<None Include="..\icon.png" Pack="true" PackagePath="\" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!-- Quartz Scheduler-->
|
||||||
|
<ItemGroup>
|
||||||
|
<PackageReference Include="Quartz" Version="3.18.1" />
|
||||||
|
<PackageReference Include="Quartz.Plugins" Version="3.18.1" />
|
||||||
|
<PackageReference Include="Quartz.Plugins.TimeZoneConverter" Version="3.18.1" />
|
||||||
|
<PackageReference Include="Quartz.Serialization.SystemTextJson" Version="3.18.1" />
|
||||||
|
|
||||||
|
<!-- Global Usings -->
|
||||||
|
<Using Include="Quartz" />
|
||||||
|
<Using Include="Mediator" />
|
||||||
|
<Using Include="FluentResults" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- Configuration -->
|
<!-- Configuration -->
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.7" />
|
<PackageReference Include="Microsoft.Extensions.Configuration" Version="10.0.7" />
|
||||||
@@ -70,10 +83,18 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<!-- Project References -->
|
<!-- Project References -->
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
<ProjectReference Include="..\LiteCharms.Abstractions\LiteCharms.Abstractions.csproj" />
|
||||||
<ProjectReference Include="..\LiteCharms.Entities\LiteCharms.Entities.csproj" />
|
<ProjectReference Include="..\LiteCharms.Entities\LiteCharms.Entities.csproj" />
|
||||||
<ProjectReference Include="..\LiteCharms.Models\LiteCharms.Models.csproj" />
|
<ProjectReference Include="..\LiteCharms.Models\LiteCharms.Models.csproj" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
|
<!-- Global Usings -->
|
||||||
|
<ItemGroup>
|
||||||
|
<Using Include="System.Text.Json" />
|
||||||
|
<Using Include="Microsoft.Extensions.Hosting" />
|
||||||
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<None Update="appsettings.json">
|
<None Update="appsettings.json">
|
||||||
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
|
||||||
|
|||||||
@@ -0,0 +1,66 @@
|
|||||||
|
using LiteCharms.Abstractions;
|
||||||
|
using static LiteCharms.Abstractions.Timezones;
|
||||||
|
|
||||||
|
namespace LiteCharms.Infrastructure.Quartz;
|
||||||
|
|
||||||
|
public class JobOrchestrator(ISchedulerFactory schedulerFactory) : IJobOrchestrator
|
||||||
|
{
|
||||||
|
public async Task SendAsync<TNotification>(TNotification notification, CancellationToken cancellationToken = default)
|
||||||
|
where TNotification : IEvent
|
||||||
|
{
|
||||||
|
var chainedJobGroup = "onetime-jobs";
|
||||||
|
|
||||||
|
var scheduler = await schedulerFactory.GetScheduler(cancellationToken);
|
||||||
|
var jobKey = new JobKey($"{notification.Name.ToLower()}-{notification.CorrelationId.ToLower()}", chainedJobGroup);
|
||||||
|
var triggerKey = new TriggerKey($"{jobKey.Name}-trigger", chainedJobGroup);
|
||||||
|
|
||||||
|
var job = JobBuilder.Create<MediatorJob<TNotification>>()
|
||||||
|
.WithIdentity(jobKey)
|
||||||
|
.WithDescription($"Correlation ID: {notification.CorrelationId}")
|
||||||
|
.UsingJobData(new JobDataMap { ["Payload"] = JsonSerializer.Serialize(notification) })
|
||||||
|
.DisallowConcurrentExecution()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var trigger = global::Quartz.TriggerBuilder.Create()
|
||||||
|
.WithIdentity(triggerKey)
|
||||||
|
.StartNow()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
await scheduler.ScheduleJob(job, new List<ITrigger> { trigger }.AsReadOnly(), replace: true, cancellationToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async Task ScheduleAsync<TNotification>(TNotification notification, string cronExpression, CancellationToken cancellationToken = default)
|
||||||
|
where TNotification : IEvent
|
||||||
|
{
|
||||||
|
var chainedJobGroup = "scheduled-jobs";
|
||||||
|
|
||||||
|
var scheduler = await schedulerFactory.GetScheduler(cancellationToken);
|
||||||
|
var jobKey = new JobKey($"{notification.Name.ToLower()}-{notification.CorrelationId.ToLower()}", chainedJobGroup);
|
||||||
|
var triggerKey = new TriggerKey($"{jobKey.Name}-trigger", chainedJobGroup);
|
||||||
|
|
||||||
|
var job = JobBuilder.Create<MediatorJob<TNotification>>()
|
||||||
|
.WithIdentity(jobKey)
|
||||||
|
.WithDescription($"Correlation ID: {notification.CorrelationId}")
|
||||||
|
.UsingJobData(new JobDataMap { ["Payload"] = JsonSerializer.Serialize(notification) })
|
||||||
|
.DisallowConcurrentExecution()
|
||||||
|
.StoreDurably()
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
var now = SouthAfricanTimeZone.UtcNow();
|
||||||
|
|
||||||
|
var trigger = global::Quartz.TriggerBuilder.Create()
|
||||||
|
.WithIdentity(triggerKey)
|
||||||
|
.WithDescription($"Scheduled via Main Job at {now:g}")
|
||||||
|
.WithCronSchedule(cronExpression, cron => cron.InTimeZone(SouthAfricanTimeZone)
|
||||||
|
.WithMisfireHandlingInstructionFireAndProceed())
|
||||||
|
.StartAt(now)
|
||||||
|
.Build();
|
||||||
|
|
||||||
|
await scheduler.AddJob(job, replace: true, cancellationToken);
|
||||||
|
|
||||||
|
if (await scheduler.CheckExists(triggerKey, cancellationToken))
|
||||||
|
await scheduler.RescheduleJob(triggerKey, trigger, cancellationToken);
|
||||||
|
else
|
||||||
|
await scheduler.ScheduleJob(job, new List<ITrigger> { trigger }.AsReadOnly(), replace: true, cancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
using LiteCharms.Abstractions;
|
||||||
|
|
||||||
|
namespace LiteCharms.Infrastructure.Quartz;
|
||||||
|
|
||||||
|
[DisallowConcurrentExecution]
|
||||||
|
public class MediatorJob<TNotification>(IMediator mediator) : IJob where TNotification : IEvent
|
||||||
|
{
|
||||||
|
public async Task Execute(IJobExecutionContext context)
|
||||||
|
{
|
||||||
|
var data = context.MergedJobDataMap["Payload"] as string;
|
||||||
|
|
||||||
|
if (string.IsNullOrWhiteSpace(data)) return;
|
||||||
|
|
||||||
|
var notification = JsonSerializer.Deserialize<TNotification>(data);
|
||||||
|
|
||||||
|
if(notification is null) return;
|
||||||
|
|
||||||
|
if(notification is TNotification)
|
||||||
|
await mediator.Publish(notification, context.CancellationToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,18 @@
|
|||||||
|
namespace LiteCharms.Infrastructure.Quartz;
|
||||||
|
|
||||||
|
public class RetryJobListener : IJobListener
|
||||||
|
{
|
||||||
|
public string Name => "RetryJobListener";
|
||||||
|
|
||||||
|
public int RetryCount { get; set; } = 3;
|
||||||
|
|
||||||
|
public Task JobExecutionVetoed(IJobExecutionContext context, CancellationToken cancellationToken = default) => Task.CompletedTask;
|
||||||
|
|
||||||
|
public Task JobToBeExecuted(IJobExecutionContext context, CancellationToken cancellationToken = default) => Task.CompletedTask;
|
||||||
|
|
||||||
|
public async Task JobWasExecuted(IJobExecutionContext context, JobExecutionException? jobException, CancellationToken cancellationToken = default)
|
||||||
|
{
|
||||||
|
if (jobException is not null && context.RefireCount < RetryCount)
|
||||||
|
jobException.RefireImmediately = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using LiteCharms.Abstractions;
|
||||||
|
using LiteCharms.Infrastructure.ServiceBus.Queues;
|
||||||
|
|
||||||
|
namespace LiteCharms.Infrastructure.ServiceBus;
|
||||||
|
|
||||||
|
public class EmailServiceBus(EmailQueue messages) : IEventBus
|
||||||
|
{
|
||||||
|
public async Task<Result> PublishAsync<TEvent>(TEvent notification, CancellationToken cancellationToken = default)
|
||||||
|
where TEvent : class, IEvent
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await messages.Outgoing.WriteAsync(notification, cancellationToken);
|
||||||
|
|
||||||
|
return Result.Ok();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using LiteCharms.Infrastructure.ServiceBus.Queues;
|
||||||
|
|
||||||
|
namespace LiteCharms.Infrastructure.ServiceBus.Exchanges;
|
||||||
|
|
||||||
|
public class EmailExchange(EmailQueue messages) : BackgroundService
|
||||||
|
{
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
if(messages.Incoming.CanCount)
|
||||||
|
await Task.Delay(1000, stoppingToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using LiteCharms.Infrastructure.ServiceBus.Queues;
|
||||||
|
|
||||||
|
namespace LiteCharms.Infrastructure.ServiceBus.Exchanges;
|
||||||
|
|
||||||
|
public class GeneralExchange(GeneralQueue messages) : BackgroundService
|
||||||
|
{
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
if (messages.Incoming.CanCount)
|
||||||
|
await Task.Delay(1000, stoppingToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,12 @@
|
|||||||
|
using LiteCharms.Infrastructure.ServiceBus.Queues;
|
||||||
|
|
||||||
|
namespace LiteCharms.Infrastructure.ServiceBus.Exchanges;
|
||||||
|
|
||||||
|
public class SalesExchange(SalesQueue messages) : BackgroundService
|
||||||
|
{
|
||||||
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||||
|
{
|
||||||
|
if (messages.Incoming.CanCount)
|
||||||
|
await Task.Delay(1000, stoppingToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using LiteCharms.Abstractions;
|
||||||
|
using LiteCharms.Infrastructure.ServiceBus.Queues;
|
||||||
|
|
||||||
|
namespace LiteCharms.Infrastructure.ServiceBus;
|
||||||
|
|
||||||
|
public class GeneralServiceBus(GeneralQueue messages) : IEventBus
|
||||||
|
{
|
||||||
|
public async Task<Result> PublishAsync<TEvent>(TEvent notification, CancellationToken cancellationToken = default)
|
||||||
|
where TEvent : class, IEvent
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await messages.Outgoing.WriteAsync(notification, cancellationToken);
|
||||||
|
|
||||||
|
return Result.Ok();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
using LiteCharms.Abstractions;
|
||||||
|
|
||||||
|
namespace LiteCharms.Infrastructure.ServiceBus.Queues;
|
||||||
|
|
||||||
|
public class EmailQueue : EventBusQueueBase, IEventBusQueue;
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
using LiteCharms.Abstractions;
|
||||||
|
|
||||||
|
namespace LiteCharms.Infrastructure.ServiceBus.Queues;
|
||||||
|
|
||||||
|
public class GeneralQueue : EventBusQueueBase, IEventBusQueue;
|
||||||
@@ -0,0 +1,5 @@
|
|||||||
|
using LiteCharms.Abstractions;
|
||||||
|
|
||||||
|
namespace LiteCharms.Infrastructure.ServiceBus.Queues;
|
||||||
|
|
||||||
|
public class SalesQueue : EventBusQueueBase, IEventBusQueue;
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
using LiteCharms.Abstractions;
|
||||||
|
using LiteCharms.Infrastructure.ServiceBus.Queues;
|
||||||
|
|
||||||
|
namespace LiteCharms.Infrastructure.ServiceBus;
|
||||||
|
|
||||||
|
public class SalesServiceBus(SalesQueue messages) : IEventBus
|
||||||
|
{
|
||||||
|
public async Task<Result> PublishAsync<TEvent>(TEvent notification, CancellationToken cancellationToken = default)
|
||||||
|
where TEvent : class, IEvent
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
await messages.Outgoing.WriteAsync(notification, cancellationToken);
|
||||||
|
|
||||||
|
return Result.Ok();
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
return Result.Fail(new Error(ex.Message).CausedBy(ex));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -4,8 +4,8 @@
|
|||||||
<TargetFramework>net10.0</TargetFramework>
|
<TargetFramework>net10.0</TargetFramework>
|
||||||
<ImplicitUsings>enable</ImplicitUsings>
|
<ImplicitUsings>enable</ImplicitUsings>
|
||||||
<Nullable>enable</Nullable>
|
<Nullable>enable</Nullable>
|
||||||
<AssemblyOriginatorKeyFile>..\LiteCharms.snk</AssemblyOriginatorKeyFile>
|
|
||||||
<SignAssembly>True</SignAssembly>
|
<SignAssembly>True</SignAssembly>
|
||||||
|
<AssemblyOriginatorKeyFile>..\LiteCharms.snk</AssemblyOriginatorKeyFile>
|
||||||
</PropertyGroup>
|
</PropertyGroup>
|
||||||
|
|
||||||
<!-- Nuget Package Details -->
|
<!-- Nuget Package Details -->
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
<Solution>
|
<Solution>
|
||||||
|
<Project Path="LiteCharms.Abstractions/LiteCharms.Abstractions.csproj" Id="e080e621-5394-4260-a793-d54178401942" />
|
||||||
<Project Path="LiteCharms.Entities/LiteCharms.Entities.csproj" />
|
<Project Path="LiteCharms.Entities/LiteCharms.Entities.csproj" />
|
||||||
<Project Path="LiteCharms.Extensions/LiteCharms.Extensions.csproj" />
|
<Project Path="LiteCharms.Extensions/LiteCharms.Extensions.csproj" />
|
||||||
<Project Path="LiteCharms.Features/LiteCharms.Features.csproj" />
|
<Project Path="LiteCharms.Features/LiteCharms.Features.csproj" />
|
||||||
|
|||||||
Reference in New Issue
Block a user