Skip to content

Commit

Permalink
Merge pull request #27 from netcorepal/dev
Browse files Browse the repository at this point in the history
add backgroundservice with trace test case
  • Loading branch information
witskeeper authored Apr 18, 2024
2 parents b3d6f91 + 0f664ac commit 4291b81
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ namespace NetCorePal.Web.Application.Commands
/// </summary>
/// <param name="orderRepository"></param>
/// <param name="logger"></param>
public class CreateOrderCommandHandler(IOrderRepository orderRepository, ILogger<CreateOrderCommandHandler> logger) : ICommandHandler<CreateOrderCommand, OrderId>
public class CreateOrderCommandHandler(IOrderRepository orderRepository, ILogger<CreateOrderCommandHandler> logger)
: ICommandHandler<CreateOrderCommand, OrderId>
{
/// <summary>
///
Expand All @@ -19,10 +20,20 @@ public class CreateOrderCommandHandler(IOrderRepository orderRepository, ILogger
/// <returns></returns>
public async Task<OrderId> Handle(CreateOrderCommand request, CancellationToken cancellationToken)
{
var a = new List<long>();
for (int i = 0; i < 1000; i++)
{
a.Add(i);
}
await Parallel.ForEachAsync(a, new ParallelOptions(), async (item,c) =>
{
await Task.Delay(12, c);
});

var order = new Order(request.Name, request.Count);
order = await orderRepository.AddAsync(order, cancellationToken);
logger.LogInformation("order created, id:{orderId}", order.Id);
return order.Id;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,24 @@ public class CreateOrderCommandBackgroundService(IServiceProvider serviceProvide
{
protected override async Task ExecuteAsync(CancellationToken stoppingToken)

Check warning on line 7 in test/NetCorePal.Web/HostedServices/CreateOrderCommandBackgroundService.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'CreateOrderCommandBackgroundService.ExecuteAsync(CancellationToken)'

Check warning on line 7 in test/NetCorePal.Web/HostedServices/CreateOrderCommandBackgroundService.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'CreateOrderCommandBackgroundService.ExecuteAsync(CancellationToken)'

Check warning on line 7 in test/NetCorePal.Web/HostedServices/CreateOrderCommandBackgroundService.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'CreateOrderCommandBackgroundService.ExecuteAsync(CancellationToken)'

Check warning on line 7 in test/NetCorePal.Web/HostedServices/CreateOrderCommandBackgroundService.cs

View workflow job for this annotation

GitHub Actions / build

Missing XML comment for publicly visible type or member 'CreateOrderCommandBackgroundService.ExecuteAsync(CancellationToken)'
{
using var scope = serviceProvider.CreateScope();
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();
while (!stoppingToken.IsCancellationRequested)
{
using var scope = serviceProvider.CreateScope();
var mediator = scope.ServiceProvider.GetRequiredService<IMediator>();

var command = new CreateOrderCommand("abc", 10, 20);
await mediator.Send(command, stoppingToken);
var distributedLock = scope.ServiceProvider
.GetRequiredService<NetCorePal.Extensions.DistributedLocks.IDistributedLock>();
var handler = await distributedLock.TryAcquireAsync("CreateOrderCommandBackgroundService",
TimeSpan.FromSeconds(10), stoppingToken);
if (handler != null)
{
await using (handler)
{
var command = new CreateOrderCommand("abc", 10, 20);
await mediator.Send(command, stoppingToken);
await Task.Delay(100, stoppingToken);
}
}
}
}
}
2 changes: 2 additions & 0 deletions test/NetCorePal.Web/NetCorePal.Web.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
<ProjectReference Include="..\..\src\Context.AspNetCore\NetCorePal.Context.AspNetCore.csproj" />
<ProjectReference Include="..\..\src\Context.CAP\NetCorePal.Context.CAP.csproj" />
<ProjectReference Include="..\..\src\Context.Shared\NetCorePal.Context.Shared.csproj" />
<ProjectReference Include="..\..\src\DistributedLocks.Abstractions\NetCorePal.Extensions.DistributedLocks.Abstractions.csproj" />
<ProjectReference Include="..\..\src\DistributedLocks.Redis\NetCorePal.Extensions.DistributedLocks.Redis.csproj" />
<ProjectReference Include="..\..\src\DistributedTransactions.CAP.SourceGenerators\NetCorePal.Extensions.DistributedTransactions.CAP.SourceGenerators.csproj" OutputItemType="Analyzer" ReferenceOutputAssembly="false" />
<ProjectReference Include="..\..\src\DistributedTransactions.CAP.MySql\NetCorePal.Extensions.DistributedTransactions.CAP.MySql.csproj" />
<ProjectReference Include="..\..\src\DistributedTransactions.CAP\NetCorePal.Extensions.DistributedTransactions.CAP.csproj" />
Expand Down
2 changes: 1 addition & 1 deletion test/NetCorePal.Web/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@
#endregion

builder.Services.AddHostedService<CreateOrderCommandBackgroundService>();

builder.Services.AddRedisLocks();
var app = builder.Build();
app.UseContext();
//app.UseKnownExceptionHandler();
Expand Down

0 comments on commit 4291b81

Please sign in to comment.