Acta. Durable execution for .NET

learn actastart jobs

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.

A how-to for Acta, durable execution for .NET. New to Acta? Run your first durable job in five minutes.

Checked every release learn/000-fundamentals/011-delayed-job
Read as

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.

delayed-job.csexcerpt
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

delayed-job.csexcerpt
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:

Terminal
dotnet run --project learn/000-fundamentals/011-delayed-job
Its output must contain
Reminder fired for Sam at

Checked 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
delayed-job.cs
// 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}.");
    }
}

New to Acta?

Acta is durable execution for .NET: workflows and jobs that survive restarts, as a library inside your application with its durable state in your SQL database.