99 lines
2.8 KiB
C#
99 lines
2.8 KiB
C#
using BMA.EHR.Insignia.Service.Configuration;
|
|
using SocketIOClient;
|
|
using System.Security.Claims;
|
|
|
|
|
|
namespace BMA.EHR.Insignia.Service.Services;
|
|
|
|
public class InsigniaRequestProcessService : BackgroundService
|
|
{
|
|
private readonly IBackgroundTaskQueue _queue;
|
|
private readonly IHttpContextAccessor _httpContextAccessor;
|
|
|
|
|
|
public InsigniaRequestProcessService(
|
|
IBackgroundTaskQueue queue,
|
|
IHttpContextAccessor httpContextAccessor)
|
|
{
|
|
_queue = queue;
|
|
_httpContextAccessor = httpContextAccessor;
|
|
|
|
|
|
}
|
|
|
|
#region " Properties "
|
|
|
|
protected string? UserId => _httpContextAccessor?.HttpContext?.User?.FindFirst(ClaimTypes.NameIdentifier)?.Value;
|
|
|
|
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
|
|
|
|
//public override async Task StartAsync(CancellationToken cancellationToken)
|
|
//{
|
|
//var client = new SocketIO("https://bma-ehr.frappet.synology.me/api/v1/org-socket",
|
|
// new SocketIOOptions
|
|
// {
|
|
// // เพิ่ม token ใน handshake.auth
|
|
// Auth = new { token = AccessToken ?? "" }
|
|
// });
|
|
|
|
//client.OnConnected += async (sender, e) =>
|
|
//{
|
|
// Console.WriteLine("Connected to Socket.IO server");
|
|
// await client.EmitAsync("eventName", "Hello from .NET client");
|
|
//};
|
|
|
|
//await client.ConnectAsync();
|
|
|
|
//await base.StartAsync(cancellationToken);
|
|
//}
|
|
|
|
|
|
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
{
|
|
|
|
|
|
while (!stoppingToken.IsCancellationRequested)
|
|
{
|
|
try
|
|
{
|
|
var workItem = await _queue.DequeueAsync(stoppingToken);
|
|
if (workItem != null)
|
|
{
|
|
var startTime = DateTime.Now;
|
|
|
|
await workItem(stoppingToken);
|
|
|
|
var endTime = DateTime.Now;
|
|
var duration = endTime - startTime;
|
|
|
|
}
|
|
}
|
|
catch (OperationCanceledException)
|
|
{
|
|
|
|
break;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// รอสักครู่ก่อนประมวลผล task ถัดไป เพื่อป้องกันการวนลูปข้อผิดพลาด
|
|
await Task.Delay(1000, stoppingToken);
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
public override async Task StopAsync(CancellationToken cancellationToken)
|
|
{
|
|
|
|
|
|
await base.StopAsync(cancellationToken);
|
|
|
|
}
|
|
}
|