mirror of
https://gitee.com/ccnetcore/Yi
synced 2026-03-13 04:59:27 +08:00
134 lines
5.6 KiB
C#
134 lines
5.6 KiB
C#
|
|
using Microsoft.Extensions.Configuration;
|
|||
|
|
using Microsoft.Extensions.Hosting;
|
|||
|
|
using Microsoft.Extensions.Logging;
|
|||
|
|
using Newtonsoft.Json;
|
|||
|
|
using System;
|
|||
|
|
using System.Collections.Generic;
|
|||
|
|
using System.Linq;
|
|||
|
|
using System.Net;
|
|||
|
|
using System.Net.Http;
|
|||
|
|
using System.Text;
|
|||
|
|
using System.Threading;
|
|||
|
|
using System.Threading.Tasks;
|
|||
|
|
using Yi.Framework.Common.IOCOptions;
|
|||
|
|
using Yi.Framework.Common.Models;
|
|||
|
|
using Yi.Framework.Common.QueueModel;
|
|||
|
|
using Yi.Framework.Core;
|
|||
|
|
using Yi.Framework.Core.ConsulExtend;
|
|||
|
|
|
|||
|
|
namespace Yi.Framework.StaticPageProcessor
|
|||
|
|
{
|
|||
|
|
public class WarmupPageWorker : BackgroundService
|
|||
|
|
{
|
|||
|
|
private readonly IConfiguration _configuration;
|
|||
|
|
private readonly ILogger<WarmupPageWorker> _logger;
|
|||
|
|
private readonly RabbitMQInvoker _RabbitMQInvoker;
|
|||
|
|
private readonly AbstractConsulDispatcher _IConsulDispatcher = null;
|
|||
|
|
|
|||
|
|
public WarmupPageWorker(ILogger<WarmupPageWorker> logger, RabbitMQInvoker rabbitMQInvoker, IConfiguration configuration, AbstractConsulDispatcher consulDispatcher)
|
|||
|
|
{
|
|||
|
|
this._logger = logger;
|
|||
|
|
this._RabbitMQInvoker = rabbitMQInvoker;
|
|||
|
|
this._configuration = configuration;
|
|||
|
|
this._IConsulDispatcher = consulDispatcher;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
|||
|
|
{
|
|||
|
|
RabbitMQConsumerModel rabbitMQConsumerModel = new RabbitMQConsumerModel()
|
|||
|
|
{
|
|||
|
|
ExchangeName = RabbitMQExchangeQueueName.SKUWarmup_Exchange,
|
|||
|
|
QueueName = RabbitMQExchangeQueueName.SKUWarmup_Queue_StaticPage
|
|||
|
|
};
|
|||
|
|
HttpClient _HttpClient = new HttpClient();
|
|||
|
|
this._RabbitMQInvoker.RegistReciveAction(rabbitMQConsumerModel, message =>
|
|||
|
|
{
|
|||
|
|
string realUrl= this._IConsulDispatcher.GetAddress(this._configuration["DetailPageUrl"]);
|
|||
|
|
|
|||
|
|
SKUWarmupQueueModel skuWarmupQueueModel = JsonConvert.DeserializeObject<SKUWarmupQueueModel>(message);
|
|||
|
|
#region <EFBFBD><EFBFBD>ClearAll
|
|||
|
|
{
|
|||
|
|
string totalUrl = $"{realUrl}{0}.html?ActionHeader=ClearAll";
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var result = _HttpClient.GetAsync(totalUrl).Result;
|
|||
|
|
if (result.StatusCode == HttpStatusCode.OK)
|
|||
|
|
{
|
|||
|
|
this._logger.LogInformation($"{nameof(WarmupPageWorker)}.ClearAll succeed {totalUrl}");
|
|||
|
|
//return true;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
this._logger.LogWarning($"{nameof(WarmupPageWorker)}.ClearAll failed {totalUrl}");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
this._logger.LogError($"{nameof(WarmupPageWorker)}.ClearAll failed {totalUrl}, Exception:{ex.Message}");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
|
|||
|
|
#region Ȼ<EFBFBD><EFBFBD>ȫ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> Warmup
|
|||
|
|
{
|
|||
|
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>----<2D><><EFBFBD><EFBFBD>---<2D><>¼<EFBFBD><C2BC><EFBFBD>µ<EFBFBD>
|
|||
|
|
|
|||
|
|
int count = 100;//<2F><><EFBFBD>β<EFBFBD>ѯ
|
|||
|
|
int pageIndex = 1;//<2F><>ҳ<EFBFBD><D2B3>ҳ<EFBFBD><D2B3><EFBFBD>ߵ<EFBFBD>
|
|||
|
|
while (count == 100)
|
|||
|
|
{
|
|||
|
|
|
|||
|
|
|
|||
|
|
// -------------------> <20>˴<EFBFBD><CBB4><EFBFBD><EFBFBD>ɾ<EFBFBD>̬ҳ<CCAC><D2B3><EFBFBD>ƣ<EFBFBD>ͨ<EFBFBD><CDA8>ʹ<EFBFBD><CAB9>id<69><64>ʶ,<2C><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>service<63>IJ<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>id
|
|||
|
|
List<long> ids = new List<long>{ 1,2,3,4,5,6,7,8,9};
|
|||
|
|
// -------------------> <20>˴<EFBFBD><CBB4><EFBFBD><EFBFBD>ɾ<EFBFBD>̬ҳ<CCAC><D2B3><EFBFBD>ƣ<EFBFBD>ͨ<EFBFBD><CDA8>ʹ<EFBFBD><CAB9>id<69><64>ʶ,<2C><><EFBFBD><EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>service<63>IJ<EFBFBD>ѯ<EFBFBD><D1AF><EFBFBD><EFBFBD><EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD>id
|
|||
|
|
|
|||
|
|
|
|||
|
|
foreach (var id in ids)
|
|||
|
|
{
|
|||
|
|
string totalUrl = $"{realUrl}{id}.html";
|
|||
|
|
try
|
|||
|
|
{
|
|||
|
|
var result = _HttpClient.GetAsync(totalUrl).Result;
|
|||
|
|
if (result.StatusCode == HttpStatusCode.OK)
|
|||
|
|
{
|
|||
|
|
this._logger.LogInformation($"{nameof(WarmupPageWorker)}.Warmup succeed {totalUrl}");
|
|||
|
|
//return true;
|
|||
|
|
}
|
|||
|
|
else
|
|||
|
|
{
|
|||
|
|
this._logger.LogWarning($"{nameof(WarmupPageWorker)}.Warmup failed {totalUrl}");
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
catch (Exception ex)
|
|||
|
|
{
|
|||
|
|
var logModel = new LogModel()
|
|||
|
|
{
|
|||
|
|
OriginalClassName = this.GetType().FullName,
|
|||
|
|
OriginalMethodName = nameof(ExecuteAsync),
|
|||
|
|
Remark = "<22><>ʱ<EFBFBD><CAB1>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>־"
|
|||
|
|
};
|
|||
|
|
this._logger.LogError(ex, $"{nameof(WarmupPageWorker)}.Warmup failed {totalUrl}, Exception:{ex.Message}", JsonConvert.SerializeObject(logModel));
|
|||
|
|
return false;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
pageIndex++;
|
|||
|
|
count = ids.Count;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
#endregion
|
|||
|
|
return true;
|
|||
|
|
});
|
|||
|
|
await Task.CompletedTask;
|
|||
|
|
//while (!stoppingToken.IsCancellationRequested)
|
|||
|
|
//{
|
|||
|
|
// _logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
|||
|
|
// await Task.Delay(1000, stoppingToken);
|
|||
|
|
//}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|