Skip to content

ahydrax/Hangfire.Atoms

Repository files navigation

Hangfire.Atoms

NuGet

Execute multiple jobs as a single atomic job.

READ THIS BEFORE UPGRADE

If you're coming from version <=0.3 you have to wait till your atomic jobs (in enqueued, scheduled, awaiting, etc. states) finishes as in version 0.4 data schema has been changed dramatically and this can lead to job state corruption.

This affects only existing users. New users have not to worry about anything.

Requirements

  • The storage you chosen must implement JobStorageConnection & JobStorageTransaction
  • NET Standard 2.0 compatible project

Setup

Install a package from Nuget. Then configure your server and dashboard like this:

services.AddHangfire(configuration =>
    {
        configuration.UseStorage(yourStorage);
        configuration.UseAtoms();
    });

Or this:

GlobalConfiguration.UseStorage(yourStorage);
GlobalConfiguration.UseAtoms();

You must setup Hangfire storage before calling UseAtoms();.

Usage

Additional extension methods are added for IBackgroundJobClient.

Atoms

var client = new BackgroundJobClient();

// Enqueue
var atomId = client.Enqueue("atom-1", builder =>
    {
        for (var i = 0; i < 50; i++)
        {
            builder.Enqueue(() => DoWork());
        }
    });

// Continuations
var job1 = client.Enqueue(() => DoPrepare());
var atomId = client.ContinueWith(job1, "atom-2", builder =>
    {
        for (var i = 0; i < 50; i++)
        {
            builder.Enqueue(() => DoWork());
        }
    });

// Scheduling
var atomId = client.Schedule("atom-3", TimeSpan.FromSeconds(3), builder =>
    {
        for (var i = 0; i < 50; i++)
        {
            builder.Enqueue(() => DoWork());
        }
    });

// Atoms can be used as a common job which means you can continue them
client.ContinueWith(atomId, () => Done());

Triggers

Triggers are event-like primitives. You can subscribe to it and set it manually whenever you need it.

// Triggers
var triggerJobId = client.OnTriggerSet("trigger-1");
client.ContinueWith(triggerJobId, () => DoWork());

// Set trigger manually when you need it
client.Schedule(() => SetTrigger("trigger-1"), TimeSpan.FromSeconds(10));

License

Authored by: Viktor Svyatokha (ahydrax)

This project is under MIT license. You can obtain the license copy here.

This work is based on the work of Sergey Odinokov, author of Hangfire. http://hangfire.io/