### Install Hangfire.MaximumConcurrentExecutions NuGet Package Source: https://github.com/alastairtree/hangfire.maximumconcurrentexecutions/blob/master/README.md This command installs the Hangfire.MaximumConcurrentExecutions NuGet package into your project using the Package Manager Console. This package provides the necessary attribute for throttling job executions. ```powershell PM> Install-Package Hangfire.MaximumConcurrentExecutions ``` -------------------------------- ### Enqueue a Job in Hangfire (C#) Source: https://github.com/alastairtree/hangfire.maximumconcurrentexecutions/blob/master/README.md Shows the standard method for enqueuing a job in Hangfire using the `BackgroundJob.Enqueue` method. This example enqueues an instance of `ExampleJob` and calls its `SomeLongRunningActivity` method. ```csharp BackgroundJob.Enqueue(job => job.SomeLongRunningActivity()); ``` -------------------------------- ### ASP.NET Core Integration with Hangfire and MaximumConcurrentExecutions (C#) Source: https://context7.com/alastairtree/hangfire.maximumconcurrentexecutions/llms.txt Shows how to integrate Hangfire with ASP.NET Core using SQL Server storage and apply the `[MaximumConcurrentExecutions]` attribute to a job. The setup involves configuring Hangfire in `Startup.cs` and decorating a job method in `ExampleJob.cs`. A controller is included to demonstrate enqueuing jobs. ```csharp // Program.cs using Microsoft.AspNetCore.Hosting; public class Program { public static void Main(string[] args) { var host = new WebHostBuilder() .UseKestrel() .UseStartup() .Build(); host.Run(); } } ``` ```csharp // Startup.cs using Hangfire; using Microsoft.AspNetCore.Builder; using Microsoft.Extensions.DependencyInjection; public class Startup { public void ConfigureServices(IServiceCollection services) { // Configure Hangfire with SQL Server storage services.AddHangfire(config => config.UseSqlServerStorage("Server=localhost;Database=HangfireDb;Integrated Security=true;")); services.AddMvc(); } public void Configure(IApplicationBuilder app) { // Enable Hangfire Dashboard at /hangfire app.UseHangfireDashboard(); // Start the Hangfire background job server app.UseHangfireServer(); app.UseMvc(); } } ``` ```csharp // ExampleJob.cs - No special configuration needed, just use the attribute using Hangfire; using System; using System.Threading.Tasks; public class ExampleJob { [MaximumConcurrentExecutions(3)] public void ProcessOrder(int orderId) { Console.WriteLine($"Processing order {orderId}"); Task.Delay(TimeSpan.FromSeconds(5)).Wait(); Console.WriteLine($"Order {orderId} processed"); } } ``` ```csharp // Controller to enqueue jobs using Hangfire; using Microsoft.AspNetCore.Mvc; using System.Linq; public class OrdersController : Controller { [HttpPost("api/orders/process-batch")] public IActionResult ProcessBatch() { // Enqueue 20 orders - only 3 will process concurrently foreach (var orderId in Enumerable.Range(1, 20)) { BackgroundJob.Enqueue(job => job.ProcessOrder(orderId)); } return Ok("20 orders queued for processing (max 3 concurrent)"); } } ``` -------------------------------- ### Configure MaximumConcurrentExecutions with Timeout (C#) Source: https://github.com/alastairtree/hangfire.maximumconcurrentexecutions/blob/master/README.md Illustrates how to configure the `MaximumConcurrentExecutions` attribute with a custom timeout. This example sets the maximum concurrent executions to 3 and the timeout to 120 seconds (2 minutes). ```csharp public class ExampleJob { [MaximumConcurrentExecutions(3, timeoutInSeconds = 120)] public void SomeLongRunningActivity() { ... } } ``` -------------------------------- ### Configure Job Concurrency with MaximumConcurrentExecutions (C#) Source: https://context7.com/alastairtree/hangfire.maximumconcurrentexecutions/llms.txt Demonstrates how to use the `[MaximumConcurrentExecutions]` attribute in C# to limit concurrent job executions. It shows how to set the maximum number of concurrent jobs, a timeout for waiting for a slot, and the polling interval for checking available slots. This example simulates a long-running export job. ```csharp using Hangfire; using System; using System.Threading.Tasks; public class DataExporter { // Allow only 1 concurrent export (like DisableConcurrentExecution but with configurable timeout) // Wait up to 10 minutes for a slot // Check for available slots every 30 seconds (jobs typically take 2-3 minutes) [MaximumConcurrentExecutions(1, timeoutInSeconds = 600, pollingIntervalInSeconds = 30)] public void ExportToExternalSystem(string datasetId) { Console.WriteLine($"Starting export of dataset {datasetId}"); // Simulate long-running export to external API Task.Delay(TimeSpan.FromMinutes(2)).Wait(); Console.WriteLine($"Export of dataset {datasetId} completed"); } } // Usage public class ExportController { public void TriggerExports() { var datasets = new[] { "sales-2024", "inventory-2024", "customers-2024" }; foreach (var dataset in datasets) { BackgroundJob.Enqueue(x => x.ExportToExternalSystem(dataset)); } // Only one export runs at a time, others poll every 30 seconds for a slot } } ``` -------------------------------- ### Configure MaximumConcurrentExecutions with Polling Interval (C#) Source: https://github.com/alastairtree/hangfire.maximumconcurrentexecutions/blob/master/README.md Demonstrates configuring the `MaximumConcurrentExecutions` attribute with both a timeout and a polling interval. This example sets the maximum concurrent executions to 3, timeout to 120 seconds, and the polling interval to 60 seconds. ```csharp public class ExampleJob { [MaximumConcurrentExecutions(3, timeoutInSeconds = 120, pollingIntervalInSeconds = 60)] public void SomeLongRunningActivity() { ... } } ``` -------------------------------- ### Decorate Job with MaximumConcurrentExecutions Attribute (C#) Source: https://github.com/alastairtree/hangfire.maximumconcurrentexecutions/blob/master/README.md Demonstrates how to apply the `MaximumConcurrentExecutions` attribute to a C# job class to limit its concurrent runs. This example sets a maximum of 3 concurrent executions for the `SomeLongRunningActivity` method. ```csharp public class ExampleJob { [MaximumConcurrentExecutions(3)] public void SomeLongRunningActivity() { Console.WriteLine("Starting job"); Task.Delay(TimeSpan.FromSeconds(5)).Wait(); } } ``` -------------------------------- ### Configure Timeout for Waiting Job Slots with MaximumConcurrentExecutionsAttribute Source: https://context7.com/alastairtree/hangfire.maximumconcurrentexecutions/llms.txt Configures the MaximumConcurrentExecutionsAttribute with a specific timeout in seconds, controlling how long a job waits for an available execution slot before throwing a DistributedLockTimeoutException. The default is 60 seconds. ```csharp using Hangfire; using System; using System.Threading.Tasks; public class ReportGenerator { // Allow up to 2 concurrent report generations // Wait up to 5 minutes (300 seconds) for a slot if all are busy [MaximumConcurrentExecutions(2, timeoutInSeconds = 300)] public void GenerateMonthlyReport(int year, int month) { Console.WriteLine($"Generating report for {year}-{month:D2}"); // Simulate long-running report generation Task.Delay(TimeSpan.FromMinutes(3)).Wait(); Console.WriteLine($"Report for {year}-{month:D2} completed"); } } // Usage: Queue reports for multiple months public class ReportScheduler { public void ScheduleQuarterlyReports() { BackgroundJob.Enqueue(x => x.GenerateMonthlyReport(2024, 1)); BackgroundJob.Enqueue(x => x.GenerateMonthlyReport(2024, 2)); BackgroundJob.Enqueue(x => x.GenerateMonthlyReport(2024, 3)); // First 2 jobs run immediately, 3rd waits up to 5 minutes for a slot } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.