DeferredTaskManager 12.0.1

Additional Details

- The logic is divided into 5 separate modules, which are registered in DI with the possibility of redefinition.
- Fixed the work of the delegate for exception handling (hitting the delegate is performed after each exception occurs, and not only after exhausting all repeated attempts as it was before. The delegate itself is expanded with additional parameters.
- The main options are passed during DI registration, and delegates are passed to the start method.
- Renamed some options
- There is no need to wrap the starting method in a Task.Run, the run method returns a single task.

There is a newer version of this package available.
See the version list below for details.
dotnet add package DeferredTaskManager --version 12.0.1
                    
NuGet\Install-Package DeferredTaskManager -Version 12.0.1
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="DeferredTaskManager" Version="12.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="DeferredTaskManager" Version="12.0.1" />
                    
Directory.Packages.props
<PackageReference Include="DeferredTaskManager" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add DeferredTaskManager --version 12.0.1
                    
#r "nuget: DeferredTaskManager, 12.0.1"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package DeferredTaskManager@12.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=DeferredTaskManager&version=12.0.1
                    
Install as a Cake Addin
#tool nuget:?package=DeferredTaskManager&version=12.0.1
                    
Install as a Cake Tool

ru

Event-driven Deferred Task Manager C#

NuGet version (DeferredTaskManager)

The implementation allows you to use multiple background tasks (or "runners") for deferred processing of consolidated data. Runners are based on the PubSub template for asynchronous waiting for new tasks, which makes this approach more reactive but less resource-intensive.

Distinctive advantage

The solution allows data consolidation in the current instance with the possibility of variable deduplication or any other operations at the discretion of the developer, which can reduce resources during further transmission and processing, as well as increase performance.

Usage example

1️⃣ Injection of the Singleton dependency with the required data type:

services.AddSingleton<IDeferredTaskManagerService<object>, DeferredTaskManagerService<object>>();

2️⃣ Creating a Background Service with the necessary parameters:

internal sealed class EventManagerService : BackgroundService
{
    private readonly IDeferredTaskManagerService<object> _deferredTaskManager;

    public EventManagerService(IDeferredTaskManagerService<object> deferredTaskManager)
    {
        _deferredTaskManager = deferredTaskManager ?? throw new ArgumentNullException(nameof(deferredTaskManager));
    }

    protected override Task ExecuteAsync(CancellationToken cancellationToken)
    {
        Func<List<object>, CancellationToken, Task> taskDelegate = (events, cancellationToken) =>
        {
            return Task.Delay(1000000, cancellationToken);
        };

        Func<List<object>, CancellationToken, Task> taskDelegateRetryExhausted = async (events, cancellationToken) =>
        {
            Console.WriteLine("Something went wrong...");
        };

        var dtmOptions = new DeferredTaskManagerOptions<string>
        {
            TaskFactory = taskDelegate,
            PoolSize = 1,
            CollectionType = CollectionType.Queue,
            SendDelayOptions = new SendDelayOptions()
            {
                MillisecondsSendDelay = 60000,
                ConsiderDifference = true
            },
            RetryOptions = new RetryOptions<string>
            {
                RetryCount = 3,
                MillisecondsRetryDelay = 10000,
                TaskFactoryRetryExhausted = taskDelegateRetryExhausted
            }
        };

        return Task.Run(() => _deferredTaskManager.StartAsync(dtmOptions, cancellationToken), cancellationToken);
    }
}
TaskFactory — delegate for custom logic

All custom logic is placed in the delegate TaskFactory, which receives a collection of consolidated events. This is where you can perform the necessary operations on them before further transmission/processing. You can also handle exceptions in the delegate (this is important if events are handled separately) by sending unprocessed events to the next session after the time delay specified in the parameters MillisecondsRetryDelay.

try
{
    // Custom operation on received events
}
catch (Exception ex)
{
    events.RemoveRange(successEvents);

    // You can issue an exception after deleting successfully 
    // completed events or add your own conditions
    throw new Exception("Sending a second attempt after Exception");
}
PoolSize — pool size (number of available runners)

The pool size is variable and is selected by the developer for a specific range of tasks, focusing on the speed of execution and the amount of resources consumed.

CollectionType — collection type

You can also specify the collection type, «Bag» for the Unordered collection of objects (it works faster) or «Queue» for the Ordered collection of objects. It is advisable to use «Queue» only if poolSize = 1, otherwise the execution order is not guaranteed.

SendDelayOptions — setting up sending events at a time interval

Configures the sending of added events for processing after a certain period of time with the possibility of variable deduction of the time of the previous operation. It makes sense to specify when the AddWithoutSend method is used to add event, which adds events without sending for processing.

RetryOptions — configuring exception handling

You can also pass an error handling delegate that will trigger when the specified number of retries is exhausted.

3️⃣ Sending data to the Deferred Task Manager for subsequent execution:

_deferredTaskManager.Add(events);

Other usage examples

The DeferredTaskManager can be used as a regular event store, receiving events on demand using the GetEventsAndClearStorage method, bypassing runners, or sending available events to a delegate to any available runner on demand using the SendEvents method.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
15.0.0 35 7/14/2025
14.5.0 30 7/13/2025
14.4.0 28 7/13/2025
14.3.0 142 6/30/2025
14.2.0 136 6/26/2025
14.1.0 101 6/22/2025
14.0.0 163 4/29/2025
13.0.5 156 4/23/2025
13.0.4 152 4/23/2025
13.0.3 155 4/23/2025
13.0.2 192 4/14/2025
13.0.1 203 4/14/2025
13.0.0 170 4/10/2025
12.0.1 218 3/30/2025 12.0.1 is deprecated because it is no longer maintained and has critical bugs.
12.0.0 215 3/30/2025 12.0.0 is deprecated because it is no longer maintained and has critical bugs.
11.0.0 227 3/17/2025 11.0.0 is deprecated because it is no longer maintained and has critical bugs.
10.0.0 233 3/17/2025 10.0.0 is deprecated because it is no longer maintained and has critical bugs.
9.2.0 249 3/12/2025 9.2.0 is deprecated because it is no longer maintained and has critical bugs.
9.1.0 482 12/16/2024 9.1.0 is deprecated because it is no longer maintained and has critical bugs.
9.0.2 217 12/16/2024 9.0.2 is deprecated because it is no longer maintained and has critical bugs.
9.0.1 215 12/15/2024 9.0.1 is deprecated because it is no longer maintained and has critical bugs.
9.0.0 205 12/15/2024 9.0.0 is deprecated because it is no longer maintained and has critical bugs.
8.1.0 224 12/15/2024 8.1.0 is deprecated because it is no longer maintained and has critical bugs.
8.0.3 216 12/14/2024 8.0.3 is deprecated because it is no longer maintained and has critical bugs.
8.0.2 370 11/2/2024 8.0.2 is deprecated because it is no longer maintained and has critical bugs.
8.0.1 225 11/2/2024 8.0.1 is deprecated because it is no longer maintained and has critical bugs.
8.0.0 275 10/28/2024 8.0.0 is deprecated because it is no longer maintained and has critical bugs.
2.0.1 266 10/27/2024 2.0.1 is deprecated because it is no longer maintained and has critical bugs.
2.0.0 249 10/27/2024 2.0.0 is deprecated because it is no longer maintained and has critical bugs.