Acta. Durable execution for .NET

learn actatake the first steps

How to pass typed input to a job

The input is a record: Acta stores it with the job and hands it to the handler as its first parameter.

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

Checked every release learn/000-fundamentals/002-job-input
Read as

1Write the input as a record

The input is plain data. Acta serializes it, stores it with the job, and hands it to the handler.

job-input.csexcerpt
public sealed record PrintNameTag(string Name, string Workshop);

2Take the record as the handler's first parameter

The handler declares the record as its first parameter, and Acta passes it the stored input on every attempt.

job-input.csexcerpt
public static class PrintNameTagJob
{
    [Job("print-name-tag")]
    public static async Task Handle(PrintNameTag tag, CancellationToken ct)
    {
        await Task.Delay(50, ct);
        Console.WriteLine($"Printed name tag: {tag.Name} ({tag.Workshop})");
    }
}

3Enqueue an instance of it

The type of the value picks the job: the handler whose first parameter is a PrintNameTag.

job-input.csexcerpt
var builder = Host.CreateApplicationBuilder(args);

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<JobInputJobs>("job-input");
});

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

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

await jobs.EnqueueAsync(new PrintNameTag("Ada", "workshop"));
Console.WriteLine("Enqueued. Worker is running - press Ctrl+C to stop.");

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

4Run it and check the output

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

Terminal
dotnet run --project learn/000-fundamentals/002-job-input
Its output must contain
Printed name tag: Ada (workshop)

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/000-fundamentals/002-job-input, 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.

job-input.cs51 lines
job-input.cs
// How to pass typed input to a job
// The input is a record: Acta stores it with the job and hands it to the handler as its first parameter.
using Acta;
using Acta.Learn.JobInput;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<JobInputJobs>("job-input");
});

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

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

// learn: 3. Enqueue an instance of it
// The type of the value picks the job: the handler whose first parameter is a `PrintNameTag`.
await jobs.EnqueueAsync(new PrintNameTag("Ada", "workshop"));

// learn: end
Console.WriteLine("Enqueued. Worker is running - press Ctrl+C to stop.");

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

namespace Acta.Learn.JobInput
{
    // learn: 1. Write the input as a record
    // The input is plain data. Acta serializes it, stores it with the job, and hands it to the handler.
    public sealed record PrintNameTag(string Name, string Workshop);

    // learn: end

    public static class PrintNameTagJob
    {
        // learn: 2. Take the record as the handler's first parameter
        // The handler declares the record as its first parameter, and Acta passes it the stored input on every attempt.
        [Job("print-name-tag")]
        public static async Task Handle(PrintNameTag tag, CancellationToken ct)
        {
            await Task.Delay(50, ct);
            Console.WriteLine($"Printed name tag: {tag.Name} ({tag.Workshop})");
        }
        // 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.