1Put the jobs in one class
Each [Job] method in a class registers independently, under the name in its attribute.
public static class TextAndMath
{
[Job("shout")]
public static void Shout(string text) => Console.WriteLine(text.ToUpperInvariant());
[Job("double-it")]
public static void Double(int n) => Console.WriteLine(n * 2);
[Job("halve")]
public static void Halve(double x) => Console.WriteLine(x / 2);
}2Enqueue each job by its own call
Each [Job] method gets its own generated call, named after the job.
using var host = builder.Build();
await host.StartAsync();
var jobs = host.Services.GetRequiredService<IJobs>();
await jobs.EnqueueAsync(ManyJobsOneClassJobs.Shout("acta"));
await jobs.EnqueueAsync(ManyJobsOneClassJobs.DoubleIt(21));
await jobs.EnqueueAsync(ManyJobsOneClassJobs.Halve(9.0));
Console.WriteLine("Enqueued 3 jobs. Worker is running - press Ctrl+C to stop.");
await host.WaitForShutdownAsync();
await host.StopAsync();3Run it and check the output
From a clone of the Acta repository, with the .NET 10 SDK:
dotnet run --project learn/000-fundamentals/006-many-jobs-one-classACTA
42Checked 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/006-many-jobs-one-class, 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.
many-jobs-one-class.cs49 lines
// How to keep several jobs in one class
// A class can hold several `[Job]` methods. Each is a job of its own, with its own generated call.
using Acta;
using Acta.Learn.ManyJobsOneClass;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.UseActa(j =>
{
j.UseLocalDatabase(builder.Configuration);
j.Run<ManyJobsOneClassJobs>("many-jobs-one-class");
});
using var host = builder.Build();
await host.StartAsync();
var jobs = host.Services.GetRequiredService<IJobs>();
// learn: 2. Enqueue each job by its own call
// Each [Job] method gets its own generated call, named after the job.
await jobs.EnqueueAsync(ManyJobsOneClassJobs.Shout("acta"));
await jobs.EnqueueAsync(ManyJobsOneClassJobs.DoubleIt(21));
await jobs.EnqueueAsync(ManyJobsOneClassJobs.Halve(9.0));
// learn: end
Console.WriteLine("Enqueued 3 jobs. Worker is running - press Ctrl+C to stop.");
await host.WaitForShutdownAsync();
await host.StopAsync();
namespace Acta.Learn.ManyJobsOneClass
{
public static class TextAndMath
{
// learn: 1. Put the jobs in one class
// Each [Job] method in a class registers independently, under the name in its attribute.
[Job("shout")]
public static void Shout(string text) => Console.WriteLine(text.ToUpperInvariant());
[Job("double-it")]
public static void Double(int n) => Console.WriteLine(n * 2);
[Job("halve")]
public static void Halve(double x) => Console.WriteLine(x / 2);
// learn: end
}
}