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 Queues { get; } public override async Task StartAsync(CancellationToken cancellationToken) { using var ctx = Provider.Create(); if (ctx.GetService() is not IMiddlewareService middleware) return; foreach (var m in await middleware.Query>()) { 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() 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); } }