hrms-api-backend/BMA.EHR.CheckInConsumer/Program.cs

131 lines
3.8 KiB
C#
Raw Normal View History

2026-04-29 09:57:26 +07:00
using Microsoft.Extensions.Configuration;
2024-08-19 16:00:00 +07:00
using RabbitMQ.Client;
using RabbitMQ.Client.Events;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
var basePath = Directory.GetCurrentDirectory();
var configuration = new ConfigurationBuilder()
.SetBasePath(basePath)
.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
.Build();
WriteToConsole("Consumer Start!");
var host = configuration["Rabbit:Host"] ?? "";
var user = configuration["Rabbit:User"] ?? "";
var pass = configuration["Rabbit:Password"] ?? "";
2024-12-13 10:35:05 +07:00
var queue = configuration["Rabbit:Queue"] ?? "basic-queue";
2024-08-19 16:00:00 +07:00
// create connection
var factory = new ConnectionFactory()
{
2026-04-29 09:57:26 +07:00
HostName = host,
UserName = user,
Password = pass,
DispatchConsumersAsync = true
2024-08-19 16:00:00 +07:00
};
using var connection = factory.CreateConnection();
using var channel = connection.CreateModel();
2024-12-13 10:35:05 +07:00
channel.QueueDeclare(queue: queue, durable: true, exclusive: false, autoDelete: false, arguments: null);
2024-08-19 16:00:00 +07:00
2026-04-29 09:57:26 +07:00
// Create a SINGLE static HttpClient instance to prevent socket exhaustion
using var httpClient = new HttpClient();
httpClient.Timeout = TimeSpan.FromSeconds(300); // 5 นาที
2024-08-19 16:00:00 +07:00
var consumer = new EventingBasicConsumer(channel);
consumer.Received += async (model, ea) =>
{
2026-04-29 09:57:26 +07:00
try
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
//WriteToConsole($"ได้รับคำขอจาก Queue: {message}");
await CallRestApi(message, httpClient, configuration);
}
catch (Exception ex)
{
WriteToConsole($"Error processing message: {ex.Message}");
}
2024-08-19 16:00:00 +07:00
};
channel.BasicConsume(queue: queue, autoAck: true, consumer: consumer);
2024-08-19 16:00:00 +07:00
2026-04-29 09:57:26 +07:00
WriteToConsole("Consumer started. Waiting for messages...");
2026-04-29 09:57:26 +07:00
// Keep the application running
await Task.Delay(-1);
2024-08-19 16:00:00 +07:00
static void WriteToConsole(string message)
{
2026-04-29 09:57:26 +07:00
Console.WriteLine($"{DateTime.Now:yyyy-MM-dd HH:mm:ss} : {message}");
2024-08-19 16:00:00 +07:00
}
2026-04-29 09:57:26 +07:00
static async Task CallRestApi(string requestData, HttpClient client, IConfiguration configuration)
2024-08-19 16:00:00 +07:00
{
2026-04-29 09:57:26 +07:00
try
{
var apiPath = $"{configuration["API"]}/leave/process-check-in";
var content = new StringContent(requestData, Encoding.UTF8, "application/json");
var response = await client.PostAsync(apiPath, content);
if (response.IsSuccessStatusCode)
{
var responseContent = await response.Content.ReadAsStringAsync();
//WriteToConsole($"Success: {responseContent}");
}
else
{
var errorMessage = await response.Content.ReadAsStringAsync();
var res = JsonSerializer.Deserialize<ResponseObject>(errorMessage);
WriteToConsole($"API Error: {res?.Message ?? "Unknown error"}");
}
}
catch (HttpRequestException ex)
2024-08-19 16:00:00 +07:00
{
2026-04-29 09:57:26 +07:00
WriteToConsole($"HTTP Error: {ex.Message}");
2024-08-19 16:00:00 +07:00
}
2026-04-29 09:57:26 +07:00
catch (TaskCanceledException ex)
2024-08-19 16:00:00 +07:00
{
2026-04-29 09:57:26 +07:00
WriteToConsole($"Timeout: {ex.Message}");
}
catch (Exception ex)
{
WriteToConsole($"Unexpected Error: {ex.Message}");
2024-08-19 16:00:00 +07:00
}
}
public class ResponseObject
{
[JsonPropertyName("status")]
public int Status { get; set; }
[JsonPropertyName("message")]
public string? Message { get; set; }
[JsonPropertyName("result")]
public object? Result { get; set; }
}
public class CheckTimeDtoRB
{
public Guid? CheckInId { get; set; }
public double Lat { get; set; } = 0;
public double Lon { get; set; } = 0;
public string POI { get; set; } = string.Empty;
public bool IsLocation { get; set; } = true;
public string? LocationName { get; set; } = string.Empty;
public string? Remark { get; set; } = string.Empty;
public Guid? UserId { get; set; }
public DateTime? CurrentDate { get; set; }
public string? CheckInFileName { get; set; }
public byte[]? CheckInFileBytes { get; set; }
2026-04-29 09:57:26 +07:00
}