Acta. Durable execution for .NET

learn actadeploy and scale

How to change a job's input without breaking the jobs already stored

A job enqueued before a deploy can run after it, so the new code has to read the input the old code stored. Keep the job's name for a change old rows survive, and give the job a new versioned name for a change they cannot.

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

Checked every release learn/500-payloads/502-job-versions
Read as

Point at anything with a dotted underline, or tap it, to see what it means.

1Add a field the stored rows can do without

Note is optional and has a default, so a row stored before it existed still reads: the job keeps its name.

job-versions.csexcerpt
public sealed record SendInvoice(string InvoiceId, string? Note = null);

2Give a change the stored rows cannot supply a new input

Every invoice now needs a currency, and the rows already stored have none. That is a new input, for a new name.

job-versions.csexcerpt
public sealed record SendInvoiceV2(string InvoiceId, string Currency);

3Put the new version right under the old one

Both call the same service, so the difference between them is one line each. The old handler stays registered until none of its jobs can still run, wait or be restarted; from then on there is one again.

job-versions.csexcerpt
public sealed class SendInvoiceJobs(Invoices invoices)
{
    [Job("send-invoice")]
    public Task V1(SendInvoice input, CancellationToken ct) => invoices.SendAsync(input.InvoiceId, "EUR", input.Note, ct);
 
    [Job("send-invoice-v2")]
    public Task V2(SendInvoiceV2 input, CancellationToken ct) => invoices.SendAsync(input.InvoiceId, input.Currency, null, ct);
}

4Enqueue new work under the new name

The callers move to the new input. The rows already stored under the old name keep their handler.

job-versions.csexcerpt
var next = await jobs.EnqueueAsync(new SendInvoiceV2("INV-3", "USD"));

(await jobs.WaitAsync(stored)).ThrowIfFailed();
(await current.WaitAsync()).ThrowIfFailed();
(await next.WaitAsync()).ThrowIfFailed();

await host.StopAsync();

5Run it and check the output

From a clone of the Acta repository, with the .NET 10 SDK:

Terminal
dotnet run --project learn/500-payloads/502-job-versions
Its output must contain
sent INV-1 in EUR
sent INV-2 in EUR (note: thanks)
sent INV-3 in USD

Checked every release. Before each release, Acta Verify runs this program and checks that its output contains these lines.

Remember

  • A job enqueued before a deploy can run after it. SendInvoice is stored with each job, so the new code has to read what the old code wrote.
  • Keep the name send-invoice for a change the stored rows survive: an optional, nullable or defaulted field, such as Note.
  • Use a new versioned name, send-invoice-v2, when the stored rows would be misread or rejected: a renamed, removed or retyped field, a changed meaning, or required data they cannot supply, such as Currency.
  • Put the new version right under the old one. From then on there are two to maintain, until the old one's jobs have finished.
  • Keep the old handler registered until none of its jobs can still run, wait or be restarted. Dropping it cancels nothing: its jobs stay Ready with no handler to run them.
  • The name in [Job("send-invoice")] and the namespace in Run<JobVersionsJobs>("job-versions") are stored with every job too. A version is a new name beside the old one, never a rename.
  • Inside a handler, a step, signal, timer, variable and child name is a key into what the job has stored. A renamed step runs again.

Check yourself

A send-invoice job is waiting for its next retry when the build with send-invoice-v2 is deployed. Which handler runs it?

Show the answer

The send-invoice handler, since the job's name is stored with the job. That is why the old handler stays registered: the stored jobs still ask for it by name.

A stored row has no Note. What does the handler read?

Show the answer

null, the field's default. An optional field with a default is a change the stored rows survive, so the job keeps its name.

The whole program

Every file of learn/500-payloads/502-job-versions, 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.

job-versions.cs84 lines
job-versions.cs
// How to change a job's input without breaking the jobs already stored
// A job enqueued before a deploy can run after it, so the new code has to read the input the old code stored. Keep
// the job's name for a change old rows survive, and give the job a new versioned name for a change they cannot.
using Acta;
using Acta.Learn.JobVersions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;

var builder = Host.CreateApplicationBuilder(args);
builder.Logging.AddFilter("Acta", LogLevel.Error);
builder.Services.AddSingleton<Invoices>();

builder.Services.UseActa(j =>
{
    j.UseLocalDatabase(builder.Configuration);
    j.Run<JobVersionsJobs>("job-versions");
});

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

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

// A row as the first version of the code stored it, before SendInvoice had a Note.
var stored = await jobs.EnqueueAsync(
    new EnqueueJobRequest("job-versions", "send-invoice", JobPayload.Json(new SendInvoiceAsFirstStored("INV-1"))),
    CancellationToken.None
);
var current = await jobs.EnqueueAsync(new SendInvoice("INV-2", "thanks"));

// learn: 4. Enqueue new work under the new name
// The callers move to the new input. The rows already stored under the old name keep their handler.
var next = await jobs.EnqueueAsync(new SendInvoiceV2("INV-3", "USD"));

// learn: end

(await jobs.WaitAsync(stored)).ThrowIfFailed();
(await current.WaitAsync()).ThrowIfFailed();
(await next.WaitAsync()).ThrowIfFailed();

await host.StopAsync();

namespace Acta.Learn.JobVersions
{
    /// <summary>The input as the first version of the code stored it: this program uses it only to write such a row.</summary>
    public sealed record SendInvoiceAsFirstStored(string InvoiceId);

    // learn: 1. Add a field the stored rows can do without
    // Note is optional and has a default, so a row stored before it existed still reads: the job keeps its name.
    public sealed record SendInvoice(string InvoiceId, string? Note = null);

    // learn: end

    // learn: 2. Give a change the stored rows cannot supply a new input
    // Every invoice now needs a currency, and the rows already stored have none. That is a new input, for a new name.
    public sealed record SendInvoiceV2(string InvoiceId, string Currency);

    // learn: end

    public sealed class SendInvoiceJobs(Invoices invoices)
    {
        // learn: 3. Put the new version right under the old one
        // Both call the same service, so the difference between them is one line each. The old handler stays
        // registered until none of its jobs can still run, wait or be restarted; from then on there is one again.
        [Job("send-invoice")]
        public Task V1(SendInvoice input, CancellationToken ct) => invoices.SendAsync(input.InvoiceId, "EUR", input.Note, ct);
 
        [Job("send-invoice-v2")]
        public Task V2(SendInvoiceV2 input, CancellationToken ct) => invoices.SendAsync(input.InvoiceId, input.Currency, null, ct);

        // learn: end
    }

    /// <summary>The service both versions call.</summary>
    public sealed class Invoices
    {
        public Task SendAsync(string invoiceId, string currency, string? note, CancellationToken ct)
        {
            Console.WriteLine($"sent {invoiceId} in {currency}{(note is null ? "" : $" (note: {note})")}");
            return Task.CompletedTask;
        }
    }
}

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.