I am using hangfire to run background job that generates an excel file based on the new requests found in database(the cron is 10 min).like it is showen in this code
public class BackgroundOperations : IBackgroundOperations
{
private readonly IStatusRepository _statusRepo;
private readonly IBackgroundJobClient _backgroundJobClient;
private readonly IEpcProcess _epcProcess;
private static readonly SemaphoreSlim Semaphore = new SemaphoreSlim(5); // Limit to 5 concurrent jobs
private const int BatchSize = 5; // Process in batches of 5
public BackgroundOperations(IStatusRepository statusRepo, IBackgroundJobClient backgroundJobClient, IEpcProcess epcProcess)
{
_statusRepo = statusRepo;
_backgroundJobClient = backgroundJobClient;
_epcProcess = epcProcess;
}
public async Task ProcessNewTransactions()
{
List<string> newTransactionIDs = await _statusRepo.GetTransactionIDsWithStatusNew();
// Divide into batches and schedule jobs
for (int i = 0; i < newTransactionIDs.Count; i += BatchSize)
{
var batch = newTransactionIDs.Skip(i).Take(BatchSize).ToList();
await ScheduleEpcStartJobBatch(batch);
}
newTransactionIDs.Clear();
}
private Task ScheduleEpcStartJobBatch(List<string> transactionIds)
{
_backgroundJobClient.Enqueue(() => ProcessBatchWithThrottling(transactionIds));
return Task.CompletedTask;
}
[AutomaticRetry(Attempts = 3, DelaysInSeconds = new int[] { 60, 120, 300 })] // Retry mechanism
public async Task ProcessBatchWithThrottling(List<string> transactionIds)
{
await Semaphore.WaitAsync();
try
{
foreach (var transactionId in transactionIds)
{
await SafeEpcProcessStart(transactionId);
}
}
finally
{
Semaphore.Release();
}
}
private async Task SafeEpcProcessStart(string transactionId)
{
try
{
await _epcProcess.EpcProcessStart(transactionId);
}
catch (Exception )
{
SentrySdk.CaptureMessage("Problem occurred with files generation", SentryLevel.Error);
}
}
when many transaction found this code works with no problem on my computer and hangfire is able to proceed with all of them perfectly(run as docker image), but when i hosted on app service i face an issue that hangfire is aborting the job and all the jobs get stack on inProcess phase and at the end gives 502 bad request, by that the entire application starts to be unavailable returning 503
So the question is why would that happen, its the same configuration and both hosted as container? Are the cpu and memory in my computer better? or what is the problem
For the configuration of hangfire in my program.cs is the folowing:
builder.Services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
.UseSimpleAssemblyNameTypeSerializer()
.UseRecommendedSerializerSettings()
.UsePostgreSqlStorage(connectionString));