Files
torrentio/src/producer/Jobs/PublisherJob.cs
iPromKnight 57f4757541 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
2024-02-02 14:43:29 +00:00

37 lines
1.2 KiB
C#

namespace Producer.Jobs;
[DisallowConcurrentExecution]
public class PublisherJob(IMessagePublisher publisher, IDataStorage storage, ILogger<PublisherJob> logger) : IJob
{
private const string JobName = nameof(PublisherJob);
public static readonly JobKey Key = new(JobName, nameof(Jobs));
public static readonly TriggerKey Trigger = new($"{JobName}-trigger", nameof(Jobs));
public async Task Execute(IJobExecutionContext context)
{
var cancellationToken = context.CancellationToken;
var torrents = await storage.GetPublishableTorrents(cancellationToken);
if (torrents.Count == 0)
{
return;
}
var published = await publisher.PublishAsync(torrents, cancellationToken);
if (!published)
{
return;
}
var result = await storage.SetTorrentsProcessed(torrents, cancellationToken);
if (!result.Success)
{
logger.LogWarning("Failed to set torrents as processed: [{Error}]", result.ErrorMessage);
return;
}
logger.LogInformation("Successfully set {Count} torrents as processed", result.UpdatedCount);
}
}