hrms-api-backend/BMA.EHR.Insignia/Services/InsigniaRequestProcessService.cs
waruneeauy fbcd1b6984
All checks were successful
Build & Deploy Leave Service / build (push) Successful in 1m12s
Build & Deploy Discipline Service / build (push) Successful in 1m19s
Merge branch 'develop'
* develop: (27 commits)
  แก้บรรจุ
  test parent
  แก้ search ช้า
  refactor code
  delete comment
  edit query
  ปรับให้จัด group ตามที่กรอง (#1858)
  ทำฟังก์ชันกลางคำนวณปีงบประมาณ
  fix sort api/v1/leave/search (#1852)
  fix sort /api/v1/leave/admin/edit (#1848)
  จัด Align (#1846)
  fix #1841, #1842
  fix timestamp report #1843 (#1844)
  add parent
  sort Discipline
  #1838
  sortBy #1814
  fix report
  fix #1835
  fix #24
  ...

# Conflicts:
#	BMA.EHR.Insignia/Services/InsigniaRequestProcessService.cs
2025-10-14 12:27:30 +07:00

119 lines
3.4 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("wss://hrmsbkk.case-collection.com",
new SocketIOOptions
{
// เพิ่ม token ใน handshake.auth
Auth = new { token = AccessToken ?? "" },
Path = "/api/v1/org-socket"
});
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);
}
//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);
}
}