using CallCenter.Caches; using CallCenter.Realtimes; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Hosting; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CallCenter.CacheManager { public class CallQueueManager : BackgroundService { private readonly IServiceScopeFactory _serviceScopeFactory; private const string CallKey = "CallQueue"; public CallQueueManager(IServiceScopeFactory serviceScopeFactory) { _serviceScopeFactory = serviceScopeFactory; } protected override async Task ExecuteAsync(CancellationToken cancellationToken) { //work缓存注入 var userCacheManager = _serviceScopeFactory.CreateScope().ServiceProvider.GetService(); //消息通知注入 var realtimeService = _serviceScopeFactory.CreateScope().ServiceProvider.GetService(); //CallQueue缓存注入 var callQueueManager = _serviceScopeFactory.CreateScope().ServiceProvider.GetService(); var time = TimeSpan.FromSeconds(1); while (!cancellationToken.IsCancellationRequested) { //查询当前队列 var callList = callQueueManager.GetCallQueueList(); if (callList ==null) { callList = new List(); } //通知 await realtimeService.CallQueueAsync(callList, cancellationToken); await Task.Delay(time, cancellationToken); } } } }