|
@@ -1,76 +1,128 @@
|
|
-//using System;
|
|
|
|
-//using System.Collections.Generic;
|
|
|
|
-//using System.Linq;
|
|
|
|
-//using System.Text;
|
|
|
|
-//using System.Threading.Tasks;
|
|
|
|
-//using Consul;
|
|
|
|
-//using Microsoft.Extensions.DependencyInjection;
|
|
|
|
-//using Microsoft.Extensions.Hosting;
|
|
|
|
-//using Microsoft.Extensions.Logging;
|
|
|
|
-//using Polly.Registry;
|
|
|
|
-//using XF.Domain.Dependency;
|
|
|
|
-//using XF.Domain.Exceptions;
|
|
|
|
-
|
|
|
|
-//namespace DataSharing.Application.Services
|
|
|
|
-//{
|
|
|
|
-// public class SendTaskHandler : ISendTaskHandler, IScopeDependency
|
|
|
|
-// {
|
|
|
|
-// private readonly ILogger<SendTaskHandler> _logger;
|
|
|
|
-
|
|
|
|
-// public SendTaskHandler(ILogger<SendTaskHandler> logger)
|
|
|
|
-// {
|
|
|
|
-// _logger = logger;
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// public async Task SendAsync(int count, CancellationToken cancellationToken)
|
|
|
|
-// {
|
|
|
|
-// _logger.LogInformation($"==>执行send开始, count:{count}");
|
|
|
|
-// var rd = Random.Shared.Next(1000, 10000);
|
|
|
|
-// if (rd <= 5000)
|
|
|
|
-// {
|
|
|
|
-// throw new UserFriendlyException("执行失败");
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// await Task.Delay(rd, cancellationToken);
|
|
|
|
-
|
|
|
|
-// _logger.LogInformation($"执行send结束, count:{count}<==");
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// public interface ISendTaskHandler
|
|
|
|
-// {
|
|
|
|
-// Task SendAsync(int count, CancellationToken cancellationToken);
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// public class SendTaskService : BackgroundService
|
|
|
|
-// {
|
|
|
|
-// private readonly IServiceScopeFactory _serviceScopeFactory;
|
|
|
|
-
|
|
|
|
-// public SendTaskService(IServiceScopeFactory serviceScopeFactory)
|
|
|
|
-// {
|
|
|
|
-// _serviceScopeFactory = serviceScopeFactory;
|
|
|
|
-// }
|
|
|
|
-
|
|
|
|
-// protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
-// {
|
|
|
|
-// using var scope = _serviceScopeFactory.CreateScope();
|
|
|
|
-// var provider = scope.ServiceProvider;
|
|
|
|
-// var pipelineProvider = provider.GetRequiredService<ResiliencePipelineProvider<string>>();
|
|
|
|
-// var pipeline = pipelineProvider.GetPipeline(StrategyDefaults.RetryStrategy);
|
|
|
|
-// var handler = provider.GetRequiredService<ISendTaskHandler>();
|
|
|
|
-
|
|
|
|
-// var count = 0;
|
|
|
|
-// while (!stoppingToken.IsCancellationRequested)
|
|
|
|
-// {
|
|
|
|
-// await pipeline.ExecuteAsync(async token =>
|
|
|
|
-// {
|
|
|
|
-// Console.WriteLine($"execute count:{count}");
|
|
|
|
-// await handler.SendAsync(count, token);
|
|
|
|
-// count++;
|
|
|
|
-// });
|
|
|
|
-
|
|
|
|
-// await Task.Delay(1000, stoppingToken);
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-// }
|
|
|
|
-//}
|
|
|
|
|
|
+using System;
|
|
|
|
+using System.Collections.Generic;
|
|
|
|
+using System.Linq;
|
|
|
|
+using System.Text;
|
|
|
|
+using System.Threading.Tasks;
|
|
|
|
+using Consul;
|
|
|
|
+using EasyCaching.Redis;
|
|
|
|
+using Microsoft.Extensions.DependencyInjection;
|
|
|
|
+using Microsoft.Extensions.Hosting;
|
|
|
|
+using Microsoft.Extensions.Logging;
|
|
|
|
+using Polly;
|
|
|
|
+using Polly.Registry;
|
|
|
|
+using Polly.Retry;
|
|
|
|
+using StackExchange.Redis;
|
|
|
|
+using XF.Domain.Dependency;
|
|
|
|
+using XF.Domain.Exceptions;
|
|
|
|
+using XF.Domain.Queues;
|
|
|
|
+
|
|
|
|
+namespace DataSharing.Application.Services
|
|
|
|
+{
|
|
|
|
+ public class SendTaskHandler : ISendTaskHandler, IScopeDependency
|
|
|
|
+ {
|
|
|
|
+ //private readonly ResiliencePipelineProvider<string> _pipelineProvider;
|
|
|
|
+ private readonly IQueue _queue;
|
|
|
|
+ private readonly ILogger<SendTaskHandler> _logger;
|
|
|
|
+
|
|
|
|
+ public SendTaskHandler(
|
|
|
|
+ //ResiliencePipelineProvider<string> pipelineProvider,
|
|
|
|
+ IQueue queue,
|
|
|
|
+ ILogger<SendTaskHandler> logger)
|
|
|
|
+ {
|
|
|
|
+ //_pipelineProvider = pipelineProvider;
|
|
|
|
+ _queue = queue;
|
|
|
|
+ _logger = logger;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public async Task SendAsync(int count, CancellationToken cancellationToken)
|
|
|
|
+ {
|
|
|
|
+ _logger.LogInformation($"==>执行send开始, count:{count}");
|
|
|
|
+ //2
|
|
|
|
+ //var pipeline = _pipelineProvider.GetPipeline(StrategyDefaults.RetryStrategy);
|
|
|
|
+
|
|
|
|
+ //await pipeline.ExecuteAsync(async token =>
|
|
|
|
+ //{
|
|
|
|
+ // var rd = Random.Shared.Next(1000, 10000);
|
|
|
|
+ // if (rd <= 5000)
|
|
|
|
+ // {
|
|
|
|
+ // throw new UserFriendlyException($"count: {count}, 执行失败");
|
|
|
|
+ // }
|
|
|
|
+ //});
|
|
|
|
+
|
|
|
|
+ //1
|
|
|
|
+ //var rd = Random.Shared.Next(1000, 10000);
|
|
|
|
+ //if (rd <= 5000)
|
|
|
|
+ //{
|
|
|
|
+ // throw new UserFriendlyException("执行失败");
|
|
|
|
+ //}
|
|
|
|
+
|
|
|
|
+ //await Task.Delay(rd, cancellationToken);
|
|
|
|
+
|
|
|
|
+ //3
|
|
|
|
+ // Create an instance of builder that exposes various extensions for adding resilience strategies
|
|
|
|
+ //ResiliencePipeline pipeline = new ResiliencePipelineBuilder()
|
|
|
|
+ // .AddRetry(new RetryStrategyOptions()) // Add retry using the default options
|
|
|
|
+ // .AddTimeout(TimeSpan.FromSeconds(10)) // Add 10 seconds timeout
|
|
|
|
+ // .Build(); // Builds the resilience pipeline
|
|
|
|
+
|
|
|
|
+ //// Execute the pipeline asynchronously
|
|
|
|
+ //await pipeline.ExecuteAsync(async token =>
|
|
|
|
+ //{
|
|
|
|
+ // var rd = Random.Shared.Next(1000, 10000);
|
|
|
|
+ // if (rd <= 5000)
|
|
|
|
+ // {
|
|
|
|
+ // throw new UserFriendlyException($"count: {count}, 执行失败");
|
|
|
|
+ // }
|
|
|
|
+ //});
|
|
|
|
+
|
|
|
|
+ //4
|
|
|
|
+ //await _queue.EnqueueAsync("test-queue", "111", cancellationToken);
|
|
|
|
+ //await _queue.EnqueueAsync("test-queue", "222", cancellationToken);
|
|
|
|
+ //await _queue.EnqueueAsync("test-queue", "333", cancellationToken);
|
|
|
|
+
|
|
|
|
+ //var a1 = await _queue.DequeueAsync<string>("test-queue", cancellationToken);
|
|
|
|
+ //var a2 = await _queue.DequeueAsync<string>("test-queue", cancellationToken);
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ _logger.LogInformation($"执行send结束, count:{count}<==");
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public interface ISendTaskHandler
|
|
|
|
+ {
|
|
|
|
+ Task SendAsync(int count, CancellationToken cancellationToken);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public class SendTaskService : BackgroundService
|
|
|
|
+ {
|
|
|
|
+ private readonly IServiceScopeFactory _serviceScopeFactory;
|
|
|
|
+
|
|
|
|
+ public SendTaskService(IServiceScopeFactory serviceScopeFactory)
|
|
|
|
+ {
|
|
|
|
+ _serviceScopeFactory = serviceScopeFactory;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|
|
|
+ {
|
|
|
|
+ using var scope = _serviceScopeFactory.CreateScope();
|
|
|
|
+ var provider = scope.ServiceProvider;
|
|
|
|
+ //var pipelineProvider = provider.GetRequiredService<ResiliencePipelineProvider<string>>();
|
|
|
|
+ //var pipeline = pipelineProvider.GetPipeline(StrategyDefaults.RetryStrategy);
|
|
|
|
+ var handler = provider.GetRequiredService<ISendTaskHandler>();
|
|
|
|
+
|
|
|
|
+ var count = 0;
|
|
|
|
+ while (!stoppingToken.IsCancellationRequested)
|
|
|
|
+ {
|
|
|
|
+ //await handler.SendAsync(count, stoppingToken);
|
|
|
|
+ ////await pipeline.ExecuteAsync(async token =>
|
|
|
|
+ ////{
|
|
|
|
+ //// Console.WriteLine($"execute count:{count}");
|
|
|
|
+ //// await handler.SendAsync(count, token);
|
|
|
|
+ ////});
|
|
|
|
+
|
|
|
|
+ //count++;
|
|
|
|
+ //await Task.Delay(1000, stoppingToken);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|