private async Task HandleMessage(string message) { if (string.IsNullOrWhiteSpace(message)) { _logger.LogWarning($"任务 {Id} 接收到空消息"); return; } _lastRequestedTime = DateTime.Now; var responses = JsonConvert.DeserializeObject <List <Response> >(message); if (responses.Count == 0) { _logger.LogWarning($"任务 {Id} 接收到空回复"); return; } var agentId = responses.First().AgentId; var successResponses = responses.Where(x => x.Success).ToList(); // 统计下载成功 if (successResponses.Count > 0) { var elapsedMilliseconds = successResponses.Sum(x => x.ElapsedMilliseconds); await _statisticsService.IncrementDownloadSuccessAsync(agentId, successResponses.Count, elapsedMilliseconds); } // 处理下载成功的请求 Parallel.ForEach(successResponses, async response => { _logger.LogInformation($"任务 {Id} 下载 {response.Request.Url} 成功"); var context = new DataFlowContext(_services.CreateScope().ServiceProvider); context.AddResponse(response); try { bool success = true; foreach (var dataFlow in _dataFlows) { var dataFlowResult = await dataFlow.HandleAsync(context); switch (dataFlowResult) { case DataFlowResult.Success: { continue; } case DataFlowResult.Failed: { _logger.LogError($"任务 {Id} 数据流处理器 {dataFlow.GetType().Name} 失败"); success = false; break; } case DataFlowResult.Terminated: { break; } } } var resultItems = context.GetItems(); // 如果解析结果为空,重试 if ((resultItems == null || resultItems.Sum(x => x.Value == null ? 0 : x.Value.Count) == 0) && RetryWhenResultIsEmpty) { response.Request.RetriedTimes++; response.Request.ComputeHash(); // 不需要添加总计 _scheduler.Enqueue(new[] { response.Request.Clone() }); } // 解析的目标请求 var followRequests = context.GetTargetRequests(); if (followRequests != null && followRequests.Count > 0) { var requests = new List <Request>(); foreach (var followRequest in followRequests) { followRequest.Depth = response.Request.Depth + 1; if (followRequest.Depth <= Depth) { requests.Add(followRequest); } } var count = _scheduler.Enqueue(requests); if (count > 0) { await _statisticsService.IncrementTotalAsync(Id, count); } } if (success) { await _statisticsService.IncrementSuccessAsync(Id); } else { await _statisticsService.IncrementFailedAsync(Id); } var result = success ? "成功" : $"失败: {context.Result}"; _logger.LogInformation($"任务 {Id} 处理 {response.Request.Url} {result}"); } catch (Exception e) { _logger.LogInformation($"任务 {Id} 处理 {response.Request.Url} 失败: {e}"); } }); var retryResponses = responses.Where(x => !x.Success && x.Request.RetriedTimes < RetryDownloadTimes) .ToList(); retryResponses.ForEach(x => { x.Request.RetriedTimes++; _logger.LogInformation($"任务 {Id} 下载 {x.Request.Url} 失败: {x.Exception}"); }); var failedRequests = responses.Where(x => !x.Success) .ToList(); // 统计下载失败 if (failedRequests.Count > 0) { await _statisticsService.IncrementFailedAsync(Id); await _statisticsService.IncrementDownloadFailedAsync(agentId, failedRequests.Count); } var retryCount = _scheduler.Enqueue(retryResponses.Select(x => x.Request.Clone())); if (retryCount > 0) { await _statisticsService.IncrementTotalAsync(Id, retryCount); } }