Job Configuration
Attributes on a handler class set the defaults for every job of that handler. A scheduling call can still override them; the order is in Where Each Setting Comes From.
[JobMasterDefinitionId("reports.monthly")]
[JobMasterTimeout(300)]
[JobMasterMaxNumberOfRetries(5)]
[JobMasterPriority(JobMasterPriority.Low)]
[JobMasterWorkerLane("reports")]
[JobMasterMetadata("Category", "Reports")]
public sealed class MonthlyReportHandler : IJobMasterHandler
{
public async Task HandleAsync(JobContext job) { ... }
}
All of them are optional.
Attributes
Definition Id
[JobMasterDefinitionId("reports.monthly")] is the id stored with each job to find the handler that runs it.
Without it, the class's full name is used (MyApp.Reports.MonthlyReportHandler). Renaming the class or moving it to another namespace then changes the id, and jobs already scheduled no longer find their handler. Setting it on every handler is recommended.
Timeout
[JobMasterTimeout(300)] is how long a run may take, in seconds. Without it, the cluster's DefaultJobTimeout applies; if the cluster doesn't set one either, it's 1 minute.
When the timeout passes, job.CancellationToken is cancelled. Pass it to your async calls so the handler stops on time; the run then counts as timed out and is retried like any failure. A handler that ignores the token runs until it finishes.
public async Task HandleAsync(JobContext job)
{
await reportService.GenerateAsync(job.MsgData.GetLongValue("ReportId"), job.CancellationToken);
}
Max Retries
[JobMasterMaxNumberOfRetries(5)] is how many times a failed run is retried. After the last retry fails, the job ends as Failed. Without it, the cluster's DefaultMaxRetryCount applies; if the cluster doesn't set one either, it's 3.
The wait between retries is set on the cluster with RetryDelays; see Cluster Basics.
Priority
[JobMasterPriority(JobMasterPriority.Low)] sets the job's priority: VeryLow, Low, Medium, High or Critical. A higher priority gets a larger share of a worker's capacity, so its jobs wait less when the worker is busy. Without it, the priority is Medium.
A cluster can disable priorities it doesn't use (DisablePriority(...)). A handler whose priority is disabled on its cluster fails at startup, and so does a call passing a disabled priority. See Cluster Configuration.
Worker Lane
[JobMasterWorkerLane("reports")] sends the handler's jobs to the workers of that lane, and only to them. Without it, the jobs have no lane and run on workers without a lane. See Several Workers in One App.
Cluster
[JobMasterClusterId("billing-cluster")] sets the cluster the handler's jobs and recurring schedules go to, when the app is connected to more than one. Without it, the default cluster is used. Cluster ids ignore case.
The cluster must be configured in the app; otherwise startup fails with a validation error.
Metadata
[JobMasterMetadata("Category", "Reports")] adds a metadata key to every job of the handler. Use it once per key; the value can be a string, a number (int, long, short, byte, double), a bool or a char.
[JobMasterMetadata("Category", "Reports")]
[JobMasterMetadata("Team", "Finance")]
public sealed class MonthlyReportHandler : IJobMasterHandler { ... }
Metadata passed when scheduling overrides these keys one by one, as does a recurring schedule's own metadata; keys set in only one place are all kept. See Metadata.
Other Attributes
- Schedule attributes (
[NaturalCronSchedule],[TimeSpanIntervalSchedule],[CronosSchedule],[NCrontabSchedule]) run a handler on a schedule; see Recurring Jobs. JobDefinitionConfigAttributesets a handler's configuration from a definition shared with apps that don't reference the handler; see Publisher/Consumer Separation.
Inheritance
The attributes are inherited: put them on a base class to share settings across its handlers. A handler's own attribute overrides the base class's.
[JobMasterClusterId("billing-cluster")]
[JobMasterWorkerLane("billing")]
public abstract class BillingHandlerBase : IJobMasterHandler { ... }
public sealed class InvoiceHandler : BillingHandlerBase { ... } // billing-cluster, billing lane
[JobMasterWorkerLane("billing-reports")]
public sealed class ReportHandler : BillingHandlerBase { ... } // billing-cluster, billing-reports lane
[JobMasterMetadata] keys are merged across the hierarchy: a key on the handler overrides the same key on a base class, and keys set on only one class are all kept. The same key twice on one class is an error.
Dependency Injection
Handlers are created from your app's DI container, so inject what they need in the constructor. Each run gets its own DI scope: scoped services, such as an EF Core DbContext, are new for every job and disposed when it ends.
public sealed class InvoiceHandler(BillingDbContext db, IEmailService email) : IJobMasterHandler
{
public async Task HandleAsync(JobContext job)
{
var invoice = await db.Invoices.FindAsync(job.MsgData.GetLongValue("InvoiceId"));
await email.SendAsync(invoice!.CustomerEmail, "Your invoice is ready", job.CancellationToken);
}
}
JobMaster registers every handler as scoped. If you register a handler yourself (for example with another lifetime or a factory), your registration is kept.
Next
Custom JSON Serializer: change how SetJson / GetJson values are written and read.