♻️ Use jetstream to handle events broadcast
This commit is contained in:
@@ -22,6 +22,8 @@
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Minio" Version="6.0.5" />
|
||||
<PackageReference Include="NATS.Client.Core" Version="2.6.8" />
|
||||
<PackageReference Include="NATS.Client.JetStream" Version="2.6.8" />
|
||||
<PackageReference Include="Nerdbank.GitVersioning" Version="3.7.115">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
|
@@ -3,6 +3,8 @@ using DysonNetwork.Drive.Storage;
|
||||
using DysonNetwork.Shared.Stream;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NATS.Client.Core;
|
||||
using NATS.Client.JetStream;
|
||||
using NATS.Client.JetStream.Models;
|
||||
|
||||
namespace DysonNetwork.Drive.Startup;
|
||||
|
||||
@@ -14,14 +16,23 @@ public class BroadcastEventHandler(
|
||||
{
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
await foreach (var msg in nats.SubscribeAsync<byte[]>(AccountDeletedEvent.Type, cancellationToken: stoppingToken))
|
||||
var js = new NatsJSContext(nats);
|
||||
var stream = await js.GetStreamAsync(AccountDeletedEvent.Type, cancellationToken: stoppingToken);
|
||||
var consumer = await stream.CreateOrUpdateConsumerAsync(new ConsumerConfig("DysonNetwork_Drive_AccountDeleted"), cancellationToken: stoppingToken);
|
||||
|
||||
await foreach (var msg in consumer.ConsumeAsync<byte[]>(cancellationToken: stoppingToken))
|
||||
{
|
||||
AccountDeletedEvent? evt = null;
|
||||
try
|
||||
{
|
||||
var evt = JsonSerializer.Deserialize<AccountDeletedEvent>(msg.Data);
|
||||
if (evt == null) continue;
|
||||
evt = JsonSerializer.Deserialize<AccountDeletedEvent>(msg.Data);
|
||||
if (evt == null)
|
||||
{
|
||||
await msg.AckAsync(cancellationToken: stoppingToken);
|
||||
continue;
|
||||
}
|
||||
|
||||
logger.LogInformation("Account deleted: {AccountId}", evt.AccountId);
|
||||
logger.LogInformation("Processing account deletion for: {AccountId}", evt.AccountId);
|
||||
|
||||
using var scope = serviceProvider.CreateScope();
|
||||
var fs = scope.ServiceProvider.GetRequiredService<FileService>();
|
||||
@@ -34,23 +45,31 @@ public class BroadcastEventHandler(
|
||||
.Where(p => p.AccountId == evt.AccountId)
|
||||
.ToListAsync(cancellationToken: stoppingToken);
|
||||
|
||||
await fs.DeleteFileDataBatchAsync(files);
|
||||
await db.Files
|
||||
.Where(p => p.AccountId == evt.AccountId)
|
||||
.ExecuteDeleteAsync(cancellationToken: stoppingToken);
|
||||
if (files.Any())
|
||||
{
|
||||
await fs.DeleteFileDataBatchAsync(files);
|
||||
await db.Files
|
||||
.Where(p => p.AccountId == evt.AccountId)
|
||||
.ExecuteDeleteAsync(cancellationToken: stoppingToken);
|
||||
}
|
||||
|
||||
await transaction.CommitAsync(cancellationToken: stoppingToken);
|
||||
|
||||
await msg.AckAsync(cancellationToken: stoppingToken);
|
||||
logger.LogInformation("Account deletion for {AccountId} processed successfully.", evt.AccountId);
|
||||
}
|
||||
catch (Exception)
|
||||
catch (Exception ex)
|
||||
{
|
||||
await transaction.RollbackAsync(cancellationToken: stoppingToken);
|
||||
throw;
|
||||
logger.LogError(ex, "Error during transaction for account deletion {AccountId}, rolling back.", evt.AccountId);
|
||||
await transaction.RollbackAsync(CancellationToken.None);
|
||||
throw; // Let outer catch handle Nak
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
logger.LogError(ex, "Error processing AccountDeleted");
|
||||
logger.LogError(ex, "Failed to process account deletion for {AccountId}, will retry.", evt?.AccountId);
|
||||
await msg.NakAsync(delay: TimeSpan.FromSeconds(30), cancellationToken: stoppingToken);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user