1Enqueue through the generated call
The manifest's generated call names the job and takes its handler's parameters; the namespace resolves from the registered manifest, and nothing is inferred from the input's type.
var builder = Host.CreateApplicationBuilder(args);
builder.Services.UseActa(j =>
{
j.UseLocalDatabase(builder.Configuration);
j.Run<JobCallJobs>("job-call");
});
using var host = builder.Build();
await host.StartAsync();
var jobs = host.Services.GetRequiredService<IJobs>();
var single = await jobs.EnqueueAsync(JobCallJobs.Hello(new Hello("World")));
var several = await jobs.EnqueueAsync(JobCallJobs.Greet("Ada", 3), o => o.DeduplicationKey("greet-ada"));
Console.WriteLine($"Enqueued hello as {single.JobRef} and greet as {several.JobRef}.");
await host.WaitForShutdownAsync();
await host.StopAsync();2Give a handler several parameters
Several input parameters travel as one generated record, JobCallJobs.GreetArgs, stored as {"name":..,"times":..}.
public static class HelloJobs
{
[Job("hello")]
public static void Handle(Hello input) => Console.WriteLine($"Hello, {input.Name}!");
[Job("greet")]
public static void Greet(string name, int times)
{
for (var i = 0; i < times; i++)
{
Console.WriteLine($"Greetings, {name}!");
}
}
}3Run it and check the output
From a clone of the Acta repository, with the .NET 10 SDK:
dotnet run --project learn/000-fundamentals/023-job-callHello, World!
Greetings, Ada!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/023-job-call, 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-call.cs54 lines
// How to enqueue by the job's generated call
// The manifest's generated call names the job and takes its handler's parameters, several of them as one input.
using Acta;
using Acta.Learn.JobCall;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.UseActa(j =>
{
j.UseLocalDatabase(builder.Configuration);
j.Run<JobCallJobs>("job-call");
});
using var host = builder.Build();
await host.StartAsync();
var jobs = host.Services.GetRequiredService<IJobs>();
// learn: 1. Enqueue through the generated call
// The manifest's generated call names the job and takes its handler's parameters; the namespace resolves from the
// registered manifest, and nothing is inferred from the input's type.
var single = await jobs.EnqueueAsync(JobCallJobs.Hello(new Hello("World")));
var several = await jobs.EnqueueAsync(JobCallJobs.Greet("Ada", 3), o => o.DeduplicationKey("greet-ada"));
// learn: end
Console.WriteLine($"Enqueued hello as {single.JobRef} and greet as {several.JobRef}.");
await host.WaitForShutdownAsync();
await host.StopAsync();
namespace Acta.Learn.JobCall
{
public sealed record Hello(string Name);
public static class HelloJobs
{
[Job("hello")]
public static void Handle(Hello input) => Console.WriteLine($"Hello, {input.Name}!");
// learn: 2. Give a handler several parameters
// Several input parameters travel as one generated record, JobCallJobs.GreetArgs, stored as {"name":..,"times":..}.
[Job("greet")]
public static void Greet(string name, int times)
{
for (var i = 0; i < times; i++)
{
Console.WriteLine($"Greetings, {name}!");
}
}
// learn: end
}
}