Acta. Durable execution for .NET

learn actarun work on a schedule

How to start each firing of a recurring job clean

A permanent job keeps its variables and step results from one firing to the next. ResetStateAsync as the firing's last line clears them, so the next one starts blank.

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

Checked every release learn/200-durable-execution/214-reset-state
Read as

1Clear the job's durable state as the last act of the firing

Clear the firing's durable state as its last line, so the next firing starts blank. Never at the top: a reset there also runs on every retry and replay of the same firing and erases its progress.

reset-state.csexcerpt
await context.ResetStateAsync(ct);

2Run it and check the output

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

Terminal
dotnet run --project learn/200-durable-execution/214-reset-state
Its output must contain
cycle start - 0 readings carried over (0 means last cycle reset)
cycle done  - 3 readings, avg

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/200-durable-execution/214-reset-state, 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.

reset-state.cs53 lines
reset-state.cs
// How to start each firing of a recurring job clean
// A permanent job keeps its variables and step results from one firing to the next. `ResetStateAsync` as the
// firing's last line clears them, so the next one starts blank.
using Acta;
using Acta.Learn.ResetState;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<ResetStateJobs>("reset-state");
});

// The [Schedule] recurs every 5s, so there's nothing to enqueue; just run the worker.
await builder.Build().RunAsync();

namespace Acta.Learn.ResetState
{
    public readonly record struct CollectMetrics;

    public sealed class MetricsMonitor
    {
        // Each fire re-runs the same durable job, so variables persist across fires unless the handler
        // clears its durable state as the final act of the cycle.
        [Job("collect-metrics", EventRecording = EventRecordingCode.Failures)]
        [Schedule("every-5-seconds", "PT5S")]
        public async Task Handle(CollectMetrics input, JobContext context, CancellationToken ct)
        {
            // Reads 0 because the previous cycle's reset wiped this variable; without it, reads 3.
            var carried = await context.GetVariableOrDefaultAsync("reading-count", 0, ct);
            Console.WriteLine($"{DateTime.UtcNow:HH:mm:ss} UTC cycle start - {carried} readings carried over (0 means last cycle reset)");

            // Write each reading's count to durable state, so the reset below has something to clear.
            var sum = 0;
            for (var i = 1; i <= 3; i++)
            {
                var reading = Random.Shared.Next(18, 25);
                sum += reading;
                await context.SetVariableAsync("reading-count", i, ct);
                await Task.Delay(100, ct);
            }
            Console.WriteLine($"{DateTime.UtcNow:HH:mm:ss} UTC cycle done  - 3 readings, avg {sum / 3}C");

            // learn: Clear the job's durable state as the last act of the firing
            // Clear the firing's durable state as its last line, so the next firing starts blank. Never at the top: a
            // reset there also runs on every retry and replay of the same firing and erases its progress.
            await context.ResetStateAsync(ct);
            // learn: end
        }
    }
}

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.