Acta. Durable execution for .NET

learn actastart jobs

How to find a job by its idempotency key

JobLookup.ByIdempotencyKey finds a job by the key it was enqueued with, when you did not keep its ref.

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/014-read-by-idempotency-key
Read as

Point at anything with a dotted underline, or tap it, to see what it means.

1Enqueue with a key you can build again later

A typed enqueue stores its key exactly as given. This one puts the job's name in front with IdempotencyKey.ForDefinition; whatever key you choose, build it the same way where you look the job up.

read-by-idempotency-key.csexcerpt
using var host = builder.Build();
await host.StartAsync();

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

var businessKey = "welcome:sam";
var finalKey = IdempotencyKey.ForDefinition("send-welcome-email", businessKey);
await jobs.EnqueueAsync(new SendWelcomeEmail("[email protected]", "Sam"), o => o.IdempotencyKey(finalKey));

var snapshot = await jobs.GetAsync(JobLookup.ByIdempotencyKey("read-by-idempotency-key", finalKey));
Console.WriteLine($"Found job {snapshot!.JobRef} by idempotency key '{snapshot.IdempotencyKey}', status {snapshot.Status}.");
Console.WriteLine("Worker is running - press Ctrl+C to stop.");

2Look the job up by that key

The first argument is the namespace the job was enqueued in, the name given to Run<...>; the second is the key exactly as stored. The lookup composes nothing from a business key.

read-by-idempotency-key.csexcerpt
var jobs = host.Services.GetRequiredService<IJobs>();

var businessKey = "welcome:sam";
var finalKey = IdempotencyKey.ForDefinition("send-welcome-email", businessKey);
await jobs.EnqueueAsync(new SendWelcomeEmail("[email protected]", "Sam"), o => o.IdempotencyKey(finalKey));

var snapshot = await jobs.GetAsync(JobLookup.ByIdempotencyKey("read-by-idempotency-key", finalKey));
Console.WriteLine($"Found job {snapshot!.JobRef} by idempotency key '{snapshot.IdempotencyKey}', status {snapshot.Status}.");
Console.WriteLine("Worker is running - press Ctrl+C to stop.");

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

3The job it acts on

read-by-idempotency-key.csexcerpt
public static class WelcomeJob
{
    [Job("send-welcome-email")]
    public static void Handle(SendWelcomeEmail input) => Console.WriteLine($"Sent welcome email to {input.Name} <{input.Email}>");
}

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/014-read-by-idempotency-key
Its output must contain
by idempotency key 'send-welcome-email:welcome:sam'
Sent welcome email to Sam <[email protected]>

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

Remember

  • The name in [Job("send-welcome-email")] and the namespace in Run<ReadByIdempotencyKeyJobs>("read-by-idempotency-key") are stored with every job. Treat both as permanent once jobs exist under them: a job stored under a name no handler carries stays Ready, and nothing runs it.
  • SendWelcomeEmail is stored with each job as its input. Add a field as optional or with a default. A renamed, removed or retyped field needs a new job name such as send-welcome-email-v2, and the old handler stays registered until its jobs have finished: Change a job's input without breaking the jobs already stored.

The whole program

Every file of learn/000-fundamentals/014-read-by-idempotency-key, 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.

read-by-idempotency-key.cs51 lines
read-by-idempotency-key.cs
// How to find a job by its idempotency key
// `JobLookup.ByIdempotencyKey` finds a job by the key it was enqueued with, when you did not keep its ref.
using Acta;
using Acta.Learn.ReadByIdempotencyKey;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<ReadByIdempotencyKeyJobs>("read-by-idempotency-key");
});

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

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

// learn: 1. Enqueue with a key you can build again later
// A typed enqueue stores its key exactly as given. This one puts the job's name in front with
// `IdempotencyKey.ForDefinition`; whatever key you choose, build it the same way where you look the job up.
var businessKey = "welcome:sam";
var finalKey = IdempotencyKey.ForDefinition("send-welcome-email", businessKey);
await jobs.EnqueueAsync(new SendWelcomeEmail("[email protected]", "Sam"), o => o.IdempotencyKey(finalKey));

// learn: end

// learn: 2. Look the job up by that key
// The first argument is the namespace the job was enqueued in, the name given to `Run<...>`; the second is the key
// exactly as stored. The lookup composes nothing from a business key.
var snapshot = await jobs.GetAsync(JobLookup.ByIdempotencyKey("read-by-idempotency-key", finalKey));

// learn: end
Console.WriteLine($"Found job {snapshot!.JobRef} by idempotency key '{snapshot.IdempotencyKey}', status {snapshot.Status}.");
Console.WriteLine("Worker is running - press Ctrl+C to stop.");

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

namespace Acta.Learn.ReadByIdempotencyKey
{
    public sealed record SendWelcomeEmail(string Email, string Name);

    public static class WelcomeJob
    {
        [Job("send-welcome-email")]
        public static void Handle(SendWelcomeEmail input) => Console.WriteLine($"Sent welcome email to {input.Name} <{input.Email}>");
    }
}

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.