CarbonAwareComputing.Hangfire 1.0.0

Suggested Alternatives

Hangfire.Community.CarbonAwareExecution

Additional Details

The package is renamed to be consistent with the Hangfire community extensions guidlines. Please move to Hangfire.Community.CarbonAwareExecution

dotnet add package CarbonAwareComputing.Hangfire --version 1.0.0                
NuGet\Install-Package CarbonAwareComputing.Hangfire -Version 1.0.0                
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="CarbonAwareComputing.Hangfire" Version="1.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add CarbonAwareComputing.Hangfire --version 1.0.0                
#r "nuget: CarbonAwareComputing.Hangfire, 1.0.0"                
#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 CarbonAwareComputing.Hangfire as a Cake Addin
#addin nuget:?package=CarbonAwareComputing.Hangfire&version=1.0.0

// Install CarbonAwareComputing.Hangfire as a Cake Tool
#tool nuget:?package=CarbonAwareComputing.Hangfire&version=1.0.0                

Carbon Aware Computing Hangfire Extension

Overview

A Hangfire extension to schedule tasks with carbon awareness in mind. The best point in time is calculated based on emission forecasts to get a window with a minimal grid carbon intensity.

Installation

CarbonAwareComputing.Hangfire is available as a NuGet package. You can install it using the NuGet Package Console window:

Install-Package CarbonAwareComputing.Hangfire

After installation add the extension to the Hangfire configuration.

builder.Services.AddHangfireCarbonAwareExecution(configuration => configuration
    .UseCarbonAwareExecution(new CarbonAwareDataProviderOpenData(), ComputingLocations.Germany)
);

Usage

There are extension to Enqueue and Schedule with WithCarbonAwarenessAsync.

Fire and Forget tasks

Setup the latest execution time and the estimated task duration. The extension will do a best effort to get a window with the estimated task duration and minimal grid carbon intensity. When no window can be detected, the task is enqueued immediately.

//use the extension methods
IBackgroundJobClient client = GetBackgroundJobClient();
await client.EnqueueWithCarbonAwarenessAsync(
    () => Console.WriteLine("Enqueue carbon aware jobs"),
    DateTimeOffset.Now + TimeSpan.FromHours(2),
    TimeSpan.FromMinutes(5));

//or use the static versions
await CarbonAwareBackgroundJob.EnqueueAsync(
    () => Console.WriteLine("Enqueue carbon aware jobs"),
    DateTimeOffset.Now + TimeSpan.FromHours(2),
    TimeSpan.FromMinutes(5));    

Delayed tasks

Setup the earliest and latest execution time and the estimated task duration. The extension will do a best effort to get a window with the estimated task duration and minimal grid carbon intensity. When no window can be detected, the task is scheduled as desired.

//use the extension methods
IBackgroundJobClient client = GetBackgroundJobClient();
await client.ScheduleWithCarbonAwarenessAsync(
    () => Console.WriteLine("Schedule carbon aware jobs"),
    DateTimeOffset.Now + TimeSpan.FromHours(2),
    TimeSpan.FromMinutes(20),
    TimeSpan.FromMinutes(30), TimeSpan.FromMinutes(5));

//or use the static versions
await CarbonAwareBackgroundJob.ScheduleAsync(
        () => Console.WriteLine("Schedule carbon aware jobs"),
        DateTimeOffset.Now + TimeSpan.FromHours(2),
        TimeSpan.FromMinutes(20),
        TimeSpan.FromMinutes(30), TimeSpan.FromMinutes(5));

Recurring tasks

Setup the maximum execution delay after planned schedule time and the estimated task duration. The extension will do a best effort to get a window with the estimated task duration and minimal grid carbon intensity. When no window can be detected, the task is scheduled as desired.

//use the extension methods
IRecurringJobManager manager = GetRecurringJobManager();
manager.AddOrUpdateCarbonAware(
    "daily", 
    () => Console.WriteLine("Hello, world!"), 
    Cron.Daily, 
    TimeSpan.FromHours(2),
    TimeSpan.FromMinutes(20));

//or use the static versions
CarbonAwareRecurringJob.AddOrUpdate(
    "daily", 
    () => Console.WriteLine("Hello, world!"), 
    Cron.Daily, TimeSpan.FromHours(2), 
    TimeSpan.FromMinutes(20));

The Hangfire Carbon Aware Extension will prevent the execution of the current instance of the recurring job. It is calculation a execution window with minimal carbon impact and the schedule that task. In the dashboard you will see the notice that the job was executed and a newly planned task.

Fallback

If your computing location is outside Europe or you need other forecasts the WattTime data provider may be useful. You need a valid WattTime account to use the data provider.

builder.Services.AddHangfireCarbonAwareExecution(configuration => configuration
    .UseCarbonAwareExecution(
        () => new CarbonAwareExecutionOptions(
            new CarbonAwareDataProviderWattTime(userName, password), 
            ComputingLocations.Germany))
        );

Extensibility

For custom forecasts or scenarios you don't want the build in provider add a own data provider. You may extend the abstract base class CarbonAwareDataProvider or use the CarbonAwareDataProviderWithCustomForecast.

Methodology

Hangfire.CarbonAwareExecution Extension make use of the Carbon Aware SDK a Green Software Foundation Project. There are some extensions to the SDK to use cached offline data sources in our fork.

The emission forecast data are uploaded periodically to a Azure Blob Storage for a given grid region and are public (e.g. for Germany https://carbonawarecomputing.blob.core.windows.net/forecasts/de.json).

For every grid region a data provider is needed:

  • Germany: The API from https://www.energy-charts.info/ are used. The data is the share of renewable energy to the total power production. The origin data source is from entso-e. The data is generated every day at 19:00+02 for the next day. After 19:00+02 the maximum forecast is next day 24:00+02.

Other data sources and regions are added soon.

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 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. 
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
1.0.0 241 4/29/2023 1.0.0 is deprecated because it is no longer maintained.