WebApiSkeleton.Tasks
1.1.0
See the version list below for details.
dotnet add package WebApiSkeleton.Tasks --version 1.1.0
NuGet\Install-Package WebApiSkeleton.Tasks -Version 1.1.0
<PackageReference Include="WebApiSkeleton.Tasks" Version="1.1.0" />
<PackageVersion Include="WebApiSkeleton.Tasks" Version="1.1.0" />
<PackageReference Include="WebApiSkeleton.Tasks" />
paket add WebApiSkeleton.Tasks --version 1.1.0
#r "nuget: WebApiSkeleton.Tasks, 1.1.0"
#:package WebApiSkeleton.Tasks@1.1.0
#addin nuget:?package=WebApiSkeleton.Tasks&version=1.1.0
#tool nuget:?package=WebApiSkeleton.Tasks&version=1.1.0
WebApiSkeleton.Tasks
WebApiSkeleton.Tasks is a NuGet package that contains an implementation of distributed task queue persisted in Redis
database. InMemory bus or RabbitMQ can be used as
a transport
Usage
Defining tasks and handlers
The task if defined by a class that is inherited
from BaseBackgroundTask (
or PeriodicTask). AdditionalExecuteAttemptsLeft property, that
defines the number of attempts to complete a task after a failure, can be overriden to set the needed attempt count.
A handler for task must implement ITaskHandler interface.
It can inject any dependencies needed from DI container as it is registered and resolved as transient.
Modifying task state
Task state is modified by calling OnStateChanged action on the instance of BaseBackgroundTask. The argument of the
action is TaskState object that contains following fields:
TaskId- identifier of a task that current state is referring to. Initialized from a constructor;StateId- identifier of a state fromTaskStatesenum represented as integer;PercentComplete- percent of task completion to display the progress of a task. Cannot be greater then 100;Comment- optional description of provided state;IsFinal- iftruethen the state of provided task is not updated anymore.
After calling OnStateChanged action the TaskState object is sent to all servers using message broker (in
RabbitMQ fanout exchange is used). The received TaskState message is passed
to ITaskIntegrityService implementation.
Queue interaction
Queue interaction is done by
using IClientBackgroundTaskQueue:
EnqueueTaskmethod adds the new task to distributed queue to be handled by any server;CancelTasksends theCancellationRequestto all servers, meaning that the task is canceled by user and should not be handled anywhere. If task is already being handled on any server then it will run until handler callstask.CancellationToken.ThrowIfCancellationRequestedmethod. If it doesn't then the task will be run until success or failure.
Queue can be also accessed by
using ITaskQueueStorage methods to get tasks from
Redis.
Registering the dependencies
Required dependencies are added to DI container by
using AddTaskQueue method on IServiceCollection. All
configuration is done by modifying TaskQueueConfiguration
object.
The following settings are required and will throw if not specified or configured with errors:
RedisSettings- contains the address of Redis database and a database number to use;ConsumerSettings- contains the unique server name that is used in message broker to identify available server correctly. Also containsJsonSerializerSettingsthat are added to DI and must haveTypeNameHandlingset toAll;TaskIntergrityImplementationType- the type of a service that implementsITaskIntegrityServicethat handles status changes of tasks in queue;BrokerImplementation- cannot be set directly, but one of methods that defines it must be called:UseRabbitMQ,UseInMemoryBroker;
Settings that have default values that can be overriden if needed:
TaskStorageSettings- contains names of task collections in Redis;TaskSemaphoreSettings- contains the name of semaphore, that is used to process tasks on this server, and maximum request (task) count for it. The name is recommended to be unique, otherwise multiple servers will use the same semaphore and maximum request count will be applied to multiple server instances simultaneously.
Tasks and handlers must also be added to DI container by using AddTasksFromAssembly method. Added assemblies will be
scanned for tasks and handlers. All handlers are added to DI as Transient.
NOTE:
you cannot use AddTaskQueue method more that 1 time as it will throw an exception.
Example
Example projects can be found in directory with the same name.
WebApiSkeleton.Tasks.Example.TasksLibrarydefines task classes that are used across all servers;WebApiSkeleton.Tasks.Example.ConsumerServeris a Web API project that is used as one of servers to handle tasks distributed way. Can be run in multiple instances (server and semaphore names are generated randomly on every startup);WebApiSkeleton.Tasks.Example.BlazorClientis a client with UI that also acts as a handle server. UI displays the state of all tasks across queue.
Versioning
All projects are versioned using following format: major.minor.patch. Versioning rules for all projects:
patchneeds to be incremented when any minor change is made to the project, such as bugfixes or small project-specific features addedminorneeds to be incremented when new template-wide feature is implemented. In this case all of the projects must have the same version setmajorneeds to be incremented when theWebApiSkeletontemplate has experienced significant changes, that need to upgrade all of the template packages. In this case all of the projects must have the same version set
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. 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. |
-
net8.0
- MassTransit (>= 8.1.3)
- MassTransit.Newtonsoft (>= 8.1.3)
- MassTransit.RabbitMQ (>= 8.1.3)
- Quartz (>= 3.8.0)
- Quartz.Extensions.DependencyInjection (>= 3.8.0)
- Quartz.Extensions.Hosting (>= 3.8.0)
- WebApiSkeleton.DistributedLockUtilities (>= 1.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.