Add methods to process pending jobs and update their statuses in LeaveProcessJobStatusRepository
This commit is contained in:
parent
c1ac687101
commit
91e6b1b35b
1 changed files with 44 additions and 0 deletions
|
|
@ -68,6 +68,15 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
|
|||
return data;
|
||||
}
|
||||
|
||||
public async Task<List<LeaveProcessJobStatus>> GetPendingOrProcessingJobsAsync()
|
||||
{
|
||||
var data = await _dbContext.Set<LeaveProcessJobStatus>()
|
||||
.Where(x => x.Status == "PENDING" || x.Status == "PROCESSING")
|
||||
.ToListAsync();
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// อัปเดตสถานะเป็น Processing
|
||||
/// </summary>
|
||||
|
|
@ -113,6 +122,41 @@ namespace BMA.EHR.Application.Repositories.Leaves.TimeAttendants
|
|||
}
|
||||
return job!;
|
||||
}
|
||||
|
||||
public async Task ProcessTaskAsync(Guid id, CancellationToken cancellationToken = default)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public async Task ProcessPendingJobsAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
// กำหนด timeout เป็น 60 นาที
|
||||
using var timeoutCts = new CancellationTokenSource(TimeSpan.FromMinutes(60));
|
||||
using var combinedCts = CancellationTokenSource.CreateLinkedTokenSource(cancellationToken, timeoutCts.Token);
|
||||
|
||||
|
||||
var pendingJobs = await GetPendingOrProcessingJobsAsync();
|
||||
|
||||
foreach (var job in pendingJobs)
|
||||
{
|
||||
try
|
||||
{
|
||||
// อัปเดตสถานะเป็น Processing
|
||||
await UpdateToProcessingAsync(job.Id);
|
||||
|
||||
// ทำงานที่ต้องการที่นี่ (เช่น เรียก API, ประมวลผลข้อมูล ฯลฯ)
|
||||
await ProcessTaskAsync(job.RootDnaId, combinedCts.Token);
|
||||
|
||||
// อัปเดตสถานะเป็น Completed
|
||||
await UpdateToCompletedAsync(job.Id);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// หากเกิดข้อผิดพลาด อัปเดตสถานะเป็น Failed พร้อมข้อความแสดงข้อผิดพลาด
|
||||
await UpdateToFailedAsync(job.Id, ex.Message);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue