แก้ cal api เยอะไป

This commit is contained in:
kittapath 2025-10-18 22:18:41 +07:00
parent 4e84e76daf
commit 4da37f4aa2

View file

@ -27,6 +27,7 @@ namespace BMA.EHR.Recurit.Exam.Service.Services
private readonly MinIOService _minioService;
private readonly MailService _mailService;
private readonly IConfiguration _configuration;
private readonly IHttpClientFactory _httpClientFactory;
#endregion
@ -38,7 +39,8 @@ namespace BMA.EHR.Recurit.Exam.Service.Services
IHttpContextAccessor httpContextAccessor,
MinIOService minioService,
MailService mailService,
IConfiguration configuration)
IConfiguration configuration,
IHttpClientFactory httpClientFactory)
{
_context = context;
_contextMetadata = contextMetadata;
@ -47,6 +49,7 @@ namespace BMA.EHR.Recurit.Exam.Service.Services
_minioService = minioService;
_mailService = mailService;
_configuration = configuration;
_httpClientFactory = httpClientFactory;
}
#endregion
@ -3003,13 +3006,25 @@ namespace BMA.EHR.Recurit.Exam.Service.Services
{
try
{
// 🚀 Prepare HTTP client once
var httpClient1 = new HttpClient();
httpClient1.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token?.Replace("Bearer ", ""));
httpClient1.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]);
// 🚀 Prepare HTTP client once via factory with timeout
var clientForPos = _httpClientFactory.CreateClient("default");
clientForPos.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token?.Replace("Bearer ", ""));
clientForPos.DefaultRequestHeaders.Remove("api_key");
clientForPos.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"] ?? "");
var apiUrl1 = $"{_configuration["API"]}/org/pos/level";
var response1 = await httpClient1.GetStringAsync(apiUrl1);
var posOptions = JsonConvert.DeserializeObject<RecruitPosRequest>(response1);
var response1 = string.Empty;
try
{
using var ctsPos = new CancellationTokenSource(TimeSpan.FromSeconds(30));
response1 = await clientForPos.GetStringAsync(apiUrl1, ctsPos.Token);
}
catch (TaskCanceledException)
{
// timeout - fallback to empty posOptions
response1 = string.Empty;
}
var posOptions = string.IsNullOrWhiteSpace(response1) ? null : JsonConvert.DeserializeObject<RecruitPosRequest>(response1);
var periodExam = await _context.PeriodExams.AsQueryable()
.Where(x => x.CheckDisability == true)
@ -3069,26 +3084,41 @@ namespace BMA.EHR.Recurit.Exam.Service.Services
.Where(x => !string.IsNullOrWhiteSpace(x.ExamId))
.ToDictionary(x => x.ExamId, x => x);
// 🚀 Prepare HTTP client once
var httpClient = new HttpClient();
httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token?.Replace("Bearer ", ""));
httpClient.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"]);
// 🚀 Batch HTTP requests using IHttpClientFactory with concurrency limit and cancellation
var clientForOrg = _httpClientFactory.CreateClient("default");
clientForOrg.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token?.Replace("Bearer ", ""));
clientForOrg.DefaultRequestHeaders.Remove("api_key");
clientForOrg.DefaultRequestHeaders.Add("api_key", _configuration["API_KEY"] ?? "");
// 🚀 Batch HTTP requests
var semaphore = new SemaphoreSlim(10); // limit concurrency
var orgTasks = candidates.Select(async candidate =>
{
if (string.IsNullOrWhiteSpace(candidate.CitizenId))
return new { CitizenId = candidate.CitizenId ?? "", org = (dynamic?)null };
var apiUrl = $"{_configuration["API"]}/org/profile/citizenid/position/{candidate.CitizenId}";
await semaphore.WaitAsync();
try
{
var response = await httpClient.GetStringAsync(apiUrl);
return new { CitizenId = candidate.CitizenId, org = JsonConvert.DeserializeObject<dynamic>(response) };
var apiUrl = $"{_configuration["API"]}/org/profile/citizenid/position/{candidate.CitizenId}";
try
{
using var cts = new CancellationTokenSource(TimeSpan.FromSeconds(30));
var response = await clientForOrg.GetStringAsync(apiUrl, cts.Token);
return new { CitizenId = candidate.CitizenId, org = JsonConvert.DeserializeObject<dynamic>(response) };
}
catch (TaskCanceledException)
{
// timeout
return new { CitizenId = candidate.CitizenId ?? "", org = (dynamic?)null };
}
catch (Exception)
{
return new { CitizenId = candidate.CitizenId ?? "", org = (dynamic?)null };
}
}
catch
finally
{
return new { CitizenId = candidate.CitizenId ?? "", org = (dynamic?)null };
semaphore.Release();
}
}).ToList();
@ -3279,8 +3309,6 @@ namespace BMA.EHR.Recurit.Exam.Service.Services
// 🚀 Single SaveChanges at the end
await _contextMetadata.SaveChangesAsync();
httpClient.Dispose();
}
catch
{