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.
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.
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
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:
dotnet run --project learn/000-fundamentals/014-read-by-idempotency-keyby 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 inRun<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. SendWelcomeEmailis 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 assend-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
// 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}>");
}
}