Acta. Durable execution for .NET

learn actadecide what happens when work fails

How to cut off an attempt that runs too long

ExecutionTimeout caps one attempt: when the time is up, the handler's CancellationToken trips and the cut spends an attempt.

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/304-execution-timeout
Read as

1Set the timeout on the job

ExecutionTimeout caps a single attempt: after 2s the framework trips the CancellationToken. The cut spends an attempt, and with MaxAttempts = 1 the job ends Failed.

execution-timeout.csexcerpt
public sealed class SlowReportJob
{
    [Job("slow-report", ExecutionTimeout = "2s", MaxAttempts = 1)]
    public async Task Handle(SlowReport input, JobContext context, CancellationToken ct)
    {
        Console.WriteLine($"[{input.Name}] generating report (this runs too long)...");
        await Task.Delay(TimeSpan.FromSeconds(10), ct); // exceeds the 2s timeout, so ct trips here
        Console.WriteLine($"[{input.Name}] report done"); // never reached
    }
}

2Pass the token to the work, so the timeout can stop it

execution-timeout.csexcerpt
public sealed class SlowReportJob
{
    [Job("slow-report", ExecutionTimeout = "2s", MaxAttempts = 1)]
    public async Task Handle(SlowReport input, JobContext context, CancellationToken ct)
    {
        Console.WriteLine($"[{input.Name}] generating report (this runs too long)...");
        await Task.Delay(TimeSpan.FromSeconds(10), ct); // exceeds the 2s timeout, so ct trips here
        Console.WriteLine($"[{input.Name}] report done"); // never reached
    }
}

3Run 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/304-execution-timeout
Its output must contain
[q3] generating report (this runs too long)...
status=Failed

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/304-execution-timeout, 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.

execution-timeout.cs54 lines
execution-timeout.cs
// How to cut off an attempt that runs too long
// `ExecutionTimeout` caps one attempt: when the time is up, the handler's `CancellationToken` trips and the cut
// spends an attempt.
using Acta;
using Acta.Learn.ExecutionTimeout;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<ExecutionTimeoutJobs>("execution-timeout");
});

// Quiet the framework's failure logging; the job times out on purpose.
builder.Logging.AddFilter("Acta", LogLevel.Error);

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

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

var outcome = await jobs.EnqueueAsync(new SlowReport("q3"));
await Task.Delay(4000); // longer than the 2s execution timeout

var snapshot = await jobs.GetAsync(outcome);
Console.WriteLine($"status={snapshot!.Status}");

await host.StopAsync();

namespace Acta.Learn.ExecutionTimeout
{
    public sealed record SlowReport(string Name);

    public sealed class SlowReportJob
    {
        // learn: 1. Set the timeout on the job
        // ExecutionTimeout caps a single attempt: after 2s the framework trips the CancellationToken. The cut
        // spends an attempt, and with MaxAttempts = 1 the job ends `Failed`.
        [Job("slow-report", ExecutionTimeout = "2s", MaxAttempts = 1)]
        // learn: end
        public async Task Handle(SlowReport input, JobContext context, CancellationToken ct)
        {
            Console.WriteLine($"[{input.Name}] generating report (this runs too long)...");
            // learn: 2. Pass the token to the work, so the timeout can stop it
            await Task.Delay(TimeSpan.FromSeconds(10), ct); // exceeds the 2s timeout, so ct trips here
            // learn: end
            Console.WriteLine($"[{input.Name}] report done"); // never reached
        }
    }
}

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.