# 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.

- Section: Start jobs, step 10
- Program: `learn/000-fundamentals/028-one-job-per-period` in https://github.com/acta-dotnet/acta (one compiled project)
- Status: Checked every release. Before each release, Acta Verify runs this program and checks that its output contains these lines.
- Rules: [/llms.txt](/llms.txt) is the contract every handler follows; "Remember" below carries the rules that bite this task.
- Release: the one /llms.txt names at its top.
- Page for people: /learn/028-one-job-per-period
- New to Acta: it is durable execution for .NET, a library with its state in the app's SQL database; the first run is in /llms.txt.

## 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.

The lines that matter:

```csharp
var keyA1 = IdempotencyKey.PerTimeBucket("nightly-summary", account, instantA, bucketSize);
var first = await jobs.EnqueueAsync(new NightlySummary("acme"), o => o.IdempotencyKey(keyA1));
```

In `one-job-per-period.cs` (excerpt):

```csharp
// --- 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}");
```

## 2. Enqueue again inside the bucket and get the first job back

The lines that matter:

```csharp
var keyA2 = IdempotencyKey.PerTimeBucket("nightly-summary", account, instantA, bucketSize);
var second = await jobs.EnqueueAsync(new NightlySummary("acme"), o => o.IdempotencyKey(keyA2));
```

In `one-job-per-period.cs` (excerpt):

```csharp
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"
);
```

## 3. Enqueue in the next bucket and get a new job

The lines that matter:

```csharp
var keyB = IdempotencyKey.PerTimeBucket("nightly-summary", account, instantB, bucketSize);
var third = await jobs.EnqueueAsync(new NightlySummary("acme"), o => o.IdempotencyKey(keyB));
```

In `one-job-per-period.cs` (excerpt):

```csharp
// --- 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");
```

## 4. The job it acts on

`one-job-per-period.cs` (excerpt):

```csharp
public static class NightlySummaryJob
{
    [Job("nightly-summary")]
    public static void Handle(NightlySummary input) => Console.WriteLine($"[{input.Tenant}] summary ran");
}
```

## 5. Run it and check the output

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

```bash
dotnet run --project learn/000-fundamentals/028-one-job-per-period
```

Its output must contain:

```text
Deduplicated - 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 in `Run<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.
- `NightlySummary` 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 `nightly-summary-v2`, and the old handler stays registered until its jobs have finished: [Change a job's input without breaking the jobs already stored](/learn/502-job-versions.md).

## Not in the packages

`UseLocalDatabase` is a helper of the Acta repository (`support/`), used so every program runs on SQLite, PostgreSQL or SQL Server. Do not copy it into an application: register the database with `UseSqlite`, `UsePostgres` or `UseSqlServer`, as in [how to run your first job](/learn/001-hello-acta.md). The rows a lab prints come from SQL views, which are the operator's contract; application code reads the same facts through `IJobs` and `IActaOperations`.

## Next

- [How to save a record and start its job in one transaction](/learn/029-enqueue-in-transaction.md)

## More

- [The whole program](/learn/028-one-job-per-period.program.md): every file as compiled, 78 lines.
