PiBox.Plugins.Jobs.Hangfire
0.9.7
See the version list below for details.
dotnet add package PiBox.Plugins.Jobs.Hangfire --version 0.9.7
NuGet\Install-Package PiBox.Plugins.Jobs.Hangfire -Version 0.9.7
<PackageReference Include="PiBox.Plugins.Jobs.Hangfire" Version="0.9.7" />
paket add PiBox.Plugins.Jobs.Hangfire --version 0.9.7
#r "nuget: PiBox.Plugins.Jobs.Hangfire, 0.9.7"
// Install PiBox.Plugins.Jobs.Hangfire as a Cake Addin #addin nuget:?package=PiBox.Plugins.Jobs.Hangfire&version=0.9.7 // Install PiBox.Plugins.Jobs.Hangfire as a Cake Tool #tool nuget:?package=PiBox.Plugins.Jobs.Hangfire&version=0.9.7
PiBox.Plugins.Jobs.Hangfire
PiBox.Plugins.Jobs.Hangfire is a plugin
that allows PiBox based services
to use the hangfire package to manage and schedule (cron) jobs
.
Installation
To install the nuget package follow these steps:
dotnet add package PiBox.Plugins.Jobs.Hangfire
or add as package reference to your .csproj
<PackageReference Include="PiBox.Plugins.Jobs.Hangfire" Version="" />
Appsettings.yml
Configure your appsettings.yml with these properties
For example
hangfire:
Host: localhost
Port: 5432
Database: postgres
User: postgres
Password: postgres
InMemory: true
DashboardUser: awesome-user #if you don't set this, you can't access the hangfire dashboard
DashboardPassword: awesome-pw #if you don't set this, you can't access the hangfire dashboard
HangfireConfiguration.cs
[Configuration("hangfire")]
public class HangfireConfiguration
{
public string? Host { get; set; }
public int Port { get; set; }
public string? Database { get; set; }
public string? User { get; set; }
public string? Password { get; set; }
public string? DashboardUser { get; set; }
public string? DashboardPassword { get; set; }
public bool InMemory { get; set; }
public int? PollingIntervalInMs { get; set; }
public int? WorkerCount { get; set; }
public string ConnectionString => $"Host={Host};Port={Port};Database={Database};Username={User};Password={Password};";
}
Configuration in your plugin host
public class SamplePluginHost : IPluginServiceConfiguration
{
public void ConfigureServices(IServiceCollection serviceCollection)
{
// here you can register your recurring jobs or just annotate them with the [RecurringJob] attribute
serviceCollection.ConfigureJobs((jobRegister, _) =>
{
jobRegister.RegisterParameterizedRecurringAsyncJob<HangfireParameterizedTestJob, int>(Cron.Daily(), 1); // every day at 0:00 UTC
jobRegister.RegisterRecurringAsyncJob<HangfireTestJob>(Cron.Hourly(15)); // every hour to minute 15 UTC
});
}
}
Usage
Please take a look at the official Hangfire documentation https://docs.hangfire.io/en/latest/ and best practises https://docs.hangfire.io/en/latest/best-practices.html
Every job does out of the box 10 retries (with increasing wait times) if not configured otherwise.
Auto registration of jobs
You can also define recurring jobs as simple classes and annotate them with the [ReccuringJob] attribute which then auto-registers the job for you at startup
[RecurringJob("0 0 * * *")]
public class HangfireTestJob : AsyncJob
{
public HangfireTestJob(ILogger<HangfireTestJob> logger) : base(logger)
{
}
protected override Task<object> ExecuteJobAsync(CancellationToken cancellationToken)
{
Logger.LogInformation("TEst");
return Task.FromResult<object>(cancellationToken);
}
}
Define jobs
Without parameters
public class HangfireTestJob : AsyncJob
{
public HangfireTestJob(ILogger<HangfireTestJob> logger) : base(logger)
{
}
protected override Task<object> ExecuteJobAsync(CancellationToken cancellationToken)
{
Logger.LogInformation("TEst");
return Task.FromResult<object>(cancellationToken);
}
}
With parameters
Job input parameters and results get serialized to json and are displayed in the hangfire dashboard, be aware of the possibility of leaking sensitive information!
public class MyResult
{
public int JobParameter {get;set;}
}
public class HangfireParameterizedTestJob : ParameterizedAsyncJob<int>
{
public HangfireParameterizedTestJob(ILogger<HangfireParameterizedTestJob> logger) : base(logger)
{
}
protected override Task<MyResult> ExecuteJobAsync(int value, CancellationToken cancellationToken)
{
var result = new MyResult(){ JobParameter = value};
Logger.LogInformation("Test job for {Value}", value);
return result; //
}
}
Fire and forget jobs or enqueue dynamically
You can create new jobs from anywhere in your service, see also https://docs.hangfire.io/en/latest/background-methods/index.html
BackgroundJob.Enqueue<IEmailSender>(x => x.Send("hangfire@example.com"));
BackgroundJob.Enqueue(() => Console.WriteLine("Hello, world!"));
Execution modes
If you want the job only to be executed as one instance at any given point in time use the
UniquePerQueueAttribute
this ensures that there is only one job of the same type/name in processing or queued at any given point!
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net7.0
- AspNetCore.HealthChecks.Hangfire (>= 7.0.0)
- HangFire (>= 1.8.5)
- Hangfire.MemoryStorage (>= 1.8.0)
- Hangfire.PostgreSql (>= 1.20.2)
- Microsoft.AspNetCore.Authentication.OpenIdConnect (>= 7.0.10)
- Newtonsoft.Json (>= 13.0.3)
- PiBox.Hosting.Abstractions (>= 0.9.7)
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.73 | 71 | 10/22/2024 |
1.0.66 | 153 | 10/15/2024 |
1.0.64 | 105 | 10/14/2024 |
1.0.61 | 107 | 10/1/2024 |
1.0.60 | 91 | 9/27/2024 |
1.0.54 | 221 | 4/30/2024 |
1.0.51 | 143 | 2/27/2024 |
1.0.49 | 100 | 2/27/2024 |
1.0.47 | 124 | 2/21/2024 |
1.0.45 | 129 | 2/20/2024 |
1.0.43 | 113 | 2/13/2024 |
1.0.41 | 113 | 2/13/2024 |
1.0.39 | 124 | 2/8/2024 |
1.0.38 | 110 | 2/8/2024 |
1.0.37 | 101 | 2/8/2024 |
1.0.35 | 103 | 2/2/2024 |
1.0.32 | 110 | 1/30/2024 |
1.0.25 | 173 | 12/27/2023 |
1.0.23 | 131 | 12/19/2023 |
1.0.22 | 103 | 12/19/2023 |
1.0.21 | 104 | 12/19/2023 |
1.0.19 | 127 | 12/11/2023 |
1.0.17 | 160 | 11/23/2023 |
1.0.7 | 112 | 11/23/2023 |
1.0.5 | 121 | 11/23/2023 |
1.0.3 | 130 | 11/23/2023 |
1.0.0 | 140 | 11/21/2023 |
0.9.9 | 130 | 11/21/2023 |
0.9.7 | 130 | 11/21/2023 |