FaktoryWorker 0.2.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package FaktoryWorker --version 0.2.1
NuGet\Install-Package FaktoryWorker -Version 0.2.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="FaktoryWorker" Version="0.2.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add FaktoryWorker --version 0.2.1
#r "nuget: FaktoryWorker, 0.2.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.
// Install FaktoryWorker as a Cake Addin
#addin nuget:?package=FaktoryWorker&version=0.2.1

// Install FaktoryWorker as a Cake Tool
#tool nuget:?package=FaktoryWorker&version=0.2.1

Status

build-and-test publish-nuget

NuGet Release

FaktoryWorker

A simple .NET worker and client for Faktory Job Server. For more information & documentation about Faktory - go to Faktory Job Server.

Quick Start

dotnet add package FaktoryWorker

Client

Using the FaktoryClient you can connect to the Faktory Server and publish jobs like so:

await using var faktoryClient = new FaktoryClient("127.0.0.1", 7419, "testworker", 2, Guid.NewGuid().ToString());
await faktoryClient.ConnectAsync();
await faktoryClient.HelloAsync();

var job = new FaktoryClient.Job(Guid.NewGuid().ToString(),"default", "SendEmail", new []{"test"});
await faktoryClient.PushJobAsync(job);

Please note that ConnectAsync and HelloAsync need to be done in that order before other commands are executed. See FaktoryClientTests.cs for a full example.

Worker

In a HostBuilder, you can use the AddFaktoryWorker extension method to configure dependency injection for the worker. The worker will then start as a BackgroundService and poll for jobs every PollingFrequencySeconds until the app is stopped. When stopped, if there are any jobs in progress, the worker will wait for the specified ShutdownTimeoutSeconds until exiting.

services.AddFaktoryWorker(options =>
{
    options.Host = "localhost";
    options.Port = 7419;
    options.ParallelJobs = 25;
    options.PollingFrequencySeconds = 1;
    options.ShutdownTimeoutSeconds = 15;
    options.FaktoryVersion = 2;
    options.WorkerHostName = "BackgroundServiceExample";
    options.WorkerId = Guid.NewGuid().ToString();
});

Its a good idea to set the Hosts ShutdownTimeout to a value larger than the worker's ShutdownTimeoutSeconds.

services.Configure<HostOptions>(o => o.ShutdownTimeout = TimeSpan.FromSeconds(30));

Create consumers for each Faktory queue you need using the IJobConsumer interface.

public class ExampleAJobConsumer : IJobConsumer
{
    public async Task ConsumeJob(FaktoryClient.Job job, CancellationToken cancellationToken = default)
    {
        //Implement the logic here
    }
}

Then configure dependency injection for your consumers and name which queue they should consume from

services
    .AddJobConsumer<ExampleAJobConsumer>("default", services)
    .AddJobConsumer<ExampleBJobConsumer>("queue2", services)
    .Build();

See BackgroundServiceExample for a full runnable example.

Faktory API

The following have been implemented so far (enough to push jobs and setup a functional worker):
HI (connect socket)
HELLO (handshake/initialize the worker)
BEAT (heartbeat)
PUSH (push a job to server)
FETCH (fetch a job from server)
ACK (notify job was successfully processed)
FAIL (notify job failed)

For full client API support the following are needed:
FLUSH
END
PUSHB
QUEUE REMOVE
QUEUE PAUSE
QUEUE RESUME
INFO

TODO

  • Reconnecting to Faktory if the connection is lost.
  • Full API support.
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  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 is compatible.  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. 
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
0.2.3 1,057 7/23/2023
0.2.2 588 6/8/2023
0.2.1 123 6/7/2023
0.2.0 135 6/7/2023
0.1.0 119 6/5/2023