1Give the job a deadline
Deadline is the whole-job budget measured from creation, stable across retries (unlike 304's ExecutionTimeout, which caps a single attempt). Strict is the default: an overdue job is cancelled at admission, and a retry that would land past the deadline is refused re-arm. To show an overdue job the program holds the first run past the deadline: jobs.ScheduleAsync(new GenerateReport("q3"), TimeSpan.FromSeconds(4)).
public static class GenerateReportJob
{
[Job("generate-report", Deadline = "2s")]
public static void Handle(GenerateReport input) =>
Console.WriteLine($"building the {input.Quarter} report (never printed; the job was overdue before it ran)");
}2Run it and check the output
From a clone of the Acta repository, with the .NET 10 SDK:
dotnet run --project learn/300-failure-and-recovery/307-deadline-strictenqueued; 2s deadline, but the first run is held 4s.
] status=Cancelled failureCount=0Checked 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/300-failure-and-recovery/307-deadline-strict, 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.
deadline-strict.cs62 lines
// How to drop a job that is past its deadline
// A `Deadline` is the whole job's budget, measured from its creation. Under the default, strict behaviour an overdue
// job is cancelled before its handler runs.
using Acta;
using Acta.Learn.DeadlineStrict;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.UseActa(j =>
{
j.UseLocalDatabase(builder.Configuration);
j.Run<DeadlineStrictJobs>("deadline-strict");
});
// Quiet the framework's failure logging; the job is cancelled on purpose.
builder.Logging.AddFilter("Acta", LogLevel.Error);
using var host = builder.Build();
await host.StartAsync();
var jobs = host.Services.GetRequiredService<IJobs>();
// The deadline is 2s after creation, but Delayed holds the first run for 4s, so by the time the
// worker can claim it the job is already overdue. A Strict deadline cancels it at admission and the
// handler never runs. Delayed() and the deadline both resolve on the database clock.
var handle = await jobs.ScheduleAsync(new GenerateReport("q3"), TimeSpan.FromSeconds(4));
Console.WriteLine($"[{DateTime.UtcNow:HH:mm:ss}] enqueued; 2s deadline, but the first run is held 4s.");
GetJobResponse? snapshot;
do
{
await Task.Delay(500);
snapshot = await jobs.GetAsync(handle);
} while (snapshot is null or { Status: not (JobStatusCode.Succeeded or JobStatusCode.Failed or JobStatusCode.Cancelled) });
// Cancelled (the finishing event reason is JobDeadlineExceeded). FailureCount stays 0: a deadline
// cancel does not consume the retry budget.
Console.WriteLine($"[{DateTime.UtcNow:HH:mm:ss}] status={snapshot.Status} failureCount={snapshot.FailureCount}");
await host.StopAsync();
namespace Acta.Learn.DeadlineStrict
{
public sealed record GenerateReport(string Quarter);
public static class GenerateReportJob
{
// learn: Give the job a deadline
// Deadline is the whole-job budget measured from creation, stable across retries (unlike 304's
// ExecutionTimeout, which caps a single attempt). Strict is the default: an overdue job is
// cancelled at admission, and a retry that would land past the deadline is refused re-arm. To show an
// overdue job the program holds the first run past the deadline:
// `jobs.ScheduleAsync(new GenerateReport("q3"), TimeSpan.FromSeconds(4))`.
[Job("generate-report", Deadline = "2s")]
// learn: end
public static void Handle(GenerateReport input) =>
Console.WriteLine($"building the {input.Quarter} report (never printed; the job was overdue before it ran)");
}
}