1Schedule the job instead of enqueueing it
ScheduleAsync counts the delay on the database clock (db_now + 5s); this host's clock does not affect timing.
var builder = Host.CreateApplicationBuilder(args);
builder.Services.UseActa(j =>
{
j.UseLocalDatabase(builder.Configuration);
j.Run<DelayedJobJobs>("delayed-job");
});
using var host = builder.Build();
await host.StartAsync();
var jobs = host.Services.GetRequiredService<IJobs>();
var handle = await jobs.ScheduleAsync(new SendReminder("Sam"), TimeSpan.FromSeconds(5));
Console.WriteLine($"Job {handle.JobRef} enqueued at {DateTime.UtcNow:HH:mm:ss}; runs in ~5s.");
Console.WriteLine("Worker is running - press Ctrl+C to stop.");
await host.WaitForShutdownAsync();
await host.StopAsync();2The job it acts on
public static class ReminderJob
{
[Job("send-reminder")]
public static void Handle(SendReminder input) =>
Console.WriteLine($"Reminder fired for {input.Name} at {DateTime.UtcNow:HH:mm:ss}.");
}3Run it and check the output
From a clone of the Acta repository, with the .NET 10 SDK:
dotnet run --project learn/000-fundamentals/011-delayed-jobReminder fired for Sam atChecked every release. Before each release, Acta Verify runs this program and checks that its output contains these lines.
Remember
ScheduleAsync(input, DateTimeOffset)runs the job at an instant instead of after a delay. Work that recurs is a[Schedule], not a job that schedules itself.
The whole program
Every file of learn/000-fundamentals/011-delayed-job, 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.
delayed-job.cs43 lines
// How to run a job once, later
// `ScheduleAsync(input, delay)` stores the job now and makes it due after the delay. No process has to stay alive
// until then.
using Acta;
using Acta.Learn.DelayedJob;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.UseActa(j =>
{
j.UseLocalDatabase(builder.Configuration);
j.Run<DelayedJobJobs>("delayed-job");
});
using var host = builder.Build();
await host.StartAsync();
var jobs = host.Services.GetRequiredService<IJobs>();
// learn: Schedule the job instead of enqueueing it
// ScheduleAsync counts the delay on the database clock (db_now + 5s); this host's clock does not affect timing.
var handle = await jobs.ScheduleAsync(new SendReminder("Sam"), TimeSpan.FromSeconds(5));
// learn: end
Console.WriteLine($"Job {handle.JobRef} enqueued at {DateTime.UtcNow:HH:mm:ss}; runs in ~5s.");
Console.WriteLine("Worker is running - press Ctrl+C to stop.");
await host.WaitForShutdownAsync();
await host.StopAsync();
namespace Acta.Learn.DelayedJob
{
public sealed record SendReminder(string Name);
public static class ReminderJob
{
[Job("send-reminder")]
public static void Handle(SendReminder input) =>
Console.WriteLine($"Reminder fired for {input.Name} at {DateTime.UtcNow:HH:mm:ss}.");
}
}