CallQueueManager.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. using CallCenter.Caches;
  2. using CallCenter.Realtimes;
  3. using Microsoft.Extensions.DependencyInjection;
  4. using Microsoft.Extensions.Hosting;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. namespace CallCenter.CacheManager
  11. {
  12. public class CallQueueManager : BackgroundService
  13. {
  14. private readonly IServiceScopeFactory _serviceScopeFactory;
  15. private const string CallKey = "CallQueue";
  16. public CallQueueManager(IServiceScopeFactory serviceScopeFactory)
  17. {
  18. _serviceScopeFactory = serviceScopeFactory;
  19. }
  20. protected override async Task ExecuteAsync(CancellationToken cancellationToken)
  21. {
  22. //work缓存注入
  23. var userCacheManager = _serviceScopeFactory.CreateScope().ServiceProvider.GetService<IUserCacheManager>();
  24. //消息通知注入
  25. var realtimeService = _serviceScopeFactory.CreateScope().ServiceProvider.GetService<IRealtimeService>();
  26. //CallQueue缓存注入
  27. var callQueueManager = _serviceScopeFactory.CreateScope().ServiceProvider.GetService<ICallCacheManager>();
  28. var time = TimeSpan.FromSeconds(1);
  29. while (!cancellationToken.IsCancellationRequested)
  30. {
  31. //查询当前队列
  32. var callList = callQueueManager.GetCallQueueList();
  33. if (callList ==null)
  34. {
  35. callList = new List<Calls.Call>();
  36. }
  37. //通知
  38. await realtimeService.CallQueueAsync(callList, cancellationToken);
  39. await Task.Delay(time, cancellationToken);
  40. }
  41. }
  42. }
  43. }