### Install JobMaster MySQL Packages Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/Sql/JobMaster.MySql/JobMaster.MySql.NuGet.README.md Commands to install the core JobMaster library and the MySQL storage provider using the .NET CLI. ```bash dotnet add package JobMaster dotnet add package JobMaster.MySql ``` -------------------------------- ### Install JobMaster.Api NuGet Packages Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/JobMaster.Api/JobMaster.Api.NuGet.README.md Installs the JobMaster and JobMaster.Api NuGet packages required for the orchestrator service. ```bash dotnet add package JobMaster dotnet add package JobMaster.Api ``` -------------------------------- ### Install JobMaster SQL Server Packages Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/Sql/JobMaster.SqlServer/JobMaster.SqlServer.NuGet.README.md Commands to install the core JobMaster package and the SQL Server provider via the .NET CLI. ```bash dotnet add package JobMaster dotnet add package JobMaster.SqlServer ``` -------------------------------- ### Standalone Setup Configuration Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/README.md Configures JobMaster for a standalone setup, including database connection and worker attachment. ```APIDOC ## Standalone Setup Configuration ### Description Registers JobMaster in the `Program.cs` file, setting up the database connection and attaching a background worker for standalone operation. ### Method `builder.Services.AddJobMasterCluster()` ### Endpoint N/A (Configuration within `Program.cs`) ### Parameters #### Request Body - **config** (Action) - Configuration options for the JobMaster cluster. - **UseStandaloneCluster()** - Configures the cluster for standalone mode. - **ClusterId(string)** - Sets a unique identifier for the cluster. - **UsePostgres(string)** - Specifies the PostgreSQL connection string. - **AddWorker()** - Attaches a background worker to process jobs. ### Request Example ```csharp builder.Services.AddJobMasterCluster(config => { config.UseStandaloneCluster() .ClusterId("Local-Cluster-01") .UsePostgres("Host=localhost;Database=jobmaster_db;Username=postgres;Password=pwd") .AddWorker(); // Starts the worker to execute jobs. }); var app = builder.Build(); // Start the JobMaster runtime loops await app.Services.StartJobMasterRuntimeAsync(); ``` ### Response #### Success Response (200) N/A (Configuration step) #### Response Example N/A ``` -------------------------------- ### Install JobMaster via NuGet Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/JobMaster/JobMaster.NuGet.README.md Command to add the JobMaster package to your .NET project. ```bash dotnet add package JobMaster ``` -------------------------------- ### Install JobMaster.Postgres Package Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/Sql/JobMaster.Postgres/JobMaster.Postgress.NuGet.README.md Installs the necessary JobMaster and JobMaster.Postgres packages using the .NET CLI. These packages are essential for integrating PostgreSQL storage with the JobMaster .Net engine. ```bash dotnet add package JobMaster dotnet add package JobMaster.Postgres ``` -------------------------------- ### Configure JobMaster Cluster Settings (.NET) Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Configures the JobMaster cluster with essential settings like Cluster ID, database provider (PostgreSQL example), mode, timeouts, message size limits, and time zone. It also sets data retention policies and designates this configuration as the default. The code assumes a .NET application builder and starts the JobMaster runtime. ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("My-Production-Cluster") .UsePostgresForMaster("Host=localhost;Database=jobmaster;Username=postgres;Password=secret") .ClusterMode(ClusterMode.Active) .ClusterTransientThreshold(TimeSpan.FromMinutes(20)) .ClusterDefaultJobTimeout(TimeSpan.FromMinutes(5)) .ClusterDefaultMaxRetryCount(3) .ClusterMaxMessageByteSize(256 * 1024) .ClusterIanaTimeZoneId("America/New_York") .DataRetentionTtl(TimeSpan.FromDays(30)) .SetAsDefault(); }); var app = builder.Build(); // Start the JobMaster runtime loops await app.Services.StartJobMasterRuntimeAsync(); ``` -------------------------------- ### Install JobMaster and NatsJetStream Packages Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/JobMaster.NatsJetStream/JobMaster.NatsJetStream.NuGet.README.md Installs the necessary JobMaster and JobMaster.NatsJetStream packages using the .NET CLI. These packages are essential for setting up NATS JetStream as a transport layer. ```bash dotnet add package JobMaster dotnet add package JobMaster.NatsJetStream ``` -------------------------------- ### Worker Configuration Example Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/WorkersConfiguration.md This C# code snippet demonstrates how to configure a Worker in JobMaster, linking it to an Agent connection, setting its name, lane, batch size, and parallelism. ```APIDOC ## Worker Configuration Example ### Description This example shows how to configure a JobMaster worker using C#. ### Method Not Applicable (Configuration Code) ### Endpoint Not Applicable (Configuration Code) ### Parameters This is a configuration code snippet, not an API endpoint. ### Request Example ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("My-Cluster"); config.AddWorker() .AgentConnName("Postgres-1") // Links to an Agent connection .WorkerName("Payroll-Worker-01") // Unique name for this instance .WorkerLane("Payroll") // Logical isolation lane .WorkerBatchSize(1000) // Jobs to fetch per DB round-trip and also onboarding list of a bucket (30 seconds to be processed) .ParallelismFactor(2) // Scaler for concurrent execution .SetWorkerMode(AgentWorkerMode.Full) .BucketQtyConfig(JobMasterPriority.Critical, 3); }); ``` ### Response Not Applicable (Configuration Code) #### Success Response (200) Not Applicable (Configuration Code) #### Response Example Not Applicable (Configuration Code) ``` -------------------------------- ### Set Up Standalone JobMaster Cluster (.NET) Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Sets up a JobMaster cluster in standalone mode, ideal for simple deployments. It uses a single PostgreSQL database connection for both coordination and storage, eliminating the need for external message brokers. This configuration also includes adding a worker to execute jobs and starts the JobMaster runtime. ```csharp builder.Services.AddJobMasterCluster(config => { config.UseStandaloneCluster() .ClusterId("Local-Cluster-01") .UsePostgres("Host=localhost;Database=jobmaster_db;Username=postgres;Password=pwd") .AddWorker(); // Starts the worker to execute jobs }); var app = builder.Build(); await app.Services.StartJobMasterRuntimeAsync(); ``` -------------------------------- ### Configure PostgreSQL Transport Provider Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Providers.md Installs and configures the PostgreSQL provider for JobMaster. It requires the JobMaster.Postgres package and connection strings for both master and agent databases. ```bash dotnet add package JobMaster --version 0.0.2-alpha dotnet add package JobMaster.Postgres --version 0.0.1-alpha ``` ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .ClusterTransientThreshold(TimeSpan.FromMinutes(1)) .ClusterMode(ClusterMode.Active); config.UsePostgresForMaster("Host=..."); config.AddAgentConnectionConfig("Postgres-1") .UsePostgresForAgent("Host=..."); }); ``` -------------------------------- ### Postgres Transport Provider Configuration Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Providers.md Configuration for using Postgres as a transport provider for JobMaster, including installation and setup instructions. ```APIDOC ## Postgres Transport Provider ### Description Configures JobMaster to use Postgres for both the master database and agent transport, offering transactional semantics with good throughput and simple operations. ### Method Configuration via `builder.Services.AddJobMasterCluster` ### Endpoint N/A (Configuration within application setup) ### Parameters N/A ### Request Example ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .ClusterTransientThreshold(TimeSpan.FromMinutes(1)) .ClusterMode(ClusterMode.Active); config.UsePostgresForMaster("Host=..."); config.AddAgentConnectionConfig("Postgres-1") .UsePostgresForAgent("Host=..."); }); ``` ### Response N/A (Configuration step) ### Notes - Ensure sufficient connection pool. - Prefer UUID support where available. ``` -------------------------------- ### Configure MySQL Transport Provider Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Providers.md Installs and configures the MySQL provider for JobMaster. Ensure connection pooling is tuned and UseAffectedRows is enabled. ```bash dotnet add package JobMaster --version 0.0.2-alpha dotnet add package JobMaster.MySql --version 0.0.1-alpha ``` ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .ClusterTransientThreshold(TimeSpan.FromMinutes(1)) .ClusterMode(ClusterMode.Active); config.UseMySqlForMaster("Server=...;Database=...;User Id=...;Password=...;TrustServerCertificate=True;"); config.AddAgentConnectionConfig("MySql-1") .UseMySqlForAgent("Server=...;Database=...;User Id=...;Password=...;TrustServerCertificate=True;"); }); ``` -------------------------------- ### Configure SQL Server Transport Provider Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Providers.md Installs and configures the SQL Server provider for JobMaster. Suitable for environments where SQL Server is the primary database infrastructure. ```bash dotnet add package JobMaster --version 0.0.2-alpha dotnet add package JobMaster.SqlServer --version 0.0.1-alpha ``` ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .ClusterTransientThreshold(TimeSpan.FromMinutes(1)) .ClusterMode(ClusterMode.Active); config.UseSqlServerForMaster("Server=...;Database=...;User Id=...;Password=...;TrustServerCertificate=True;"); config.AddAgentConnectionConfig("SqlServer-1") .UseSqlServerForAgent("Server=...;Database=...;User Id=...;Password=...;TrustServerCertificate=True;"); }); ``` -------------------------------- ### Configure JobMaster with NATS JetStream Transport Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/JobMaster.NatsJetStream/JobMaster.NatsJetStream.NuGet.README.md Configures the JobMaster .Net cluster to use NATS JetStream for the Agent's transport layer. This example shows setting up a database for the Master and connecting to a NATS JetStream instance for the Agent, along with attaching a worker. ```csharp builder.Services.AddJobMasterCluster(config => { // Master MUST use a database provider (Postgres, SQL Server, or MySQL) config.ClusterId("Production-Cluster") .UsePostgresForMaster("Your_Database_Connection_String"); // Agent can use NATS JetStream for high-performance message transport config.AddAgentConnectionConfig("NATS-Transport") .UseNatsJetStreamForAgent("nats://localhost:4222"); // Attach a worker to the NATS transport config.AddWorker() .AgentConnName("NATS-Transport"); }); ``` -------------------------------- ### MySQL Transport Provider Configuration Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Providers.md Configuration for using MySQL as a transport provider for JobMaster, including installation and setup instructions. ```APIDOC ## MySQL Transport Provider ### Description Configures JobMaster to use MySQL-compatible engines for both the master database and agent transport, suitable when your infrastructure standardizes on MySQL. ### Method Configuration via `builder.Services.AddJobMasterCluster` ### Endpoint N/A (Configuration within application setup) ### Parameters N/A ### Request Example ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .ClusterTransientThreshold(TimeSpan.FromMinutes(1)) .ClusterMode(ClusterMode.Active); config.UseMySqlForMaster("Server=...;Database=...;User Id=...;Password=...;TrustServerCertificate=True;"); // Agent connection config.AddAgentConnectionConfig("MySql-1") .UseMySqlForAgent("Server=...;Database=...;User Id=...;Password=...;TrustServerCertificate=True;"); }); ``` ### Response N/A (Configuration step) ### Notes - Tune `innodb_buffer_pool_size` and connection pooling. - Use `UseAffectedRows=True`. ``` -------------------------------- ### SQL Server Transport Provider Configuration Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Providers.md Configuration for using SQL Server as a transport provider for JobMaster, including installation and setup instructions. ```APIDOC ## SQL Server Transport Provider ### Description Configures JobMaster to use SQL Server for both the master database and agent transport, ideal for Microsoft-centric environments or when SQL Server features are required. ### Method Configuration via `builder.Services.AddJobMasterCluster` ### Endpoint N/A (Configuration within application setup) ### Parameters N/A ### Request Example ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .ClusterTransientThreshold(TimeSpan.FromMinutes(1)) .ClusterMode(ClusterMode.Active); config.UseSqlServerForMaster("Server=...;Database=...;User Id=...;Password=...;TrustServerCertificate=True;"); // Agent connection config.AddAgentConnectionConfig("SqlServer-1") .UseSqlServerForAgent("Server=...;Database=...;User Id=...;Password=...;TrustServerCertificate=True;"); }); ``` ### Response N/A (Configuration step) ``` -------------------------------- ### Implementing a Job Handler Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/README.md Demonstrates how to create a basic job handler by implementing the `IJobHandler` interface. ```APIDOC ## Implementing a Job Handler ### Description Defines a class that implements the `IJobHandler` interface to encapsulate the logic for a background job. The `HandleAsync` method is the entry point for job execution. ### Method `IJobHandler.HandleAsync(JobContext job)` ### Endpoint N/A (Handler implementation) ### Parameters #### Request Body - **job** (JobContext) - Provides metadata and data payload for the current job execution. - **job.Id** (Guid) - The unique identifier of the job. - **job.MsgData** (MessageData) - The data payload (arguments) sent to the job. Use `GetStringValue`, `GetIntValue`, etc. to retrieve data. - **job.Metadata** (MessageData) - Non-business data like correlation IDs. ### Request Example ```csharp using JobMaster.Sdk.Abstractions.Models; public class ProcessImageHandler : IJobHandler { public async Task HandleAsync(JobContext job) { // 1. Retrieve data sent during scheduling var imageUrl = job.MsgData.GetStringValue("SourceUrl"); var filterType = job.MsgData.GetStringValue("Filter"); Console.WriteLine($"[Job {job.Id}] Processing image: {imageUrl} with {filterType}"); // 2. Perform your business logic await Task.Delay(500); // Simulating work // 3. Handlers are async-ready await Task.CompletedTask; } } ``` ### Response #### Success Response (200) N/A (Handler execution) #### Response Example N/A ``` -------------------------------- ### Configure Multi-Agent Database Connections Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Demonstrates how to distribute job storage across multiple database providers including PostgreSQL, SQL Server, MySQL, and NATS JetStream within a single cluster configuration. ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Multi-Agent-Cluster").UsePostgresForMaster("Host=master-db;Database=jobmaster;..."); config.AddAgentConnectionConfig("Postgres-1").UsePostgresForAgent("Host=agent-db-1;Database=jobs;..."); config.AddAgentConnectionConfig("SqlServer-1").UseSqlServerForAgent("Server=agent-db-2;Database=jobs;..."); config.AddAgentConnectionConfig("MySql-1").UseMySqlForAgent("Server=agent-db-3;Database=jobs;..."); config.AddAgentConnectionConfig("Nats-1").UseNatsJetStream("nats://localhost:4222"); config.AddWorker().AgentConnName("Postgres-1"); config.AddWorker().AgentConnName("SqlServer-1"); config.AddWorker().AgentConnName("Nats-1"); }); ``` -------------------------------- ### Configure Multiple JobMaster Clusters in C# Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Demonstrates how to register and configure multiple JobMaster clusters using the .NET SDK. This includes setting up database connections for masters and agents, defining workers, and specifying a default cluster. It also shows how to schedule jobs to either the default cluster or a specific named cluster. ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Sales-Cluster") .UsePostgresForMaster(salesConnectionString) .SetAsDefault(); // Default cluster for scheduling config.AddAgentConnectionConfig("Sales-Agent") .UsePostgresForAgent(salesAgentConnectionString); config.AddWorker().AgentConnName("Sales-Agent"); }); builder.Services.AddJobMasterCluster(config => { config.ClusterId("Analytics-Cluster") .UsePostgresForMaster(analyticsConnectionString); config.AddAgentConnectionConfig("Analytics-Agent") .UsePostgresForAgent(analyticsAgentConnectionString); config.AddWorker().AgentConnName("Analytics-Agent"); }); // Schedule to specific clusters app.MapPost("/process-sale", async (IJobMasterScheduler scheduler) => { var msg = WriteableMessageData.New().SetGuidValue("SaleId", Guid.NewGuid()); // Uses default cluster (Sales-Cluster) await scheduler.OnceNowAsync(msg); return Results.Accepted(); }); app.MapPost("/generate-report", async (IJobMasterScheduler scheduler) => { var msg = WriteableMessageData.New().SetStringValue("ReportType", "Weekly"); // Explicitly target Analytics cluster await scheduler.OnceNowAsync(msg, clusterId: "Analytics-Cluster"); return Results.Accepted(); }); ``` -------------------------------- ### Configure JobMaster Services in Program.cs Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/README.md Registers JobMaster services, sets up cluster identity, and configures storage providers using a fluent API. This includes defining the main cluster database and multiple agent connections for different storage types. It also shows how to attach workers to specific agent connections. ```csharp builder.Services.AddJobMasterCluster(config => { // Configure the main cluster database config.ClusterId("Cluster-1") .UsePostgresForMaster("[master-connection-string]"); // Define agent connections config.AddAgentConnectionConfig("Postgres-1") .UsePostgresForAgent("[agent-connection-string]"); config.AddAgentConnectionConfig("SqlServer-1") .UseSqlServerForAgent("[agent-connection-string]"); config.AddAgentConnectionConfig("Nats-1") .UseNatsJetStream("[agent-connection-string]"); /// ... Many more agents as needed // Attach a worker to a specific connection config.AddWorker() .AgentConnName("Postgres-1"); config.AddWorker() .AgentConnName("SqlServer-1"); config.AddWorker() .AgentConnName("Nats-1"); }); // Start the runtime await app.Services.StartJobMasterRuntimeAsync(); ``` -------------------------------- ### Configure SQL Provider Table Prefixes Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Providers.md Demonstrates how to customize table prefixes for SQL providers and disable automatic schema provisioning. ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .ClusterTransientThreshold(TimeSpan.FromMinutes(1)) .ClusterMode(ClusterMode.Active); config.UseSqlTablePrefixForMaster("jm_custom_") .DisableAutoProvisionSqlSchema(); config.AddAgentConnectionConfig("Pg-1") .UseSqlTablePrefixForAgent("jm_agent_"); }); ``` -------------------------------- ### Configure Producer-Only Instance Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/AgentsConfiguration.md Sets up an instance to schedule work without processing it. By omitting the worker configuration, the instance acts as a lightweight API or web server. ```csharp config.AddAgentConnectionConfig("Postgres-1") .UsePostgresForAgent(connectionString); ``` -------------------------------- ### GET /job/context Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Retrieves job metadata and payload data within a job handler using the JobContext interface. ```APIDOC ## GET /job/context ### Description Accesses job metadata (ID, ClusterId, Priority) and payload data (MsgData) within a registered IJobHandler implementation. ### Method GET ### Parameters #### Path Parameters - **jobId** (Guid) - Required - The unique identifier of the job being processed. ### Response #### Success Response (200) - **Id** (Guid) - Unique job identifier - **ClusterId** (string) - Cluster owning the job - **MsgData** (object) - Strongly-typed job payload data - **Metadata** (object) - Custom job metadata #### Response Example { "Id": "550e8400-e29b-41d4-a716-446655440000", "ClusterId": "production-cluster", "MsgData": { "OrderId": "a1b2c3d4-e5f6-7890-abcd-ef1234567890", "CustomerEmail": "user@example.com" } } ``` -------------------------------- ### Configure Master and Agent MySQL Storage Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/Sql/JobMaster.MySql/JobMaster.MySql.NuGet.README.md Demonstrates how to register the MySQL storage provider for the Master coordination layer and the Agent transport layer within the .NET dependency injection container. ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Production-Cluster") .UseMySqlForMaster("Your_Connection_String"); }); config.AddAgentConnectionConfig("Sql-Transport") .UseMySqlForAgent("Your_Connection_String"); config.AddWorker() .AgentConnName("Sql-Transport"); ``` -------------------------------- ### Configure JobMaster Cluster and Bucket Quantities Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/BucketsConfiguration.md Demonstrates how to register the JobMaster cluster service and define the number of buckets per priority level. This configuration determines the level of parallelism for the worker cluster. ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("My-Cluster"); config.AddWorker() .AgentConnName("MyPostgresAgent-1") .BucketQtyConfig(JobMasterPriority.VeryLow, 1) .BucketQtyConfig(JobMasterPriority.Low, 2) .BucketQtyConfig(JobMasterPriority.Medium, 3) .BucketQtyConfig(JobMasterPriority.High, 4) .BucketQtyConfig(JobMasterPriority.Critical, 5); }); ``` -------------------------------- ### Configure NATS JetStream Transport Provider Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Providers.md Installs and configures NATS JetStream for high-performance, low-latency messaging. Requires specific transient threshold settings. ```bash dotnet add package JobMaster --version 0.0.2-alpha dotnet add package JobMaster.NatsJetStream --version 0.0.4-alpha ``` ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .ClusterTransientThreshold(TimeSpan.FromMinutes(1)) .ClusterMode(ClusterMode.Active); config.AddAgentConnectionConfig("Nats-1") .UseNatsJetStream("nats://localhost:4222"); }); ``` -------------------------------- ### Implement Basic Job Handler (IJobHandler Interface) (.NET) Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Demonstrates how to implement a basic job handler by inheriting from the `IJobHandler` interface in .NET. The `HandleAsync` method contains the business logic for processing a job, accessing job data like image URL, filter type, and user ID from the `JobContext`. ```csharp using JobMaster.Abstractions; using JobMaster.Abstractions.Models; // Basic handler implementation public class ProcessImageHandler : IJobHandler { public async Task HandleAsync(JobContext job) { // Retrieve data sent during scheduling var imageUrl = job.MsgData.GetStringValue("SourceUrl"); var filterType = job.MsgData.GetStringValue("Filter"); var userId = job.MsgData.GetGuidValue("UserId"); Console.WriteLine($"[Job {job.Id}] Processing image: {imageUrl} with {filterType}"); // Perform business logic await ProcessImageAsync(imageUrl, filterType); } } ``` -------------------------------- ### Schedule a Job from a Minimal API Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/README.md Demonstrates how to schedule a job for immediate execution using the IJobMasterScheduler. This involves injecting the scheduler into an API endpoint and calling the OnceNowAsync method with the job handler type and message data. ```csharp app.MapPost("/schedule-job", async (IJobMasterScheduler jobScheduler) => { // Build a fluent message data object var msg = WriteableMessageData.New().SetStringValue("Name", "John Doe"); // Enqueue the job for immediate execution await jobScheduler.OnceNowAsync(msg); return Results.Accepted(); }).WithOpenApi(); ``` -------------------------------- ### Configure JobMaster Cluster in C# Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/ClusterConfiguration.md This snippet demonstrates how to initialize the JobMaster cluster using the service collection builder. It configures essential parameters such as the database connection, cluster mode, transient thresholds, and job execution policies. ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("My-Cluster") .UsePostgresForMaster("Host=localhost;Database=jobmaster;...") .ClusterMode(ClusterMode.Active) .ClusterTransientThreshold(TimeSpan.FromMinutes(20)) .ClusterDefaultJobTimeout(TimeSpan.FromMinutes(1)) .ClusterDefaultMaxRetryCount(3) .ClusterMaxMessageByteSize(256 * 1024) .ClusterIanaTimeZoneId("America/Sao_Paulo") .SetAsDefault(); }); ``` -------------------------------- ### Configure API Key Authentication Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/ApiConfiguration.md Sets up API Key authentication for server-to-server communication, allowing custom header names and multiple key registrations. ```csharp builder.Services.UseJobMasterApi(o => { o.UseApiKeyAuth() .ApiKeyHeader("x-api-key") .AddApiKey("Grafana-Monitor", "secure-key-123") .AddApiKey("Admin-Tool", "another-secure-key"); }); ``` -------------------------------- ### Scheduling a Job from a Minimal API Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/README.md Shows how to schedule a job using `IJobMasterScheduler` within a .NET Minimal API endpoint. ```APIDOC ## Schedule from a Minimal API ### Description Injects the `IJobMasterScheduler` into a Minimal API endpoint to enqueue jobs for immediate or scheduled execution. This allows triggering background tasks directly from web requests. ### Method `POST /schedule-job` ### Endpoint `/schedule-job` ### Parameters #### Query Parameters N/A #### Request Body N/A (Job data is passed programmatically) ### Request Example ```csharp app.MapPost("/schedule-job", async (IJobMasterScheduler jobScheduler) => { // Build a fluent message data object var msg = WriteableMessageData.New().SetStringValue("Name", "John Doe"); // Enqueue the job for immediate execution await jobScheduler.OnceNowAsync(msg); return Results.Accepted(); }).WithOpenApi(); ``` ### Response #### Success Response (202) - **202 Accepted** - Indicates the job has been successfully enqueued. #### Response Example N/A (Typically returns an empty 202 Accepted response) ``` -------------------------------- ### Schedule One-Off Jobs Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Demonstrates how to schedule jobs for immediate, delayed, or future execution using the IJobMasterScheduler. It also shows how to override handler attributes dynamically at the time of scheduling. ```csharp app.MapPost("/schedule-job", async (IJobMasterScheduler scheduler) => { var msg = WriteableMessageData.New() .SetStringValue("Name", "John Doe") .SetIntValue("Tries", 1) .SetGuidValue("UserId", Guid.NewGuid()) .SetDecimalValue("Amount", 99.99m) .SetJson("OrderDetails", new { Items = 3, Discount = 0.1 }); await scheduler.OnceNowAsync(msg); return Results.Accepted(); }); app.MapPost("/schedule-future-job", async (IJobMasterScheduler scheduler) => { var msg = WriteableMessageData.New().SetStringValue("ReportType", "Monthly").SetDateTimeValue("GeneratedAt", DateTime.UtcNow); var runAt = DateTime.UtcNow.AddMinutes(5); await scheduler.OnceAtAsync(runAt, msg); await scheduler.OnceAfterAsync(TimeSpan.FromHours(1), msg); return Results.Accepted(); }); app.MapPost("/schedule-priority-job", async (IJobMasterScheduler scheduler) => { var msg = WriteableMessageData.New().SetStringValue("Task", "Urgent"); var customMeta = WritableMetadata.New().SetStringValue("Source", "WebAPI"); await scheduler.OnceNowAsync(msg, priority: JobMasterPriority.Critical, workerLane: "HighPriority", timeout: TimeSpan.FromMinutes(10), maxNumberOfRetries: 5, metadata: customMeta); return Results.Accepted(); }); ``` -------------------------------- ### Configure JobMaster Cluster with SQL Server Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/Sql/JobMaster.SqlServer/JobMaster.SqlServer.NuGet.README.md Registers the SQL Server storage backend for the Master coordination layer within the application's service configuration. ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Production-Cluster") .UseSqlServerForMaster("Your_Connection_String"); }); ``` -------------------------------- ### Configure Job Behavior with Attributes Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Scheduling.md Illustrates various attributes that can be applied to a job handler class to configure its behavior, such as unique identity, timeout, retry count, priority, worker lane, and metadata. ```csharp [JobMasterDefinitionId("HelloJob")] public sealed class HelloJobHandler : IJobHandler ``` ```csharp [JobMasterTimeout(10)] public sealed class HelloJobHandler : IJobHandler ``` ```csharp [JobMasterMaxNumberOfRetries(3)] public sealed class HelloJobHandler : IJobHandler ``` ```csharp [JobMasterPriority(JobMasterPriority.Low)] public sealed class HelloJobHandler : IJobHandler ``` ```csharp [JobMasterWorkerLane("PaymentsProcessing")] public sealed class HelloJobHandler : IJobHandler ``` ```csharp [JobMasterMetadata("Category", "Payroll")] public sealed class HelloJobHandler : IJobHandler ``` -------------------------------- ### Implement Job Handler Interface Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Scheduling.md Demonstrates how to implement the IJobHandler interface for processing work. It uses IServiceProvider for dependency injection, so all dependencies must be registered in the DI container. ```csharp public sealed class HelloJobHandler : IJobHandler { public Task HandleAsync(JobContext job) { var name = ctx.MsgData.TryGetStringValue("Name") ?? "World"; Console.WriteLine($"Hello {name}"); await Task.CompletedTask; } } ``` -------------------------------- ### Configure and Map JobMaster.Api Service Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/JobMaster.Api/JobMaster.Api.NuGet.README.md Configures the JobMaster.Api service during application startup by registering it with the dependency injection container and mapping its endpoints. Options include setting the base path, enabling authentication, Swagger UI, and logging. ```csharp builder.Services.UseJobMasterApi(o => { o.BasePath = "/jm-api"; // Base route for all JobMaster endpoints o.RequireAuthentication = true; // Global toggle for security o.EnableSwagger = true; // Enables isolated Swagger UI o.EnableLogging = true; // Logs API requests to the Cluster Logger }); var app = builder.Build(); // This maps the endpoints (e.g., /jm-api/my-cluster/jobs) app.MapJobMasterApi(); ``` -------------------------------- ### Configure User and Password Authentication Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/ApiConfiguration.md Enables credential-based authentication using PBKDF2 (SHA256) encryption for user validation. ```csharp builder.Services.UseJobMasterApi(o => { o.UseUserPwdAuth() .UserNameHeaderName("X-User-Name") .PwdHeaderName("X-Password") .AddUserPwd("hugo", "p@ssword_master"); }); ``` -------------------------------- ### Configure and Map JobMaster API Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/ApiConfiguration.md Initializes the JobMaster API service with custom settings and maps the required endpoints to the application pipeline. ```csharp builder.Services.UseJobMasterApi(o => { o.BasePath = "/jm-api"; o.RequireAuthentication = true; o.EnableSwagger = true; o.EnableLogging = true; }); var app = builder.Build(); app.MapJobMasterApi(); ``` -------------------------------- ### Configure Database Providers and Table Prefixes in JobMaster .NET Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Configure different database providers (PostgreSQL, MySQL, SQL Server) for Master and Agent storage layers. Custom table prefixes can be applied to organize database schemas. Auto-provisioning of SQL schema can also be disabled. ```csharp // PostgreSQL setup builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .ClusterTransientThreshold(TimeSpan.FromMinutes(1)) .ClusterMode(ClusterMode.Active) .UsePostgresForMaster("Host=localhost;Database=master;...") .UseSqlTablePrefixForMaster("jm_master_"); config.AddAgentConnectionConfig("Postgres-1") .UsePostgresForAgent("Host=localhost;Database=agent;...") .UseSqlTablePrefixForAgent("jm_agent_"); }); // MySQL setup builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .UseMySqlForMaster("Server=localhost;Database=master;User Id=root;Password=secret;"); config.AddAgentConnectionConfig("MySql-1") .UseMySqlForAgent("Server=localhost;Database=agent;User Id=root;Password=secret;"); }); // SQL Server setup builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .UseSqlServerForMaster("Server=localhost;Database=master;User Id=sa;Password=secret;TrustServerCertificate=True;"); config.AddAgentConnectionConfig("SqlServer-1") .UseSqlServerForAgent("Server=localhost;Database=agent;User Id=sa;Password=secret;TrustServerCertificate=True;"); }); // Disable auto-provisioning of SQL schema (for managed environments) builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .DisableAutoProvisionSqlSchema() .UsePostgresForMaster(connectionString); }); ``` -------------------------------- ### Implement Job Handler with Dependency Injection (.NET) Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Shows how to implement a job handler in .NET that utilizes dependency injection. The handler, `NotificationHandler`, injects services like `IEmailService` and `ILogger` through its constructor. The `HandleAsync` method uses these injected services to send an email based on job data. ```csharp using JobMaster.Abstractions; using JobMaster.Abstractions.Models; // Handler with dependency injection public class NotificationHandler : IJobHandler { private readonly IEmailService _emailService; private readonly ILogger _logger; public NotificationHandler(IEmailService emailService, ILogger logger) { _emailService = emailService; _logger = logger; } public async Task HandleAsync(JobContext job) { var email = job.MsgData.GetStringValue("UserEmail"); var subject = job.MsgData.GetStringValue("Subject"); _logger.LogInformation("Sending email to {Email}", email); await _emailService.SendAsync(email, subject, "Your report is ready!"); } } ``` -------------------------------- ### SQL Providers - Table Prefix and Schema Provisioning Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Providers.md Customizing SQL provider behavior in JobMaster, including table prefixes and disabling automatic schema provisioning. ```APIDOC ## SQL Providers - Customization ### Description Allows customization of SQL provider settings in JobMaster, such as setting table prefixes for master and agent databases, and disabling automatic SQL schema provisioning. ### Method Configuration via `builder.Services.AddJobMasterCluster` ### Endpoint N/A (Configuration within application setup) ### Parameters N/A ### Request Example ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Cluster-1") .ClusterTransientThreshold(TimeSpan.FromMinutes(1)) .ClusterMode(ClusterMode.Active); config.UseSqlTablePrefixForMaster("jm_custom_") .DisableAutoProvisionSqlSchema(); // Agent connection config.AddAgentConnectionConfig("Pg-1") .UseSqlTablePrefixForAgent("jm_agent_"); }); ``` ### Response N/A (Configuration step) ### Notes - Default table prefix is "jm_". - `DisableAutoProvisionSqlSchema()` skips table creation for the entire cluster. ``` -------------------------------- ### Configure JobMaster API with Authentication Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Configures the JobMaster REST API with options for authentication (API Key, User/Password, JWT Bearer), Swagger UI, and logging. Requires the JobMaster.Api NuGet package. ```csharp // Install packages: dotnet add package JobMaster.Api builder.Services.UseJobMasterApi(o => { o.BasePath = "/jm-api"; o.RequireAuthentication = true; o.EnableSwagger = true; o.EnableLogging = true; // API Key authentication o.UseApiKeyAuth() .ApiKeyHeader("x-api-key") .AddApiKey("Monitoring-Service", "secure-key-123") .AddApiKey("Admin-Dashboard", "another-secure-key"); // User/Password authentication o.UseUserPwdAuth() .UserNameHeaderName("X-User-Name") .PwdHeaderName("X-Password") .AddUserPwd("admin", "secure-password"); // JWT Bearer authentication o.UseJwtBearerAuth() .Scheme("Bearer") .AuthorizationHeaderName("Authorization") .RegisterDefaultJwtBearerAuthProvider(new TokenValidationParameters { ValidateIssuer = true, ValidIssuer = "https://your-auth-server.com", IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes("your-secret-key")) }); }); var app = builder.Build(); app.MapJobMasterApi(); // Swagger UI available at: http://localhost:5000/jm-api/swagger ``` -------------------------------- ### POST /process-sale (Default Cluster) Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Schedules a job to the default configured JobMaster cluster. ```APIDOC ## POST /process-sale ### Description Triggers a sale processing job using the default configured JobMaster cluster. ### Method POST ### Endpoint /process-sale ### Request Body - **SaleId** (Guid) - Required - Unique identifier for the sale transaction. ### Response #### Success Response (202) - **Status** (string) - Accepted #### Response Example { "status": "Accepted" } ``` -------------------------------- ### Configure Consumer-Only Instance Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/AgentsConfiguration.md Sets up a worker instance to process jobs from a specific agent connection. The worker is bound to the agent via the AgentConnName method. ```csharp config.AddAgentConnectionConfig("Postgres-1"); config.AddWorker() .AgentConnName("Postgres-1"); ``` -------------------------------- ### Configuring JobMaster API Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/ApiConfiguration.md How to register and map the JobMaster API services within your .NET application. ```APIDOC ## POST /api/configuration ### Description Configures the JobMaster API service during the application startup phase. ### Method POST ### Endpoint /jm-api (Configurable via BasePath) ### Parameters #### Request Body - **BasePath** (string) - Optional - The base route for all JobMaster endpoints. - **RequireAuthentication** (boolean) - Optional - Global toggle for security. - **EnableSwagger** (boolean) - Optional - Enables isolated Swagger UI. - **EnableLogging** (boolean) - Optional - Logs API requests to the Cluster Logger. ### Request Example { "BasePath": "/jm-api", "RequireAuthentication": true, "EnableSwagger": true, "EnableLogging": true } ### Response #### Success Response (200) - **status** (string) - Configuration applied successfully. ``` -------------------------------- ### Schedule One-Off Jobs with IJobMasterScheduler Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Scheduling.md Shows how to use the IJobMasterScheduler to enqueue jobs for immediate execution, a specific future time, or a relative time using async methods. ```csharp // 1. Schedule immediately var msg = WriteableMessageData.New() .SetStringValue("Name", "John Doe") .SetIntValue("Tries", 1); await jobMasterScheduler.OnceNowAsync(msg); // 2. Schedule for a specific DateTime var runAt = DateTime.UtcNow.AddMinutes(5); await jobMasterScheduler.OnceAtAsync(runAt, msg); // 3. Schedule using a relative time in the future (TimeSpan) await jobMasterScheduler.OnceAfterAsync(TimeSpan.FromMinutes(5), msg); ``` -------------------------------- ### Agent Connection Configuration Source: https://context7.com/hugoj0s3/jobmaster-net/llms.txt Configure multiple agent connections to distribute database load across different storage providers (PostgreSQL, MySQL, SQL Server, NATS JetStream). ```APIDOC ## Agent Connection Configuration Configure multiple agent connections to distribute database load across different storage providers (PostgreSQL, MySQL, SQL Server, NATS JetStream). ### Description This section explains how to configure different agent connections for JobMaster, supporting various database backends and message queues. ### Method Configuration within C# code. ### Endpoint N/A (Configuration) ### Parameters N/A ### Request Example ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Multi-Agent-Cluster") .UsePostgresForMaster("Host=master-db;Database=jobmaster;..."); // PostgreSQL agent for transactional workloads config.AddAgentConnectionConfig("Postgres-1") .UsePostgresForAgent("Host=agent-db-1;Database=jobs;..."); // SQL Server agent for Microsoft environments config.AddAgentConnectionConfig("SqlServer-1") .UseSqlServerForAgent("Server=agent-db-2;Database=jobs;..."); // MySQL agent config.AddAgentConnectionConfig("MySql-1") .UseMySqlForAgent("Server=agent-db-3;Database=jobs;..."); // NATS JetStream for ultra-low latency workloads config.AddAgentConnectionConfig("Nats-1") .UseNatsJetStream("nats://localhost:4222"); // Attach workers to specific agents config.AddWorker().AgentConnName("Postgres-1"); config.AddWorker().AgentConnName("SqlServer-1"); config.AddWorker().AgentConnName("Nats-1"); }); ``` ### Response N/A (Configuration) ``` -------------------------------- ### Configure Worker Parallelism Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/WorkersConfiguration.md Demonstrates how to set the parallelism factor for a worker in JobMaster. This configuration scales job priorities based on the provided factor. ```csharp config.AddWorker() .ParallelismFactor(2.0); ``` -------------------------------- ### Override Job Configuration at Runtime Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/docs/Scheduling.md Demonstrates how to override or add job configurations, such as priority and metadata, dynamically when scheduling a job using the IJobMasterScheduler. ```csharp // Overriding priority and metadata during scheduling var customMeta = WritableMetadata.New().SetStringValue("Source", "WebAPI"); await scheduler.OnceNowAsync( msg, priority: JobMasterPriority.Low, metadata: customMeta ); ``` -------------------------------- ### Schedule Recurring Jobs with NaturalCron Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/README.md Demonstrates how to schedule recurring jobs using the NaturalCron library. It shows two methods: using a fluent API to build the schedule and using a direct expression string. Both methods involve scheduling a specific job handler with data and metadata. ```csharp // FLuent build var schedule = NaturalCronBuilder.Every(1).Minutes().Build(); jobScheduler.Recurring(schedule, WriteableMessageData.New().SetStringValue("Name", Faker.Name.FullName()), metadata: WritableMetadata.New().SetStringValue("expression", expression), workerLane: lane); // Via expression string jobScheduler.Recurring(NaturalCronExprCompiler.TypeId, "every 1 minutes", WriteableMessageData.New().SetStringValue("Name", Faker.Name.FullName()), metadata: WritableMetadata.New().SetStringValue("expression", expression), workerLane: lane); ``` -------------------------------- ### Configure JobMaster Agent and Worker Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/Sql/JobMaster.SqlServer/JobMaster.SqlServer.NuGet.README.md Sets up the SQL Server transport for the Agent layer and attaches a worker to the specified transport connection. ```csharp config.AddAgentConnectionConfig("Sql-Transport") .UseSqlServerForAgent("Your_Connection_String"); config.AddWorker() .AgentConnName("Sql-Transport"); ``` -------------------------------- ### Implement Job Handler Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/JobMaster/JobMaster.NuGet.README.md Create a custom job handler by implementing the IJobHandler interface to define business logic for background tasks. ```csharp public class NotificationHandler : IJobHandler { public async Task HandleAsync(JobContext job) { var userId = job.MsgData.GetStringValue("UserId"); await Task.CompletedTask; } } ``` -------------------------------- ### Configure JobMaster Cluster Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/JobMaster/JobMaster.NuGet.README.md Register the JobMaster service in Program.cs by defining the master database and agent transport connections. ```csharp builder.Services.AddJobMasterCluster(config => { config.ClusterId("Production-Cluster") .UsePostgresForMaster(connectionString); config.AddAgentConnectionConfig("Transport-1") .UsePostgresForAgent(agentConnectionString); config.AddWorker() .AgentConnName("Transport-1"); }); await app.Services.StartJobMasterRuntimeAsync(); ``` -------------------------------- ### Schedule Immediate Jobs Source: https://github.com/hugoj0s3/jobmaster-net/blob/master/JobMaster/JobMaster.NuGet.README.md Use the IJobMasterScheduler to enqueue jobs for immediate execution within a Minimal API or service. ```csharp app.MapPost("/send-welcome", async (IJobMasterScheduler scheduler) => { var msg = WriteableMessageData.New().SetStringValue("UserId", "user_123"); await scheduler.OnceNowAsync(msg); return Results.Accepted(); }); ```