1Write a handler with no parameters
With no input there is no type to enqueue by, so the job is named by the call the manifest generates for it.
public static class HeartbeatJob
{
[Job("heartbeat")]
public static Task Handle()
{
Console.WriteLine("Heartbeat tick (no input needed).");
return Task.CompletedTask;
}
}2Enqueue it by its generated call
The manifest's generated call names the job; a job without input takes no payload.
var builder = Host.CreateApplicationBuilder(args);
builder.Services.UseActa(j =>
{
j.UseLocalDatabase(builder.Configuration);
j.Run<NoInputJobJobs>("no-input-job");
});
using var host = builder.Build();
await host.StartAsync();
var jobs = host.Services.GetRequiredService<IJobs>();
await jobs.EnqueueAsync(NoInputJobJobs.Heartbeat());
Console.WriteLine("Enqueued. 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/004-no-input-jobHeartbeat tick (no input needed).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/004-no-input-job, 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.
no-input-job.cs45 lines
// How to write a job that takes no input
// A job with no input: a handler with no parameters, enqueued by its generated call.
using Acta;
using Acta.Learn.NoInputJob;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
var builder = Host.CreateApplicationBuilder(args);
builder.Services.UseActa(j =>
{
j.UseLocalDatabase(builder.Configuration);
j.Run<NoInputJobJobs>("no-input-job");
});
using var host = builder.Build();
await host.StartAsync();
var jobs = host.Services.GetRequiredService<IJobs>();
// learn: 2. Enqueue it by its generated call
// The manifest's generated call names the job; a job without input takes no payload.
await jobs.EnqueueAsync(NoInputJobJobs.Heartbeat());
// learn: end
Console.WriteLine("Enqueued. Worker is running - press Ctrl+C to stop.");
await host.WaitForShutdownAsync();
await host.StopAsync();
namespace Acta.Learn.NoInputJob
{
public static class HeartbeatJob
{
// learn: 1. Write a handler with no parameters
// With no input there is no type to enqueue by, so the job is named by the call the manifest generates for it.
[Job("heartbeat")]
public static Task Handle()
{
Console.WriteLine("Heartbeat tick (no input needed).");
return Task.CompletedTask;
}
// learn: end
}
}