Overview of libraries for Hangfire

Introduction

Hangfire is a distributed and extensible task scheduler for .NET applications.

In this article I made a short review of libraries that allow you to expand its capabilities. If you don’t know what Hangfire is, I advise you to read this article or take a look at official site.

Hangfire.Console

Hangfire.Console – a library that adds a console for managing tasks. This library allows you to display logs and also add a progress bar. Unfortunately, this library has not been updated since 2018, but this does not prevent it from being used as a basis for other libraries.

Hangfire.Console.Extensions

This library provides abstractions for working with Hangfire.Console. Running a task via IJobManager inside another task will allow them to be linked as a Continuation. Instead of using PerformContext for logging, you can write them in the usual ILogger. A progress bar can be created via IProgressBarFactory. IJobCancellationToken can now be registered via the constructor rather than being passed as a method parameter. PerformContext can be obtained via IPerformingContextAccessor. This way we will eliminate the need to pass PerformContext and IJobCancellationToken to the method, which will make the code cleaner and more readable.

Hangfire.Console.Extensions.Serilog

Hangfire.Console.Extensions.Serilog is a small library that allows you to connect Serilog with Hangfire. Now the logs will go to Hangfire.

"Serilog": {
  "Enrich": [
    "WithHangfireContext"
  ],
  "WriteTo": [
    {
      "Name": "Hangfire",
      "Args": {
        "restrictedToMinimumLevel": "Information"
      }
    }
  ]
}

Hangfire.PowerShellExecutor

Hangfire.PowerShellExecutor extends the capabilities of Hangfire.Console, allowing you to run PowerShell scripts and display them in the console.

public void WithoutInjection(PerformContext context, CancellationToken cancellationToken)
{
	var localProcess = new PSExecutorBuilder(context)
		.SetCommand("Get-ChildItem")
		.SetExecutionPolicy(PSExecutionPolicy.Bypass)
		.Build();
	localProcess.Start(cancellationToken);
}

Hangfire.Dashboard.Management.v2

Hangfire.Dashboard.Management.v2 is one of the many libraries for Hangfire that allows you to manage tasks through a graphical interface. The library’s functionality allows you to flexibly schedule the execution time of a task (instantly, after a certain period of time, or specify a CRON expression).

I liked this task management library the most, but we will look at another good option.

Hangfire.MissionControl

Hangfire.MissionControl is a simple library for launching Fire-and-Forget tasks; for some, its functionality will be more than enough, but compared to Hangfire.Dashboard.Management.v2 its capabilities are very limited.

FaceIT.Hangfire.Tags

This library allows you to tag tasks to make them easier to find later. I couldn’t find another library that provides a good task search, so if you have a huge number of different tasks and want to avoid getting lost in them, this library is indispensable.

Hangfire.HttpJob

A library for creating a task via an HTTP request can be very useful for integration with both external and internal systems.

url:http://{hangfireserver}/hangfire/httpjob?op=backgroundjob
method:post
data:
{
  "Method": "POST",
  "ContentType": "application/json",
  "Url": "http://XXXXXXX",
  "DelayFromMinutes": 1,
  "Data": "{\"userName\":\"test\"}",
  "Timeout": 5000,
  "BasicUserName": "",
  "BasicPassword": "",
  "JobName": "test_backgroundjob"
}

Hangfire.HttpJob also provides an HTTP Client for scheduling tasks from code.

var serverUrl = "http://localhost:5000/job";
var result = HangfireJobClient.AddBackgroundJob(serverUrl, new BackgroundJob
{
    JobName = "myapi",
    Method = "Get",
    Url = "http://localhost:5000/testaaa",
    Mail = new List<string> {"1877682825@qq.com"},
    SendSucMail = true,
    DelayFromMinutes = 1
}, new HangfireServerPostOption
{
    BasicUserName = "admin",
    BasicPassword = "test"
});

TransactHangfire

Have you ever had a situation where you needed to perform two tasks within one transaction? To be honest, I don’t either, but the TransactHangfire library allows you to do this. You just need to attach the UseTransactionScope attribute to the class and voila, now two tasks are combined by one TransactionScope.

OpenTelemetry.Instrumentation.Hangfire

I think everything is clear from the name, we collect telemetry data. In the task parameters, opentelemetry_activity_context is sewn in, through which we can access our telemetry.

Conclusion

In this short article, I highlighted the most interesting libraries, in my opinion, that expand the standard capabilities of Hangfire. Based on my experience, I can say that almost everywhere I had to use Hangfire, I added a bundle with the following libraries: Hangfire.Console, Hangfire.Console.Extensions, Hangfire.Dashboard.Management.v2, FaceIT.Hangfire.Tags, OpenTelemetry .Instrumentation.Hangfire.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *