Getting started
MassiveJobs publishes jobs to RabbitMQ. Any app can publish, and every app that runs workers takes jobs from the same queues. This page gets one app doing both, then a second app sharing the work.
Requirements
Section titled “Requirements”- RabbitMQ 4.4 or later. Scheduled jobs and retries use the delayed delivery of quorum queues, new in 4.4. MassiveJobs refuses to connect to an older server.
- .NET 10 or 11, or anything that runs
netstandard2.0: .NET 8 and 9, and .NET Framework 4.6.2 or later. - A C# compiler from the .NET 8 SDK or Visual Studio 2022 17.8 on, in the projects that register jobs: MassiveJobs’ source generator runs there.
Your first job
Section titled “Your first job”-
Start RabbitMQ. In a container, with the management UI on localhost:15672 (user and password
guest):Terminal window docker run -d --name rabbit -p 5672:5672 -p 15672:15672 rabbitmq:4-management -
Create an app and add the package. A worker service here; an ASP.NET Core app works the same way.
Terminal window dotnet new worker -n Shop.Workercd Shop.Workerdotnet add package MassiveJobs.RabbitMqBrokerMassiveJobs.RabbitMqBrokerbringsMassiveJobs.Corewith it. -
Write a job. A job is a class with a
Performmethod. Derive it fromJob<TJob, TArgs>(orJobAsync<TJob, TArgs>for an async one), whereTArgsis what you pass when you publish it:// SendWelcomeEmail.csusing MassiveJobs.Core;public class SendWelcomeEmail(ILogger<SendWelcomeEmail> logger) : JobAsync<SendWelcomeEmail, int>{public override async Task Perform(int customerId, CancellationToken cancellationToken){await Task.Delay(500, cancellationToken); // the emaillogger.LogInformation("Welcome email sent to customer {CustomerId}", customerId);}}The constructor gets its parameters from your services, in a scope created for each run.
-
Register MassiveJobs. In
Program.cs, add MassiveJobs, the jobs of this project, and RabbitMQ:// Program.csvar builder = Host.CreateApplicationBuilder(args);builder.Services.AddMassiveJobs().WithJobsFromAssemblyOf<SendWelcomeEmail>().UseRabbitMq(); // localhost, as guestbuilder.Services.AddHostedService<Worker>();builder.Build().Run();The host starts the workers with the app and stops them with it.
-
Publish. Inject
IJobPublisherwhere you need it. In the template’sWorker, publish a job every few seconds:// Worker.csusing MassiveJobs.Core;public class Worker(IServiceScopeFactory scopes) : BackgroundService{protected override async Task ExecuteAsync(CancellationToken stoppingToken){for (var customerId = 1; !stoppingToken.IsCancellationRequested; customerId++){using (var scope = scopes.CreateScope()){var publisher = scope.ServiceProvider.GetRequiredService<IJobPublisher>();await publisher.PublishAsync<SendWelcomeEmail, int>(customerId, cancellationToken: stoppingToken);}await Task.Delay(2000, stoppingToken);}}}IJobPublisheris scoped: a controller or a minimal API handler just takes it as a parameter. -
Run it, twice.
dotnet runin two terminals. Both publish, and the jobs are shared: each one runs once, in either app.
What you get by default
Section titled “What you get by default”Each app that runs workers consumes 4 immediate queues, one job at a time each, and 2 long-running queues for jobs published with a timeout of 10 seconds or more. A job that throws is retried 25 times with growing delays, then kept in the failed queue. All of it is in Configuration; how the queues are shared is in Queues and workers.
Apps that share jobs must agree on a few things: the job tags, the queue counts, and the name prefix. The defaults agree.
Other kinds of apps
Section titled “Other kinds of apps”var builder = WebApplication.CreateBuilder(args);
builder.Services.AddMassiveJobs() .WithJobsFromAssemblyOf<Program>() .UseRabbitMq(o => o.ConnectionString = builder.Configuration.GetConnectionString("RabbitMq"));
var app = builder.Build();
app.MapPost("/customers/{id:int}/welcome", async (int id, IJobPublisher publisher) =>{ await publisher.PublishAsync<SendWelcomeEmail, int>(id); return Results.Accepted();});
app.Run();An app that publishes jobs but doesn’t run them, e.g. a web front end with separate worker apps:
builder.Services.AddMassiveJobs(o => o.StartWorkers = false) .WithJobsFromAssemblyOf<SendWelcomeEmail>() .UseRabbitMq(o => o.ConnectionString = builder.Configuration.GetConnectionString("RabbitMq"));It still registers the jobs: only registered jobs can be published.
A console app or a .NET Framework app without Microsoft.Extensions.Hosting starts MassiveJobs itself, once the service
provider is built:
var services = new ServiceCollection();services.AddLogging();services.AddMassiveJobs() .WithJobsFromAssemblyOf<SendWelcomeEmail>() .UseRabbitMq();
using var provider = services.BuildServiceProvider();
var jobs = MassiveJobsMediator.Initialize(provider); // checks the configuration, throws if it's wrongawait jobs.StartAsync();
await SendWelcomeEmail.PublishAsync(42); // the static API: no publisher to inject
Console.ReadLine();await jobs.StopAsync();MassiveJobsMediator.Deinitialize();- Jobs: job classes, arguments, tags, publishing, timeouts.
- Guarantees: when a job can run twice, and what to do about it.
- Scheduling and Periodic jobs.