Acta. Durable execution for .NET

learn actastart jobs

How to enqueue many jobs in one round trip

EnqueueJobsAsync writes a whole list of jobs in one round trip to the database and answers their outcomes in request order.

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/019-batch-enqueue
Read as

1Build the requests and enqueue them together

EnqueueJobsAsync writes the whole batch in one round-trip; outcomes return in request order. Two requests with one deduplication key in the same batch throw before anything is written.

batch-enqueue.csexcerpt
using var host = builder.Build();
await host.StartAsync();

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

var requests = Enumerable.Range(1, 500).Select(_ => new EnqueueJobRequest("batch-enqueue", "ping")).ToList();
 
var sw = Stopwatch.StartNew();
var outcomes = (await jobs.EnqueueJobsAsync(requests)).Jobs;
sw.Stop();
Console.WriteLine(
    $"Enqueued {outcomes.Count} jobs in one round-trip in {sw.ElapsedMilliseconds} ms (ids {outcomes[0].JobId}..{outcomes[^1].JobId})."
);
Console.WriteLine("Worker is running - press Ctrl+C to stop.");

2The job it acts on

batch-enqueue.csexcerpt
public static class PingJob
{
    [Job("ping")]
    public static void Handle() { }
}

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/019-batch-enqueue
Its output must contain
Enqueued 500 jobs in one round-trip

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/019-batch-enqueue, 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.

batch-enqueue.cs48 lines
batch-enqueue.cs
// How to enqueue many jobs in one round trip
// `EnqueueJobsAsync` writes a whole list of jobs in one round trip to the database and answers their outcomes in
// request order.
using System.Diagnostics;
using Acta;
using Acta.Learn.BatchEnqueue;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

var builder = Host.CreateApplicationBuilder(args);

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<BatchEnqueueJobs>("batch-enqueue");
});

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

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

// learn: Build the requests and enqueue them together
// EnqueueJobsAsync writes the whole batch in one round-trip; outcomes return in request order. Two requests with one
// deduplication key in the same batch throw before anything is written.
var requests = Enumerable.Range(1, 500).Select(_ => new EnqueueJobRequest("batch-enqueue", "ping")).ToList();
 
var sw = Stopwatch.StartNew();
var outcomes = (await jobs.EnqueueJobsAsync(requests)).Jobs;

// learn: end
sw.Stop();
Console.WriteLine(
    $"Enqueued {outcomes.Count} jobs in one round-trip in {sw.ElapsedMilliseconds} ms (ids {outcomes[0].JobId}..{outcomes[^1].JobId})."
);
Console.WriteLine("Worker is running - press Ctrl+C to stop.");

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

namespace Acta.Learn.BatchEnqueue
{
    public static class PingJob
    {
        [Job("ping")]
        public static void Handle() { }
    }
}

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.