Point at anything with a dotted underline, or tap it, to see what it means.
1Build the key from the time bucket
The key carries the bucket its instant falls in, so every enqueue inside the same half hour composes the same key. bucketSize is thirty minutes and instantA a fixed instant, so the run repeats. PerHour(job, businessKey) and PerDay(job, businessKey) are the fixed sizes, and every bucket is counted in UTC.
// --- Within the same bucket: two enqueues collapse to one job ---
Console.WriteLine("Enqueueing twice in the same 30-minute bucket...");
var keyA1 = IdempotencyKey.PerTimeBucket("nightly-summary", account, instantA, bucketSize);
var first = await jobs.EnqueueAsync(new NightlySummary("acme"), o => o.IdempotencyKey(keyA1));
Console.WriteLine($" enqueue 1: job {first.JobRef} action={first.Action}");
var keyA2 = IdempotencyKey.PerTimeBucket("nightly-summary", account, instantA, bucketSize);
var second = await jobs.EnqueueAsync(new NightlySummary("acme"), o => o.IdempotencyKey(keyA2));
Console.WriteLine($" enqueue 2: job {second.JobRef} action={second.Action}");2Enqueue again inside the bucket and get the first job back
var keyA1 = IdempotencyKey.PerTimeBucket("nightly-summary", account, instantA, bucketSize);
var first = await jobs.EnqueueAsync(new NightlySummary("acme"), o => o.IdempotencyKey(keyA1));
Console.WriteLine($" enqueue 1: job {first.JobRef} action={first.Action}");
var keyA2 = IdempotencyKey.PerTimeBucket("nightly-summary", account, instantA, bucketSize);
var second = await jobs.EnqueueAsync(new NightlySummary("acme"), o => o.IdempotencyKey(keyA2));
Console.WriteLine($" enqueue 2: job {second.JobRef} action={second.Action}");
Console.WriteLine(
second.Action == EnqueueJobAction.Deduplicated ? "Deduplicated - same bucket, same job." : "ERROR: expected Deduplicated"
);3Enqueue in the next bucket and get a new job
// --- Across buckets: a third enqueue in a new bucket inserts a fresh job ---
Console.WriteLine("Enqueueing in the next bucket...");
var keyB = IdempotencyKey.PerTimeBucket("nightly-summary", account, instantB, bucketSize);
var third = await jobs.EnqueueAsync(new NightlySummary("acme"), o => o.IdempotencyKey(keyB));
Console.WriteLine($" enqueue 3: job {third.JobRef} action={third.Action}");
Console.WriteLine(third.Action == EnqueueJobAction.Inserted ? "Inserted - new bucket, new job." : "ERROR: expected Inserted");
Console.WriteLine(first.JobRef != third.JobRef ? "Two distinct jobs across buckets - done." : "ERROR: expected distinct jobs");4The job it acts on
public static class NightlySummaryJob
{
[Job("nightly-summary")]
public static void Handle(NightlySummary input) => Console.WriteLine($"[{input.Tenant}] summary ran");
}5Run it and check the output
From a clone of the Acta repository, with the .NET 10 SDK:
dotnet run --project learn/000-fundamentals/028-one-job-per-periodDeduplicated - same bucket, same job.
Inserted - new bucket, new job.
Two distinct jobs across buckets - done.Checked every release. Before each release, Acta Verify runs this program and checks that its output contains these lines.
Remember
- A delayed job can sit in the database for days before it runs. The name in
[Job("nightly-summary")]and the namespace inRun<OneJobPerPeriodJobs>("one-job-per-period")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. NightlySummaryis 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 asnightly-summary-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/028-one-job-per-period, 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.
one-job-per-period.cs78 lines
// How to allow one job per hour or per day
// An idempotency key built from a time bucket lets one job in per bucket: `IdempotencyKey.PerTimeBucket` here,
// with `PerHour` and `PerDay` as its fixed sizes.
using Acta;
using Acta.Learn.OneJobPerPeriod;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.UseActa(j =>
{
j.UseLocalDatabase(builder.Configuration);
j.Run<OneJobPerPeriodJobs>("one-job-per-period");
});
using var host = builder.Build();
await host.StartAsync();
var jobs = host.Services.GetRequiredService<IJobs>();
// Fixed instants keep the buckets deterministic; a per-run account keeps a second run against the same
// local database from deduplicating onto the first run's jobs.
var account = $"acme-{DateTime.UtcNow:yyyyMMddHHmmssfff}";
var bucketSize = TimeSpan.FromMinutes(30);
var instantA = new DateTimeOffset(2025, 6, 1, 10, 00, 00, TimeSpan.Zero); // bucket 10:00-10:30
var instantB = new DateTimeOffset(2025, 6, 1, 10, 45, 00, TimeSpan.Zero); // bucket 10:30-11:00
// --- Within the same bucket: two enqueues collapse to one job ---
Console.WriteLine("Enqueueing twice in the same 30-minute bucket...");
// learn: 1. Build the key from the time bucket
// The key carries the bucket its instant falls in, so every enqueue inside the same half hour composes the same key.
// `bucketSize` is thirty minutes and `instantA` a fixed instant, so the run repeats. `PerHour(job, businessKey)` and
// `PerDay(job, businessKey)` are the fixed sizes, and every bucket is counted in UTC.
var keyA1 = IdempotencyKey.PerTimeBucket("nightly-summary", account, instantA, bucketSize);
var first = await jobs.EnqueueAsync(new NightlySummary("acme"), o => o.IdempotencyKey(keyA1));
// learn: end
Console.WriteLine($" enqueue 1: job {first.JobRef} action={first.Action}");
// learn: 2. Enqueue again inside the bucket and get the first job back
var keyA2 = IdempotencyKey.PerTimeBucket("nightly-summary", account, instantA, bucketSize);
var second = await jobs.EnqueueAsync(new NightlySummary("acme"), o => o.IdempotencyKey(keyA2));
// learn: end
Console.WriteLine($" enqueue 2: job {second.JobRef} action={second.Action}");
Console.WriteLine(
second.Action == EnqueueJobAction.Deduplicated ? "Deduplicated - same bucket, same job." : "ERROR: expected Deduplicated"
);
// --- Across buckets: a third enqueue in a new bucket inserts a fresh job ---
Console.WriteLine("Enqueueing in the next bucket...");
// learn: 3. Enqueue in the next bucket and get a new job
var keyB = IdempotencyKey.PerTimeBucket("nightly-summary", account, instantB, bucketSize);
var third = await jobs.EnqueueAsync(new NightlySummary("acme"), o => o.IdempotencyKey(keyB));
// learn: end
Console.WriteLine($" enqueue 3: job {third.JobRef} action={third.Action}");
Console.WriteLine(third.Action == EnqueueJobAction.Inserted ? "Inserted - new bucket, new job." : "ERROR: expected Inserted");
Console.WriteLine(first.JobRef != third.JobRef ? "Two distinct jobs across buckets - done." : "ERROR: expected distinct jobs");
await host.StopAsync();
namespace Acta.Learn.OneJobPerPeriod
{
public sealed record NightlySummary(string Tenant);
public static class NightlySummaryJob
{
[Job("nightly-summary")]
public static void Handle(NightlySummary input) => Console.WriteLine($"[{input.Tenant}] summary ran");
}
}