1Give the schedule an interval instead of a cron
A schedule can be cron or an interval duration - the human form ("10s") or ISO 8601 ("PT10S", and calendar forms like "P1D"). An interval has no wall-clock anchor, so it ignores TimeZoneId.
public sealed class HeartbeatJob
{
[Job("heartbeat")]
[Schedule("every-10-seconds", "10s")]
public void Handle(Heartbeat input) => Console.WriteLine($"{DateTime.UtcNow:HH:mm:ss} UTC heartbeat");
}2Run it and check the output
From a clone of the Acta repository, with the .NET 10 SDK:
dotnet run --project learn/100-scheduling/102-interval-schedule heartbeatChecked 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/100-scheduling/102-interval-schedule, 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.
interval-schedule.cs33 lines
// How to replace a timer loop with an interval schedule
// An interval schedule, such as `10s`, runs a job at a fixed gap, where a timer loop in a hosted service would
// otherwise stand.
using Acta;
using Acta.Learn.IntervalSchedule;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.UseActa(j =>
{
j.UseLocalDatabase(builder.Configuration);
j.Run<IntervalScheduleJobs>("interval-schedule");
});
// The [Schedule] below recurs on a fixed interval, so there's nothing to enqueue.
await builder.Build().RunAsync();
namespace Acta.Learn.IntervalSchedule
{
public readonly record struct Heartbeat;
public sealed class HeartbeatJob
{
[Job("heartbeat")]
// learn: Give the schedule an interval instead of a cron
// A schedule can be cron or an interval duration - the human form ("10s") or ISO 8601 ("PT10S",
// and calendar forms like "P1D"). An interval has no wall-clock anchor, so it ignores TimeZoneId.
[Schedule("every-10-seconds", "10s")]
// learn: end
public void Handle(Heartbeat input) => Console.WriteLine($"{DateTime.UtcNow:HH:mm:ss} UTC heartbeat");
}
}