Worker Basics
A worker is the part of your app that picks up jobs and runs your handlers. .AddWorker() in the cluster registration from Getting Started turns one on in that process.
An app without a worker can still schedule jobs, but it never runs them. Nothing warns you: the jobs wait until a worker of the same cluster runs.
Producers and Consumers
An app doesn't have to do both:
- Producer only: for example your app's REST API, which only produces jobs. Register the cluster without
AddWorker(). - Consumer: for example a worker service, which runs the jobs. Register the cluster with
AddWorker().
Both use the same cluster id and database:
// REST API (producer only): schedules jobs, never runs them.
builder.Services.AddJobMasterCluster(config =>
{
config.UseStandaloneCluster()
.ClusterId("Local-Cluster-01")
// provider connection....
});
// Worker service (consumer): runs the jobs.
builder.Services.AddJobMasterCluster(config =>
{
config.UseStandaloneCluster()
.ClusterId("Local-Cluster-01")
// provider connection....
.AddWorker();
});
One app can also be both, as in Getting Started: it schedules jobs and its worker runs them.
Naming a Worker
Give a worker a name to recognize it on the dashboard and in the logs:
.AddWorker("billing-worker")
- Letters, numbers,
-and_, at most 25 characters. - JobMaster adds a suffix at every start, for example
billing-worker-3c1a8b2, so each restart shows up as a new worker. - Without a name, the machine name is used, with the same suffix.
Several Workers in One App
An app can have more than one worker. This makes sense with worker lanes.
A worker lane is a logical separation: jobs with a lane run only on workers with that lane, which are dedicated to them. It's mainly used for slow jobs, such as data uploads or report processing, so they don't hold up the other jobs:
.AddWorker("main-worker") // jobs without a lane
.AddWorker("reports-worker", workerLane: "reports") // only jobs with the "reports" lane
On the handler side, set the lane with an attribute, so every job of that handler goes to the lane:
[JobMasterWorkerLane("reports")]
public sealed class MonthlyReportHandler : IJobMasterHandler
{
public async Task HandleAsync(JobContext job) { ... }
}
Or pass it on the call for a single job: OnceNowAsync<ExportHandler>(msg, workerLane: "reports").
A job with a lane runs only on a worker with that lane, so make sure one runs somewhere in the cluster.
Running Several Instances
Run the same app on several machines, each with a worker, and they share the jobs. Each job runs on one of them; as with any job, at least once.
When a Worker Stops
On a normal shutdown, the worker hands its jobs back and another worker continues them. If the process crashes, its jobs are picked up by another worker within a few minutes. Either way, no job is lost.