fix OOM Error
All checks were successful
Build & Deploy on Dev / build (push) Successful in 54s

This commit is contained in:
Suphonchai Phoonsawat 2026-05-15 16:37:14 +07:00
parent 9c2caa3f4a
commit 2b000cbd69
5 changed files with 95 additions and 63 deletions

View file

@ -41,64 +41,61 @@ public class ImportBackgroundService : BackgroundService
{
var job = await _queue.DequeueAsync(stoppingToken);
_ = Task.Run(async () =>
{
using var scope = _scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
var minioService = scope.ServiceProvider.GetRequiredService<MinIOService>();
var recruitService = scope.ServiceProvider.GetRequiredService<RecruitService>();
var notificationService = scope.ServiceProvider.GetRequiredService<NotificationService>();
var webHostEnv = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<ImportBackgroundService>>();
using var scope = _scopeFactory.CreateScope();
var context = scope.ServiceProvider.GetRequiredService<ApplicationDbContext>();
var minioService = scope.ServiceProvider.GetRequiredService<MinIOService>();
var recruitService = scope.ServiceProvider.GetRequiredService<RecruitService>();
var notificationService = scope.ServiceProvider.GetRequiredService<NotificationService>();
var webHostEnv = scope.ServiceProvider.GetRequiredService<IWebHostEnvironment>();
var logger = scope.ServiceProvider.GetRequiredService<ILogger<ImportBackgroundService>>();
try
{
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Running);
switch (job.JobType)
{
case ImportJobType.CandidateFile:
await ProcessCandidateFileAsync(context, minioService, webHostEnv, job);
break;
case ImportJobType.CandidateFileById:
await ProcessCandidateFileByIdAsync(context, minioService, recruitService, webHostEnv, job);
break;
case ImportJobType.ScoreFile:
await ProcessScoreFileAsync(context, minioService, recruitService, job);
break;
case ImportJobType.ResultFile:
await ProcessResultFileAsync(context, recruitService, job);
break;
}
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Completed, job.TotalCount);
await notificationService.SendImportNotificationAsync(job.Token, false, "ระบบนำเข้าข้อมูลสำเร็จ");
}
catch (Exception ex)
{
logger.LogError(ex, "Import job {JobId} failed: {Message}", job.JobId, ex.Message);
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Failed, 0, ex.Message);
try { await notificationService.SendImportNotificationAsync(job.Token, true, ex.Message); } catch { }
// cleanup minio file on failure
if (!string.IsNullOrEmpty(job.ImportDocId))
{
try { await minioService.DeleteFileAsync(Guid.Parse(job.ImportDocId)); } catch { }
}
}
finally
{
// cleanup temp file
try
{
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Running);
switch (job.JobType)
{
case ImportJobType.CandidateFile:
await ProcessCandidateFileAsync(context, minioService, webHostEnv, job);
break;
case ImportJobType.CandidateFileById:
await ProcessCandidateFileByIdAsync(context, minioService, recruitService, webHostEnv, job);
break;
case ImportJobType.ScoreFile:
await ProcessScoreFileAsync(context, minioService, recruitService, job);
break;
case ImportJobType.ResultFile:
await ProcessResultFileAsync(context, recruitService, job);
break;
}
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Completed, job.TotalCount);
await notificationService.SendImportNotificationAsync(job.Token, false, "ระบบนำเข้าข้อมูลสำเร็จ");
if (System.IO.File.Exists(job.ImportFile))
System.IO.File.Delete(job.ImportFile);
}
catch (Exception ex)
{
logger.LogError(ex, "Import job {JobId} failed: {Message}", job.JobId, ex.Message);
_tracker.UpdateStatus(job.JobId, ImportJobStatus.Failed, 0, ex.Message);
await notificationService.SendImportNotificationAsync(job.Token, true, ex.Message);
// cleanup minio file on failure
if (!string.IsNullOrEmpty(job.ImportDocId))
{
try { await minioService.DeleteFileAsync(Guid.Parse(job.ImportDocId)); } catch { }
}
}
finally
{
// cleanup temp file
try
{
if (System.IO.File.Exists(job.ImportFile))
System.IO.File.Delete(job.ImportFile);
}
catch { }
}
}, stoppingToken);
catch { }
}
}
}
@ -266,6 +263,8 @@ public class ImportBackgroundService : BackgroundService
await _context.BulkInsertAsync(batchCertificates);
await _context.BulkInsertAsync(batchEducations);
_context.ChangeTracker.Clear();
batchRecruits.Clear();
batchAddresses.Clear();
batchPayments.Clear();
@ -296,6 +295,8 @@ public class ImportBackgroundService : BackgroundService
await _context.BulkInsertAsync(batchOccupations);
await _context.BulkInsertAsync(batchCertificates);
await _context.BulkInsertAsync(batchEducations);
_context.ChangeTracker.Clear();
}
}
@ -510,6 +511,8 @@ public class ImportBackgroundService : BackgroundService
throw new Exception($"BulkInsert failed (rows {batchStartRow}-{row - 1}, {batchRecruits.Count} records): {ex.InnerException?.Message ?? ex.Message}", ex);
}
_context.ChangeTracker.Clear();
// Clear all lists for next batch
batchRecruits.Clear();
batchEducations.Clear();
@ -537,6 +540,8 @@ public class ImportBackgroundService : BackgroundService
var batchStartRow = row - batchCount + 1;
throw new Exception($"BulkInsert failed (rows {batchStartRow}-{row - 1}, {batchRecruits.Count} records): {ex.InnerException?.Message ?? ex.Message}", ex);
}
_context.ChangeTracker.Clear();
}
}