mirror of
https://github.com/knightcrawler-stremio/knightcrawler.git
synced 2024-12-20 03:29:51 +00:00
Implement Max Queue and Max Batch size when publishing
MaxPublishBatchSize must be set, but MaxQueueSize can be set to 0 to disable check of the rabbitmq queue size
This commit is contained in:
@@ -1,12 +1,24 @@
|
||||
namespace Producer.Services;
|
||||
|
||||
public class TorrentPublisher(ISendEndpointProvider sendEndpointProvider, RabbitMqConfiguration configuration) : IMessagePublisher
|
||||
public class TorrentPublisher(
|
||||
ISendEndpointProvider sendEndpointProvider,
|
||||
RabbitMqConfiguration configuration,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
ILogger<TorrentPublisher> logger) : IMessagePublisher
|
||||
{
|
||||
public async Task PublishAsync(IEnumerable<Torrent> torrents, CancellationToken cancellationToken = default)
|
||||
public async Task<bool> PublishAsync(IReadOnlyCollection<Torrent> torrents, CancellationToken cancellationToken = default)
|
||||
{
|
||||
var queueAddress = ConstructQueue();
|
||||
var sendEndpoint = await sendEndpointProvider.GetSendEndpoint(new(queueAddress));
|
||||
|
||||
if (!await CanPublishToRabbitMq(torrents, cancellationToken))
|
||||
{
|
||||
logger.LogWarning("Queue is full or not accessible, not publishing this time.");
|
||||
return false;
|
||||
}
|
||||
|
||||
await sendEndpoint.SendBatch(torrents, cancellationToken: cancellationToken);
|
||||
return true;
|
||||
}
|
||||
|
||||
private string ConstructQueue()
|
||||
@@ -19,4 +31,48 @@ public class TorrentPublisher(ISendEndpointProvider sendEndpointProvider, Rabbit
|
||||
|
||||
return queueBuilder.ToString();
|
||||
}
|
||||
|
||||
private async Task<bool> CanPublishToRabbitMq(IReadOnlyCollection<Torrent> torrents, CancellationToken cancellationToken)
|
||||
{
|
||||
if (configuration.MaxQueueSize == 0)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
var client = httpClientFactory.CreateClient("RabbitMq");
|
||||
|
||||
client.DefaultRequestHeaders.Authorization =
|
||||
new("Basic", Convert.ToBase64String(Encoding.UTF8.GetBytes($"{configuration.Username}:{configuration.Password}")));
|
||||
|
||||
var url = $"http://{configuration.Host}:15672/api/queues/{Uri.EscapeDataString("/")}/{configuration.QueueName}";
|
||||
|
||||
var response = await client.GetAsync(url, cancellationToken);
|
||||
|
||||
var body = await response.Content.ReadAsStringAsync(cancellationToken);
|
||||
var doc = JsonDocument.Parse(body);
|
||||
|
||||
if (!doc.RootElement.TryGetProperty("messages", out var messages))
|
||||
{
|
||||
logger.LogWarning("Failed to get message count from RabbitMq");
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!messages.TryGetInt32(out var messageCount))
|
||||
{
|
||||
logger.LogWarning("Failed to get message count from RabbitMq");
|
||||
return false;
|
||||
}
|
||||
|
||||
logger.LogInformation("Current queue message count: {MessageCount}", messageCount);
|
||||
|
||||
var canPublish = messageCount < configuration.MaxQueueSize + torrents.Count;
|
||||
|
||||
if (!canPublish)
|
||||
{
|
||||
logger.LogWarning("You have configured a max queue size of {MaxQueueSize}.", configuration.MaxQueueSize);
|
||||
logger.LogWarning("Not publishing this time.");
|
||||
}
|
||||
|
||||
return canPublish;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user