hrms-api-backend/BMA.EHR.Insignia/Services/InsigniaRequestProcessService.cs

101 lines
2.8 KiB
C#
Raw Normal View History

2025-09-02 14:55:07 +07:00
using BMA.EHR.Insignia.Service.Configuration;
using SocketIOClient;
using System.Security.Claims;
2025-08-28 12:33:18 +07:00
namespace BMA.EHR.Insignia.Service.Services;
public class InsigniaRequestProcessService : BackgroundService
{
private readonly IBackgroundTaskQueue _queue;
2025-09-02 14:55:07 +07:00
private readonly IHttpContextAccessor _httpContextAccessor;
public InsigniaRequestProcessService(
IBackgroundTaskQueue queue,
IHttpContextAccessor httpContextAccessor)
2025-08-28 12:33:18 +07:00
{
_queue = queue;
2025-09-02 14:55:07 +07:00
_httpContextAccessor = httpContextAccessor;
2025-08-28 12:33:18 +07:00
}
2025-09-02 14:55:07 +07:00
#region " Properties "
2025-08-28 12:33:18 +07:00
2025-09-02 14:55:07 +07:00
protected string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
2025-08-28 12:33:18 +07:00
2025-09-02 14:55:07 +07:00
protected string? FullName => _httpContextAccessor?.HttpContext?.User?.FindFirst("name")?.Value;
protected bool? IsPlacementAdmin => _httpContextAccessor?.HttpContext?.User?.IsInRole("placement1");
protected string? AccessToken => _httpContextAccessor?.HttpContext?.Request.Headers["Authorization"];
#endregion
2025-08-28 12:33:18 +07:00
2025-09-02 14:55:07 +07:00
public override async Task StartAsync(CancellationToken cancellationToken)
{
var client = new SocketIO("wss://hrmsbkk.case-collection.com",
2025-09-02 14:55:07 +07:00
new SocketIOOptions
2025-08-28 12:33:18 +07:00
{
2025-09-02 14:55:07 +07:00
// เพิ่ม token ใน handshake.auth
Auth = new { token = AccessToken ?? "" },
Path = "/api/v1/org-socket"
2025-08-28 12:33:18 +07:00
});
2025-09-02 14:55:07 +07:00
client.OnConnected += async (sender, e) =>
2025-08-28 12:33:18 +07:00
{
2025-09-02 14:55:07 +07:00
Console.WriteLine("Connected to Socket.IO server");
await client.EmitAsync("eventName", "Hello from .NET client");
};
2025-08-28 12:33:18 +07:00
2025-09-02 14:55:07 +07:00
await client.ConnectAsync();
await base.StartAsync(cancellationToken);
2025-08-28 12:33:18 +07:00
}
2025-09-02 14:55:07 +07:00
2025-08-28 12:33:18 +07:00
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
2025-09-02 14:55:07 +07:00
2025-08-28 12:33:18 +07:00
while (!stoppingToken.IsCancellationRequested)
{
try
{
var workItem = await _queue.DequeueAsync(stoppingToken);
if (workItem != null)
{
2025-09-02 14:55:07 +07:00
var startTime = DateTime.Now;
2025-08-28 12:33:18 +07:00
await workItem(stoppingToken);
2025-09-02 14:55:07 +07:00
var endTime = DateTime.Now;
var duration = endTime - startTime;
2025-08-28 12:33:18 +07:00
}
}
2025-09-02 14:55:07 +07:00
catch (OperationCanceledException)
{
break;
}
2025-08-28 12:33:18 +07:00
catch (Exception ex)
{
2025-09-02 14:55:07 +07:00
// รอสักครู่ก่อนประมวลผล task ถัดไป เพื่อป้องกันการวนลูปข้อผิดพลาด
await Task.Delay(1000, stoppingToken);
2025-08-28 12:33:18 +07:00
}
}
2025-09-02 14:55:07 +07:00
2025-08-28 12:33:18 +07:00
}
2025-09-02 14:55:07 +07:00
public override async Task StopAsync(CancellationToken cancellationToken)
2025-08-28 12:33:18 +07:00
{
2025-09-02 14:55:07 +07:00
await base.StopAsync(cancellationToken);
2025-08-28 12:33:18 +07:00
}
}