Skip to content
Merged
1 change: 1 addition & 0 deletions src/All.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@
<Project Path="Mocha/src/Mocha.Mediator.Abstractions/Mocha.Mediator.Abstractions.csproj" />
<Project Path="Mocha/src/Mocha.Mediator/Mocha.Mediator.csproj" />
<Project Path="Mocha/src/Mocha.Outbox/Mocha.Outbox.csproj" />
<Project Path="Mocha/src/Mocha.Scheduling/Mocha.Scheduling.csproj" />
<Project Path="Mocha/src/Mocha.Threading/Mocha.Threading.csproj" />
<Project Path="Mocha/src/Mocha.Transport.InMemory/Mocha.Transport.InMemory.csproj" />
<Project Path="Mocha/src/Mocha.Transport.Postgres/Mocha.Transport.Postgres.csproj" />
Expand Down
1 change: 1 addition & 0 deletions src/Mocha/Mocha.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<Project Path="src/Mocha.Mediator.Abstractions/Mocha.Mediator.Abstractions.csproj" />
<Project Path="src/Mocha.Mediator/Mocha.Mediator.csproj" />
<Project Path="src/Mocha.Outbox/Mocha.Outbox.csproj" />
<Project Path="src/Mocha.Scheduling/Mocha.Scheduling.csproj" />
<Project Path="src/Mocha.Threading/Mocha.Threading.csproj" />
<Project Path="src/Mocha.Mediator/Mocha.Mediator.csproj" />
<Project Path="src/Mocha.Analyzers/Mocha.Analyzers.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public void Cleanup()
obj.Message = s_message;
obj.MessageType = s_type;
var result = obj.Message;
// No return GC collects it
// No return - GC collects it
return result;
}

Expand Down
2 changes: 2 additions & 0 deletions src/Mocha/src/Demo/Demo.Billing/Data/BillingDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using Microsoft.EntityFrameworkCore.Design;
using Mocha.Inbox;
using Mocha.Outbox;
using Mocha.Scheduling;

namespace Demo.Billing.Data;

Expand All @@ -20,6 +21,7 @@ protected override void OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.AddPostgresOutbox();
modelBuilder.AddPostgresInbox();
modelBuilder.AddPostgresScheduledMessages();

modelBuilder.Entity<Invoice>(entity =>
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,5 +70,21 @@ await messageBus.PublishAsync(
cancellationToken);

logger.LogInformation("PaymentCompletedEvent published for order {OrderId}", message.OrderId);

// Schedule a payment reminder 30 seconds from now
await messageBus.SchedulePublishAsync(
new PaymentReminderEvent
{
InvoiceId = invoice.Id,
OrderId = message.OrderId,
CustomerId = message.CustomerId,
Amount = invoice.Amount
},
DateTimeOffset.UtcNow.AddSeconds(30),
cancellationToken);

logger.LogInformation(
"Payment reminder scheduled for order {OrderId} in 30 seconds",
message.OrderId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using Demo.Billing.Data;
using Demo.Billing.Entities;
using Demo.Contracts.Events;
using Microsoft.EntityFrameworkCore;
using Mocha;

namespace Demo.Billing.Handlers;

public class PaymentReminderEventHandler(
BillingDbContext db,
ILogger<PaymentReminderEventHandler> logger) : IEventHandler<PaymentReminderEvent>
{
public async ValueTask HandleAsync(PaymentReminderEvent message, CancellationToken cancellationToken)
{
var invoice = await db.Invoices.FirstOrDefaultAsync(i => i.Id == message.InvoiceId, cancellationToken);

if (invoice is null)
{
logger.LogWarning("Payment reminder for invoice {InvoiceId} - invoice not found", message.InvoiceId);
return;
}

if (invoice.Status == InvoiceStatus.Pending)
{
logger.LogWarning(
"Payment reminder: Invoice {InvoiceId} for order {OrderId} is still pending. Amount: {Amount}",
message.InvoiceId,
message.OrderId,
message.Amount);
}
else
{
logger.LogInformation(
"Payment reminder: Invoice {InvoiceId} for order {OrderId} is already {Status}",
message.InvoiceId,
message.OrderId,
invoice.Status);
}
}
}
Loading
Loading