Acta. Durable execution for .NET

learn actakeep another system in sync

How to check again later without spending a retry

RescheduleAsync ends the execution and runs the handler again after a delay, without counting as a failure or spending a retry.

A how-to for Acta, durable execution for .NET. New to Acta? Run your first durable job in five minutes.

Checked every release learn/300-failure-and-recovery/306-reschedule
Read as

1Reschedule when the answer is not there yet

RescheduleAsync re-arms the job after a delay without burning the retry budget; the handler stops here and replays on the next run (RescheduleUntilAsync takes an absolute time).

reschedule.csexcerpt
public sealed class PollResourceJob
{
    private static int _tries;

    [Job("poll-resource")]
    public async Task Handle(PollResource input, JobContext context, CancellationToken ct)
    {
        var ready = Interlocked.Increment(ref _tries) >= 2;
        if (!ready)
        {
            Console.WriteLine($"[{input.Url}] not ready, rescheduling...");
            await context.RescheduleAsync(TimeSpan.FromSeconds(1), "resource not ready", ct);
        }

        Console.WriteLine($"[{input.Url}] ready - processed");
    }
}

2Run it and check the output

From a clone of the Acta repository, with the .NET 10 SDK:

Terminal
dotnet run --project learn/300-failure-and-recovery/306-reschedule
Its output must contain
[api.example.com/report] not ready, rescheduling...
[api.example.com/report] ready - processed

Checked every release. Before each release, Acta Verify runs this program and checks that its output contains these lines.

This page shows the code. The rules it relies on, starting with the handler running again from its first line, are in the handler contract.

The whole program

Every file of learn/300-failure-and-recovery/306-reschedule, as it is compiled.

UseLocalDatabase is a helper of the Acta repository, not of the packages. In your own app, register the database with UseSqlite, UsePostgres or UseSqlServer, as in how to run your first job.

reschedule.cs53 lines
reschedule.cs
// How to check again later without spending a retry
// `RescheduleAsync` ends the execution and runs the handler again after a delay, without counting as a failure or
// spending a retry.
using Acta;
using Acta.Learn.Reschedule;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<RescheduleJobs>("reschedule");
});

using var host = builder.Build();
await host.StartAsync();

var jobs = host.Services.GetRequiredService<IJobs>();

await jobs.EnqueueAsync(new PollResource("api.example.com/report"));
Console.WriteLine("Enqueued. Worker is running - press Ctrl+C to stop.");

await host.WaitForShutdownAsync();
await host.StopAsync();

namespace Acta.Learn.Reschedule
{
    public sealed record PollResource(string Url);

    public sealed class PollResourceJob
    {
        private static int _tries;

        [Job("poll-resource")]
        public async Task Handle(PollResource input, JobContext context, CancellationToken ct)
        {
            var ready = Interlocked.Increment(ref _tries) >= 2;
            if (!ready)
            {
                // learn: Reschedule when the answer is not there yet
                // RescheduleAsync re-arms the job after a delay without burning the retry budget; the
                // handler stops here and replays on the next run (RescheduleUntilAsync takes an absolute time).
                Console.WriteLine($"[{input.Url}] not ready, rescheduling...");
                await context.RescheduleAsync(TimeSpan.FromSeconds(1), "resource not ready", ct);
                // learn: end
            }

            Console.WriteLine($"[{input.Url}] ready - processed");
        }
    }
}

Where to go next

New to Acta?

Acta is durable execution for .NET: workflows and jobs that survive restarts, as a library inside your application with its durable state in your SQL database.