Acta. Durable execution for .NET

learn actastart jobs

How to enqueue by the input's type

EnqueueAsync takes the input alone: its type names the job, so there is no name string to get wrong.

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/009-typed-enqueue
Read as

1Enqueue the input and let its type pick the job

No job-name string: Acta picks the job from the input type. The handle that comes back carries the job's ref and says whether this call inserted the job.

typed-enqueue.csexcerpt
var builder = Host.CreateApplicationBuilder(args);

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<TypedEnqueueJobs>("typed-enqueue");
});

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

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

var handle = await jobs.EnqueueAsync(new GreetUser("Sam"));
Console.WriteLine($"Enqueued job {handle.JobRef} ({handle.Action}). Worker is running - press Ctrl+C to stop.");

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

2The job it acts on

typed-enqueue.csexcerpt
public static class GreetUserJob
{
    [Job("greet-user")]
    public static void Handle(GreetUser input) => Console.WriteLine($"Hi, {input.Name}!");
}

3Run it and check the output

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

Terminal
dotnet run --project learn/000-fundamentals/009-typed-enqueue
Its output must contain
(Inserted)
Hi, Sam!

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/009-typed-enqueue, 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.

typed-enqueue.cs41 lines
typed-enqueue.cs
// How to enqueue by the input's type
// `EnqueueAsync` takes the input alone: its type names the job, so there is no name string to get wrong.
using Acta;
using Acta.Learn.TypedEnqueue;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<TypedEnqueueJobs>("typed-enqueue");
});

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

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

// learn: Enqueue the input and let its type pick the job
// No job-name string: Acta picks the job from the input type. The handle that comes back carries the job's ref and
// says whether this call inserted the job.
var handle = await jobs.EnqueueAsync(new GreetUser("Sam"));

// learn: end
Console.WriteLine($"Enqueued job {handle.JobRef} ({handle.Action}). Worker is running - press Ctrl+C to stop.");

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

namespace Acta.Learn.TypedEnqueue
{
    public sealed record GreetUser(string Name);

    public static class GreetUserJob
    {
        [Job("greet-user")]
        public static void Handle(GreetUser input) => Console.WriteLine($"Hi, {input.Name}!");
    }
}

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.