Acta. Durable execution for .NET

learn actastart jobs

How to run urgent jobs first

A priority on the enqueue decides which of the jobs that are due at once a worker claims first.

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/017-priority
Read as

1Set the priority when you enqueue

Priority is an option of the enqueue. When several jobs are due at once, the worker claims the one with the higher priority first, whichever arrived first. together is one instant a moment ahead that every job here shares, so they all come due at once.

priority.csexcerpt
await jobs.EnqueueAsync(new GenerateReport("URGENT"), o => o.Priority(JobPriorityCode.High).NextRunAt(together));
Console.WriteLine("Enqueued. Worker is running - press Ctrl+C to stop.");

await host.WaitForShutdownAsync();
await host.StopAsync();

2The job it acts on

priority.csexcerpt
public static class ReportJob
{
    [Job("generate-report")]
    public static async Task Handle(GenerateReport input, CancellationToken ct)
    {
        Console.WriteLine($"running {input.Name}");
        await Task.Delay(400, ct);
    }
}

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/017-priority
Its output must contain
running URGENT

Checked 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/000-fundamentals/017-priority, 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.

priority.cs58 lines
priority.cs
// How to run urgent jobs first
// A priority on the enqueue decides which of the jobs that are due at once a worker claims first.
using Acta;
using Acta.Learn.Priority;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    // One executor at a time, so claim order is observable.
    j.ConfigureOptions(o => o.MaxConcurrentExecutors = 1);
    j.Run<PriorityJobs>("priority");
});

using var host = builder.Build();
await host.StartAsync();

var jobs = host.Services.GetRequiredService<IJobs>();

await jobs.EnqueueAsync(new GenerateReport("bulk-1"), o => o.Priority(JobPriorityCode.Bulk));

// Due at the same instant, so priority (not arrival order) decides claim order: High beats Bulk.
// Expected order: bulk-1, URGENT, bulk-2, bulk-3, bulk-4, bulk-5.
var together = DateTimeOffset.UtcNow.AddSeconds(1.5);
foreach (var n in new[] { 2, 3, 4, 5 })
{
    await jobs.EnqueueAsync(new GenerateReport($"bulk-{n}"), o => o.Priority(JobPriorityCode.Bulk).NextRunAt(together));
}

// learn: Set the priority when you enqueue
// `Priority` is an option of the enqueue. When several jobs are due at once, the worker claims the one with the
// higher priority first, whichever arrived first. `together` is one instant a moment ahead that every job here
// shares, so they all come due at once.
await jobs.EnqueueAsync(new GenerateReport("URGENT"), o => o.Priority(JobPriorityCode.High).NextRunAt(together));

// learn: end
Console.WriteLine("Enqueued. Worker is running - press Ctrl+C to stop.");

await host.WaitForShutdownAsync();
await host.StopAsync();

namespace Acta.Learn.Priority
{
    public sealed record GenerateReport(string Name);

    public static class ReportJob
    {
        [Job("generate-report")]
        public static async Task Handle(GenerateReport input, CancellationToken ct)
        {
            Console.WriteLine($"running {input.Name}");
            await Task.Delay(400, ct);
        }
    }
}

Where to go next

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.