Jobs.Vector
1.0.0
See the version list below for details.
dotnet add package Jobs.Vector --version 1.0.0
NuGet\Install-Package Jobs.Vector -Version 1.0.0
<PackageReference Include="Jobs.Vector" Version="1.0.0" />
<PackageVersion Include="Jobs.Vector" Version="1.0.0" />
<PackageReference Include="Jobs.Vector" />
paket add Jobs.Vector --version 1.0.0
#r "nuget: Jobs.Vector, 1.0.0"
#:package Jobs.Vector@1.0.0
#addin nuget:?package=Jobs.Vector&version=1.0.0
#tool nuget:?package=Jobs.Vector&version=1.0.0
Jobs.Vector
A lightweight, portable .NET 8 background job queue and worker hosting service built on standard .NET primitives (System.Threading.Channels and BackgroundService).
It processes CPU-intensive and long-running operations asynchronously using bounded channels for backpressure, supports multi-threaded concurrent execution loops, and offers a thread-safe in-memory job status store with configurable time-to-live (TTL) eviction.
Technical Architecture
1. Bounded Backpressure via System.Threading.Channels
BackgroundJobQueue utilizes C# Channels configured in BoundedChannelFullMode.Wait mode. If the queue fills up to its maximum capacity, the enqueuing thread blocks and waits rather than letting the memory heap grow out of control.
2. Spanning Multi-Thread Worker Loops
On application startup, BackgroundJobWorker reads the configured worker count and spawns that number of independent Task execution loops. The workers poll the channel concurrently:
- A thread-safe dequeue removes the job item.
- The worker updates the status store to
Processing. - The delegate closure is executed inside a structured try/catch block.
- Once finished, the state changes to
Completed(orFailedwith the exception message).
3. Thread-Safe Status Store with TTL Pruning
InMemoryJobStatusStore uses a ConcurrentDictionary to cache job outcomes. To prevent memory leaks, jobs have an associated TTL retention window:
- Lazy Eviction: Calling
GetStatus(jobId)checks the timestamp; if the job has expired, it is removed immediately. - Active Cleanup:
JobStatusSweepWorkerruns a background task that sweeps the dictionary and removes expired entries every minute.
How to Use
1. Registering the Background Services
Bind configuration options and register workers:
using Jobs.Vector;
// Registers IBackgroundJobQueue, IJobStatusStore, and starts the BackgroundJobWorker & JobStatusSweepWorker
builder.Services.AddBackgroundJobs(builder.Configuration);
Configure via appsettings.json:
{
"Jobs": {
"Workers": 2,
"QueueCapacity": 100,
"StatusRetention": "00:30:00"
}
}
2. Enqueuing a Job
Inject IBackgroundJobQueue and enqueue an asynchronous task closure:
using Jobs.Vector;
public class IngestionController
{
private readonly IBackgroundJobQueue _jobQueue;
private readonly IJobStatusStore _statusStore;
public IngestionController(IBackgroundJobQueue jobQueue, IJobStatusStore statusStore)
{
_jobQueue = jobQueue;
_statusStore = statusStore;
}
public async Task StartIngestion(string fileId, CancellationToken ct)
{
string jobId = Guid.NewGuid().ToString();
// Enqueue job delegate containing processing task context
await _jobQueue.EnqueueAsync(async (jobCt) =>
{
// Simulated work logic
await Task.Delay(5000, jobCt);
_statusStore.SetImportResult(jobId, peopleImported: 42, eventsImported: 99);
}, jobId, ct);
}
}
3. Polling Job Status
Query the status store using the unique JobId:
using Jobs.Vector;
public JobStatusSnapshot? GetJobStatus(string jobId)
{
return _statusStore.GetStatus(jobId);
}
License
Distributed under the PolyForm Noncommercial License 1.0.0. See LICENSE for details.
| 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
- Microsoft.Extensions.Hosting.Abstractions (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.0)
- Microsoft.Extensions.Options.ConfigurationExtensions (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.