You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Common/Common/Collections/QueueClientService.cs

54 lines
1.3 KiB

2 years ago
using Connected.Collections.Queues;
using Connected.Hosting.Workers;
using Connected.Middleware;
using Connected.ServiceModel;
namespace Common.Collections;
internal sealed class QueueClientService : ScheduledWorker
{
public QueueClientService(IContextProvider provider)
{
Dispatcher = new();
Timer = TimeSpan.FromMilliseconds(500);
Queues = new();
Provider = provider;
}
private IContextProvider Provider { get; }
private QueueMessageDispatcher Dispatcher { get; }
private List<string> Queues { get; }
public override async Task StartAsync(CancellationToken cancellationToken)
{
using var ctx = Provider.Create();
if (ctx.GetService<IMiddlewareService>() is not IMiddlewareService middleware)
return;
foreach (var m in await middleware.Query<IQueueClient<QueueArgs>>())
{
if (m.GetType().FullName is string fullName)
Queues.Add(fullName);
}
}
protected override async Task OnInvoke(CancellationToken cancellationToken)
{
using var ctx = Provider.Create();
if (ctx.GetService<IQueueService>() is not IQueueService queue)
return;
var messages = await queue.Dequeue(new DequeueArgs
{
MaxCount = Dispatcher.Available,
NextVisible = TimeSpan.FromSeconds(30),
Queues = Queues
});
foreach (var message in messages)
Dispatcher.Enqueue(message);
}
}