Remove FetchProducts activity and model

The FetchProducts.cs and Product.cs files have been removed as they are no longer needed. Additionally, changes in the OrderBatchProcessor.cs and FetchOrders.cs reflect updates to order fetching logic to accommodate these removals.
This commit is contained in:
Sipke Schoorstra 2024-03-13 12:07:39 +01:00
parent ee89b30e13
commit cad90e4e4e
5 changed files with 32 additions and 76 deletions

View file

@ -7,6 +7,9 @@ using Elsa.Workflows.Models;
namespace Elsa.Samples.AspNet.BatchProcessing.Activities;
/// <summary>
/// Fetches orders from the data source.
/// </summary>
[Activity("Demo", "Warehousing", "Fetch orders from the data source.")]
[Output(IsSerializable = false)]
public class FetchOrders : CodeActivity<IAsyncEnumerable<ICollection<Order>>>
@ -14,24 +17,31 @@ public class FetchOrders : CodeActivity<IAsyncEnumerable<ICollection<Order>>>
/// <summary>
/// The total number of orders to fetch.
/// </summary>
[Input(Description = "The total number of orders to fetch.")]
public Input<int> Count { get; set; } = new(100);
[Input(
Description = "The total number of orders to fetch.",
DefaultValue = 1000
)]
public Input<int> Count { get; set; } = new(1000);
/// <summary>
/// The number of orders to fetch per batch.
/// </summary>
[Input(Description = "The number of orders to fetch per batch.")]
[Input(
Description = "The number of orders to fetch per batch.",
DefaultValue = 100
)]
public Input<int> BatchSize { get; set; } = new(100);
/// <inheritdoc />
protected override void Execute(ActivityExecutionContext context)
{
var count = Count.Get(context);
var batchSize = BatchSize.Get(context);
var orders = GenerateOrders(count).Chunk(batchSize).ToAsyncEnumerable();
Result.Set(context, orders);
}
private IEnumerable<Order> GenerateOrders(int count)
{
var orderFaker = new Faker<Order>()

View file

@ -1,59 +0,0 @@
using Bogus;
using Elsa.Extensions;
using Elsa.Samples.AspNet.BatchProcessing.Models;
using Elsa.Workflows;
using Elsa.Workflows.Attributes;
using Elsa.Workflows.Models;
using Elsa.Workflows.Options;
using Elsa.Workflows.Signals;
namespace Elsa.Samples.AspNet.BatchProcessing.Activities;
[Activity("Demo", "Warehousing", "Fetch products from the data source.")]
[Output(IsSerializable = false)]
public class FetchProducts : CodeActivity<ICollection<Product>>
{
private const string CurrentBathKey = nameof(CurrentBathKey);
/// <summary>
/// The total number of products to fetch.
/// </summary>
[Input(Description = "The total number of products to fetch.")]
public Input<int> Count { get; set; } = new(100);
/// <summary>
/// The number of products to fetch per batch.
/// </summary>
[Input(Description = "The number of products to fetch per batch.")]
public Input<int> BatchSize { get; set; } = new(100);
protected override async ValueTask ExecuteAsync(ActivityExecutionContext context)
{
var count = Count.Get(context);
var batchSize = BatchSize.Get(context);
var currentBatch = context.ActivityInput.TryGetValue(CurrentBathKey, out var currentBatchValue) ? (int)currentBatchValue : 0;
var orders = GenerateProducts(count).Skip(currentBatch * batchSize).Take(batchSize).ToList();
Result.Set(context, orders);
await context.CompleteActivityAsync();
if (orders.Any())
{
currentBatch++;
context.SetProperty(CurrentBathKey, currentBatch);
// Schedule the next batch.
await context.SendSignalAsync(new ScheduleChildActivity(this, new Dictionary<string, object> { [CurrentBathKey] = currentBatch }));
}
}
private IEnumerable<Product> GenerateProducts(int count)
{
var productFaker = new Faker<Product>()
.RuleFor(o => o.Id, f => Guid.NewGuid().ToString())
.RuleFor(o => o.Name, f => Guid.NewGuid().ToString())
.RuleFor(o => o.Price, f => f.Random.Decimal(0.01m, 1000.00m));
return productFaker.Generate(count);
}
}

View file

@ -1,8 +0,0 @@
namespace Elsa.Samples.AspNet.BatchProcessing.Models;
public class Product
{
public string Id { get; set; } = default!;
public string Name { get; set; } = default!;
public decimal Price { get; set; }
}

View file

@ -1,6 +1,7 @@
using Elsa.EntityFrameworkCore.Modules.Management;
using Elsa.EntityFrameworkCore.Modules.Runtime;
using Elsa.Extensions;
using Elsa.Samples.AspNet.BatchProcessing.Models;
var builder = WebApplication.CreateBuilder(args);
@ -28,12 +29,17 @@ builder.Services.AddElsa(elsa =>
elsa.UseDefaultAuthentication(auth => auth.UseAdminApiKey());
elsa.AddActivitiesFrom<Program>();
elsa.AddWorkflowsFrom<Program>();
elsa.AddVariableTypeAndAlias<Order>("Order", "Warehousing");
elsa.AddVariableTypeAndAlias<IAsyncEnumerable<ICollection<Order>>>("BatchedOrderStream", "Warehousing");
});
builder.Services.AddHealthChecks();
builder.Services.AddCors(cors => cors.AddDefaultPolicy(policy => policy.AllowAnyOrigin().AllowAnyHeader().AllowAnyMethod().WithExposedHeaders("*")));
var app = builder.Build();
app.UseHttpsRedirection();
app.UseHealthChecks("/");
app.UseCors();
app.UseAuthentication();
app.UseAuthorization();

View file

@ -6,17 +6,24 @@ using Elsa.Workflows.Contracts;
namespace Elsa.Samples.AspNet.BatchProcessing.Workflows;
/// <summary>
/// A workflow that processes orders in batches.
/// </summary>
public class OrderBatchProcessor : WorkflowBase
{
/// <inheritdoc />
protected override void Build(IWorkflowBuilder builder)
{
var orders = builder.WithVariable<IAsyncEnumerable<Order>>();
var orders = builder.WithVariable<IAsyncEnumerable<ICollection<Order>>>();
builder.Root = new Sequence
{
Activities =
{
new WriteLine("Fetching orders..."),
new FetchOrders(),
new FetchOrders
{
Result = new(orders)
},
new ParallelForEach<Order>
{
Items = new(orders)