The one-stop generic task batching and management library
The one-stop generic task batching and management library
The one-stop generic task batching and management library.
Contributions are welcome to add features, flexibility, performance test coverage...
…// Set isEnabled to configure the state of the batchPoolContainer at initialization
BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: false);
Task aTask = new Task(() => Console.WriteLine("Hello"));
BatchPoolTask task = batchPoolContainer.Add(aTask);
await task.WaitForTaskAsync();
// Resume and forget
batchPoolContainer.ResumeAndForget();
// Or resume and wait for all task to finish
await batchPoolContainer.ResumeAndWaitForAllAsync();
// Then pause again to prevent new pending tasks to run
batchPoolContainer.Pause();BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: false);
// Increase or reduce the capacity and wait for it to finish updating. (The batchPoolContainer will need to wait if a reduction is requested while it is currently processing)
await batchPoolContainer.UpdateCapacityAsync(10);
// Perform the same operation in the background
batchPoolContainer.UpdateCapacityAndForget(10);BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: true);
Task aTask = new Task(() => Console.WriteLine("Hello"));
// The callback will run as soon as the main task completes
Task aCallbackTask = new Task(() => Console.WriteLine("Hello"));
BatchPoolTask task = batchPoolContainer.Add(aTask, aCallbackTask);
await task.WaitForTaskAsync();BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: true);
Task aTask = new Task(() => Console.WriteLine("Hello"));
BatchPoolTask task = batchPoolContainer.Add(aTask);
bool isCanceled = task.IsCanceled;
bool isCompleted = task.IsCompleted;BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: true);
Task aTask = new Task(() => Console.WriteLine("Hello"));
BatchPoolTask task = batchPoolContainer.Add(aTask);
// Attempt to cancel
bool didCancel = task.Cancel();
// Attempt to cancel all pending tasks (pending = tasks that have not yet started processing due to the batch size, or the paused state of the BatchPool)
batchPoolContainer.RemoveAndCancelPendingTasks();CancellationTokenSource cancellationTokenSource = new CancellationTokenSource();
BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: true, cancellationToken: cancellationTokenSource.Token);
// All pending tasks will be Canceled
cancellationTokenSource.Cancel();BatchPoolContainer batchPoolContainer = new BatchPoolContainer(batchSize: 5, isEnabled: true);
Task aTask1 = new Task(() => Console.WriteLine("Hello"));
Task aTask2 = new Task(() => Console.WriteLine("Hello"));
List listOfTasks = new List() { aTask1, aTask2 };
ICollection tasks = batchPoolContainer.Add(listOfTasks);int order example:
…string order example:
BatchPoolDynamicContainer batchPool = new BatchPoolDynamicContainer(batchSize, isEnabled: false);
var task1 = new Task(() => Console.WriteLine("Hello #1"));
var batchTask1 = batchPool.Add(task1, priority: "a");
var task2 = new Task(() => Console.WriteLine("Hello #2"));
var batchTask2 = batchPool.Add(task2, "b");
var task3 = new Task(() => Console.WriteLine("Hello #3"));
var batchTask3 = batchPool.Add(task3, "c");
await batchPool.ResumeAndWaitForAllAsync();enum order example:
…Custom ordering simply by passing a new IComparer:
// Reverse the default order
public class StringReverseComparer : IComparer
{
public int Compare(string? x, string? y)
{
return y!.CompareTo(x!);
}
}
BatchPoolDynamicContainer batchPool = new BatchPoolDynamicContainer(batchSize, new StringReverseComparer(), isEnabled: false);…Custom ordering simply by passing a new IComparer:
// Reverse the default order
public class StringReverseComparer : IComparer
{
public int Compare(string? x, string? y)
{
return y!.CompareTo(x!);
}
}
BatchPoolUpdatableDynamicContainer batchPool = new BatchPoolUpdatableDynamicContainer(batchSize, new StringReverseComparer(), isEnabled: false);Technical Note: More checks are required for BatchPoolUpdatableDynamicContainer over the BatchPoolDynamicContainer. However, the performance impact should not be noticeable as they occur at O(1). UpdatePriority does not modify the existing data structure, but instead allows the container to check if a priority is current and valid or a new has been added.
The BatchPoolFactory simply provides a quick way to instantiate a new BatchPoolContainer:
// Default queue
BatchPoolContainer batchPool = BatchPoolFactory.GetQueueBatchPool(batchSize, isEnabled: false);
// Dynamic ordering
BatchPoolDynamicContainer batchPool = BatchPoolFactory.GetDynamicallyOrderedBatchPool(batchSize, isEnabled: false);
// Dynamic ordering and update existing tasks priority
BatchPoolUpdatableDynamicContainer batchPool = BatchPoolFactory.GetUpdatableDynamicallyOrderedBatchPool(batchSize, isEnabled: false);……IHost hostBuilder = Host.CreateDefaultBuilder()
.ConfigureServices((_, services) => services.AddSingleton>())
.Build();
using IServiceScope serviceScope = hostBuilder.Services.CreateScope();
IServiceProvider serviceProvider = serviceScope.ServiceProvider;
BatchPoolContainerManager batchPoolContainerManager = serviceProvider.GetRequiredService>();No open issues yet, or sync has not completed.