Empty batches stop without executing their AwaitBatch() actions

Using HangfirePro 2.0.1, I am unable to get a blank batch job to execute any of its AwaitBatch jobs:

public class HangfireTest
{
    public void DemoBatchWeirdness()
    {
        var emptyBatch = BatchJob.StartNew(action =>
        {
            //action.Enqueue(() => NOOP());
        });

        BatchJob.AwaitBatch(emptyBatch,
                            action =>
                            {
                                action.Enqueue<HangfireTest>(x => x.DemoBatchWeirdness2());
                            });
    }

    public void NOOP() { }

    public void DemoBatchWeirdness2()
    {
        Debugger.Break();
    }
}

If I uncomment action.Enqueue(() => NOOP());, the debugger will stop within DemoBatchWeirness2(), otherwise it won’t. Method 2 won’t even show up in the hangfire dashboard.

I am calling the first method by injecting in a IBackgroundJobClient into my controller and calling _client.Enqueue<HangfireTest>(x => x.DemoBatchWeirdness());

Am I doing something wrong here, or is this just a bug?

That’s because empty batch without any job will be finished in the Completed state. By default, batch continuations are fired only when final state is Succeeded, i.e. all background jobs were finished successfully. If you need to call continuation regardless of the final state, use the options argument:

BatchJob.AwaitBatch(emptyBatch, batch =>
{
    batch.Enqueue(() => Console.WriteLine("Continuation"));
}, options: BatchContinuationOptions.OnAnyFinishedState);