Refactor LeaveProcessJobStatusRepository methods and update Hangfire configuration for improved job processing

This commit is contained in:
Suphonchai Phoonsawat 2026-03-30 12:08:30 +07:00
parent 8732c34564
commit 3dee5f7166
4 changed files with 25 additions and 16 deletions

View file

@ -68,10 +68,10 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
return data; return data;
} }
public async Task<List<LeaveProcessJobStatus>> GetPendingOrProcessingJobsAsync() public async Task<List<LeaveProcessJobStatus>> GetPendingJobsAsync()
{ {
var data = await _dbContext.Set<LeaveProcessJobStatus>() var data = await _dbContext.Set<LeaveProcessJobStatus>()
.Where(x => x.Status == "PENDING" || x.Status == "PROCESSING") .Where(x => x.Status == "PENDING")
.ToListAsync(); .ToListAsync();
return data; return data;
@ -125,15 +125,14 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
public async Task ProcessTaskAsync(Guid id, CancellationToken cancellationToken = default) public async Task ProcessTaskAsync(Guid id, CancellationToken cancellationToken = default)
{ {
Console.WriteLine($"กำลังประมวลผลงานที่มี RootDnaId: {id}");
} }
public async Task ProcessPendingJobsAsync() public async Task ProcessPendingJobsAsync()
{ {
var pendingJobs = await GetPendingJobsAsync();
Console.WriteLine($"พบงานที่ค้างอยู่ในสถานะ PENDING จำนวน {pendingJobs.Count} งาน");
var pendingJobs = await GetPendingOrProcessingJobsAsync();
foreach (var job in pendingJobs) foreach (var job in pendingJobs)
{ {

View file

@ -131,7 +131,7 @@ var builder = WebApplication.CreateBuilder(args);
{ {
options.ServerName = "Insignia-Server"; // ← ระบุชื่อ server options.ServerName = "Insignia-Server"; // ← ระบุชื่อ server
options.WorkerCount = 5; // ← options.WorkerCount = 5; // ←
options.Queues = new[] { "insignia" }; // ← worker จะรันเฉพาะ queue "insignia" options.Queues = new[] { "insignia","default" }; // ← worker จะรันเฉพาะ queue "insignia"
}); });

View file

@ -119,7 +119,7 @@ builder.Services.AddHealthChecks();
builder.Services.AddRabbitMqConnectionPooling(builder.Configuration); builder.Services.AddRabbitMqConnectionPooling(builder.Configuration);
// Add Hangfire services. // Add Hangfire services.
var defaultConnection = builder.Configuration.GetConnectionString("DefaultConnection"); var hangfireConnection = builder.Configuration.GetConnectionString("defaultConnection");
builder.Services.AddHangfire(configuration => configuration builder.Services.AddHangfire(configuration => configuration
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170) .SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
@ -127,19 +127,24 @@ builder.Services.AddHangfire(configuration => configuration
.UseRecommendedSerializerSettings() .UseRecommendedSerializerSettings()
.UseStorage( .UseStorage(
new MySqlStorage( new MySqlStorage(
defaultConnection, hangfireConnection,
new MySqlStorageOptions new MySqlStorageOptions
{ {
TransactionIsolationLevel = IsolationLevel.ReadCommitted,
QueuePollInterval = TimeSpan.FromSeconds(15), QueuePollInterval = TimeSpan.FromSeconds(15),
JobExpirationCheckInterval = TimeSpan.FromHours(1), JobExpirationCheckInterval = TimeSpan.FromHours(1),
CountersAggregateInterval = TimeSpan.FromMinutes(5), CountersAggregateInterval = TimeSpan.FromMinutes(5),
PrepareSchemaIfNecessary = true, PrepareSchemaIfNecessary = true,
DashboardJobListLimit = 50000, DashboardJobListLimit = 50000,
TransactionTimeout = TimeSpan.FromMinutes(1), TransactionTimeout = TimeSpan.FromMinutes(1),
TablesPrefix = "Hangfire" InvisibilityTimeout = TimeSpan.FromHours(3),
TablesPrefix = "Hangfire_Leave"
}))); })));
builder.Services.AddHangfireServer(); builder.Services.AddHangfireServer(options =>
{
options.ServerName = "Leave-Server"; // ← ระบุชื่อ server
options.WorkerCount = 5; // ←
options.Queues = new[] { "leave","default" }; // ← worker จะรันเฉพาะ queue "leave"
});
var app = builder.Build(); var app = builder.Build();
@ -191,7 +196,12 @@ if (manager != null)
// ทำความสะอาดข้อมูล CheckIn Job Status ที่เก่ากว่า 30 วัน - รันทุกวันเวลา 02:00 น. // ทำความสะอาดข้อมูล CheckIn Job Status ที่เก่ากว่า 30 วัน - รันทุกวันเวลา 02:00 น.
manager.AddOrUpdate("ทำความสะอาดข้อมูล CheckIn Job Status", Job.FromExpression<CheckInJobStatusRepository>(x => x.CleanupOldJobsAsync(30)), "0 2 * * *", bangkokTimeZone); manager.AddOrUpdate("ทำความสะอาดข้อมูล CheckIn Job Status", Job.FromExpression<CheckInJobStatusRepository>(x => x.CleanupOldJobsAsync(30)), "0 2 * * *", bangkokTimeZone);
manager.AddOrUpdate("ประมวลผลงานที่ค้างอยู่ในสถานะ Pending หรือ Processing", Job.FromExpression<LeaveProcessJobStatusRepository>(x => x.ProcessPendingJobsAsync()), "0 3 * * *", bangkokTimeZone); manager.AddOrUpdate("ประมวลผลงานที่ค้างอยู่ในสถานะ Pending หรือ Processing", Job.FromExpression<LeaveProcessJobStatusRepository>(x => x.ProcessPendingJobsAsync()), "0 3 * * *",
new RecurringJobOptions
{
TimeZone = bangkokTimeZone,
QueueName = "leave" // ← กำหนด queue
});
} }
// apply migrations // apply migrations

View file

@ -19,9 +19,9 @@
// "ExamConnection": "server=192.168.1.80;user=root;password=adminVM123;port=3306;database=hrms_exam;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;", // "ExamConnection": "server=192.168.1.80;user=root;password=adminVM123;port=3306;database=hrms_exam;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;",
// "LeaveConnection": "server=192.168.1.80;user=root;password=adminVM123;port=3306;database=hrms_leave;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;" // "LeaveConnection": "server=192.168.1.80;user=root;password=adminVM123;port=3306;database=hrms_leave;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;"
"DefaultConnection": "server=192.168.1.63;user=root;password=12345678;port=3306;database=hrms;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;", "DefaultConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;Database=hrms;Allow User Variables=True;Convert Zero Datetime=True;Pooling=True;",
"ExamConnection": "server=192.168.1.63;user=root;password=12345678;port=3306;database=hrms_exam;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;", "ExamConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;Database=hrms_exam;Allow User Variables=True;Convert Zero Datetime=True;Pooling=True;",
"LeaveConnection": "server=192.168.1.63;user=root;password=12345678;port=3306;database=hrms_leave;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;" "LeaveConnection": "Server=192.168.1.63;User ID=root;Password=12345678;Port=3306;Database=hrms_leave;Allow User Variables=True;Convert Zero Datetime=True;Pooling=True;"
//"DefaultConnection": "server=172.27.17.68;user=user;password=cDldaqkwESWvuZ37Gr0n;port=3306;database=hrms;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;", //"DefaultConnection": "server=172.27.17.68;user=user;password=cDldaqkwESWvuZ37Gr0n;port=3306;database=hrms;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;",
//"ExamConnection": "server=172.27.17.68;user=user;password=cDldaqkwESWvuZ37Gr0n;port=3306;database=hrms_exam;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;", //"ExamConnection": "server=172.27.17.68;user=user;password=cDldaqkwESWvuZ37Gr0n;port=3306;database=hrms_exam;Convert Zero Datetime=True;Allow User Variables=true;Pooling=True;",