1Call the other job as a child and take its result
CallChildAsync enqueues one child and waits for its result; ValueOrThrow() returns it or throws. A failed child stays failed, so the throw fails the parent at once, with no retry; recovering means restarting the child, then the parent once the child has succeeded.
// A child is a real job (own retry policy, queue identity, operator-visible), unlike a private
// idempotent step (202).
[Job("publish-episode")]
public async Task<EpisodePublished> Handle(PublishEpisode episode, JobContext context, CancellationToken ct)
{
var transcode = CallChildJobs.TranscodeVideo(new TranscodeVideo(episode.SourceUrl));
var transcoded = await context.CallChildAsync("transcode", transcode, ct);
return new EpisodePublished(episode.EpisodeId, transcoded.ValueOrThrow().StreamUrl);
}2Run it and check the output
From a clone of the Acta repository, with the .NET 10 SDK:
dotnet run --project learn/200-durable-execution/213-call-childpublished: EpisodePublished { EpisodeId = ep-42, StreamUrl = hls/ep-42.m3u8 }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/200-durable-execution/213-call-child, 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.
call-child.cs64 lines
// How to call another job and use its result
// `CallChildAsync` starts another job as a child, waits for it without holding a worker, and returns its outcome,
// whose `ValueOrThrow()` is the result.
using Acta;
using Acta.Learn.CallChild;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.UseActa(j =>
{
j.UseLocalDatabase(builder.Configuration);
j.Run<CallChildJobs>("call-child");
});
using var host = builder.Build();
await host.StartAsync();
var jobs = host.Services.GetRequiredService<IJobs>();
var outcome = await jobs.CallAsync(CallChildJobs.PublishEpisode(new PublishEpisode("ep-42", "raw/ep-42.mov")));
Console.WriteLine($"published: {outcome.Value}");
await host.StopAsync();
namespace Acta.Learn.CallChild
{
public sealed record PublishEpisode(string EpisodeId, string SourceUrl);
public sealed record TranscodeVideo(string SourceUrl);
public sealed record TranscodeResult(string StreamUrl);
public sealed record EpisodePublished(string EpisodeId, string StreamUrl);
public sealed class PublishingJobs
{
// A child is a real job (own retry policy, queue identity, operator-visible), unlike a private
// idempotent step (202).
[Job("publish-episode")]
public async Task<EpisodePublished> Handle(PublishEpisode episode, JobContext context, CancellationToken ct)
{
// learn: Call the other job as a child and take its result
// CallChildAsync enqueues one child and waits for its result;
// ValueOrThrow() returns it or throws. A failed child stays failed, so the throw fails the
// parent at once, with no retry; recovering means restarting the child, then the parent once
// the child has succeeded.
var transcode = CallChildJobs.TranscodeVideo(new TranscodeVideo(episode.SourceUrl));
var transcoded = await context.CallChildAsync("transcode", transcode, ct);
return new EpisodePublished(episode.EpisodeId, transcoded.ValueOrThrow().StreamUrl);
// learn: end
}
[Job("transcode-video")]
public async Task<TranscodeResult> Transcode(TranscodeVideo video, CancellationToken ct)
{
await Task.Delay(400, ct);
var streamUrl = video.SourceUrl.Replace("raw/", "hls/").Replace(".mov", ".m3u8");
Console.WriteLine($"transcoded {video.SourceUrl} -> {streamUrl}");
return new TranscodeResult(streamUrl);
}
}
}