BlendInteractive.ActionQueue
1.0.1
Prefix Reserved
dotnet add package BlendInteractive.ActionQueue --version 1.0.1
NuGet\Install-Package BlendInteractive.ActionQueue -Version 1.0.1
<PackageReference Include="BlendInteractive.ActionQueue" Version="1.0.1" />
paket add BlendInteractive.ActionQueue --version 1.0.1
#r "nuget: BlendInteractive.ActionQueue, 1.0.1"
// Install BlendInteractive.ActionQueue as a Cake Addin #addin nuget:?package=BlendInteractive.ActionQueue&version=1.0.1 // Install BlendInteractive.ActionQueue as a Cake Tool #tool nuget:?package=BlendInteractive.ActionQueue&version=1.0.1
Blend.ActionQueue
This is a simple, non-durable queue for when you need something like a message queue, but with minimal setup, for small, low-volume messages or actions where persistence is not a requirement.
For example, you might use this to invalidate caches across servers without holding up the main UI thread while API calls are executed. Or you might send non-critical notifications via this queue, again freeing up the primary UI thread.
Usage
To create a queue, implement AbstractActionQueue<T>
, where T
is the type of message you'll be queuing. Note: each instance of your queue will be a separate queue and thread, so you may want to ensure your queue is a singleton.
Example Implementation
/// <summary>
/// This is an example message type where the `Action` to execute is provided in the constructor.
/// </summary>
public class ExampleAction
{
private readonly Action action;
public ExampleAction(Action action)
{
this.action = action;
}
public void Execute() => action?.Invoke();
}
/// <summary>
/// This is an example queue which accepts `ExampleAction` as its "message", and executes the Action.
/// To log errors, you would set `OnError` to some kind of error handler `Action`.
/// </summary>
public class ExampleQueue : AbstractActionQueue<ExampleAction>
{
public ExampleQueue(System.Threading.CancellationToken token) : base(token)
{
}
public Action<Exception> OnError { get; set; }
protected override void LogException(Exception ex) => OnError?.Invoke(ex);
protected override void ProcessItem(ExampleAction item) => item.Execute();
}
Example Usage
var exampleQueue = new ExampleQueue(CancellationToken.None);
int totalExecutions = 0;
exampleQueue.QueueAction(new ExampleAction(() => totalExecutions += 2));
exampleQueue.QueueAction(new ExampleAction(() => totalExecutions += 4));
exampleQueue.QueueAction(new ExampleAction(() => totalExecutions += 8));
// Hopefully adding 3 numbers doesn't take longer than 100ms
Thread.Sleep(100);
Assert.Equal(14, totalExecutions);
Handling Errors
Because ProcessItem
is being called on a background thread, any errors thrown in the ProcessItem
will be caught and LogException
will be called with the exception to pass it through to whatever logging you're using.
Caveats
The queue is backed by a BlockingCollection<T>
and items are popped off and executed one at a time. In theory, the queue itself should not have any race conditions or other threading issues, but... mutlithreading is hard.
Keep in mind if using this in an ASP.NET context, you will not be able to rely on things like HttpContext.Current
, as this is running in a separate thread.
Each instance of a queue is a separate thread and queue. You'll most likely want each queue type to be a singleton. For example:
// WRONG
new ExampleQueue(CancellationToken.None).QueueAction(new ExampleAction(() => Console.WriteLine("No.")));
new ExampleQueue(CancellationToken.None).QueueAction(new ExampleAction(() => Console.WriteLine("Don't do this.")));
new ExampleQueue(CancellationToken.None).QueueAction(new ExampleAction(() => Console.WriteLine("It's wrong.")));
// OK
private static readonly ExampleQueue queue = new new ExampleQueue(CancellationToken.None);
queue.QueueAction(new ExampleAction(() => Console.WriteLine("OK.")));
queue.QueueAction(new ExampleAction(() => Console.WriteLine("Do this.")));
queue.QueueAction(new ExampleAction(() => Console.WriteLine("It's fine.")));
This queue is not durable. If your application shuts down or restarts with items pending in the queue, those items will be lost.
Product | Versions 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. |
.NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
.NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
.NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
MonoAndroid | monoandroid was computed. |
MonoMac | monomac was computed. |
MonoTouch | monotouch was computed. |
Tizen | tizen40 was computed. tizen60 was computed. |
Xamarin.iOS | xamarinios was computed. |
Xamarin.Mac | xamarinmac was computed. |
Xamarin.TVOS | xamarintvos was computed. |
Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- No dependencies.
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 |
---|---|---|
1.0.1 | 491 | 9/24/2022 |
Initial Release