using AlibabaCloud.SDK.Vod20170321; using Coravel.Invocable; using Microsoft.AspNetCore.Http; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http.Json; using System.Reflection; using System.Security.Cryptography; using System.Security.Policy; using System.Text; using System.Text.Json; using System.Threading.Tasks; using UserCenter.Model.Enum; using VideoAnalysisCore.Common; using VideoAnalysisCore.Model; using VideoAnalysisCore.Model.Dto; using VideoAnalysisCore.Model.蓝鲸智库; using Yitter.IdGenerator; namespace VideoAnalysisCore.Job { /// /// [蓝鲸智库] 查找文件包未处理的视频 /// public class NodePackageJob : IInvocable { /// /// 每个扫描文件包每次取出{20}个 /// private readonly int TopLength = 20; private readonly Repository nodePackageInfoDB; private readonly Repository attachmentsDB; private readonly Repository videoTaskDB; private readonly IHttpClientFactory _clientFactory; public NodePackageJob(Repository videoTaskDB, Repository nodePackageInfoDB, Repository videotaskDB) { this.attachmentsDB = videoTaskDB; this.nodePackageInfoDB = nodePackageInfoDB; this.videoTaskDB = videotaskDB; } public async Task Invoke() { Console.WriteLine($"{DateTime.Now} 执行=>文件包任务"); var taskArr = await nodePackageInfoDB.AsQueryable() .Where(s => s.SuccessTime == null) .ToArrayAsync(); var videoIdArr = await videoTaskDB.AsQueryable() .Where(s => s.EndTime != null) .Select(s => s.TagId) .ToArrayAsync(); var videoPPTArr = await videoTaskDB.AsQueryable() .Where(s => s.EndTime != null) .Select(s => s.PPTVideoCode) .ToArrayAsync(); var postData = new List(); foreach (var item in taskArr) { if (videoIdArr.Contains(item.VideoCode)|| videoPPTArr.Contains(item.VideoCode)) { postData.Add(item); item.SuccessTime = DateTime.Now; } } Console.WriteLine($"{DateTime.Now} 执行=>文件包任务 任务回调 数量{postData.Count} ..."); if (postData.Count() == 0) return; foreach (var item in taskArr) { HttpResponseMessage responseMessage = null; try { var postUrl = !string.IsNullOrWhiteSpace(item.CallBackUrl) ? item.CallBackUrl : AppCommon.Config.Subsystem.蓝鲸智库.APIUrl + "/api/callback/platform/videosAnalyze"; var apiClent = _clientFactory.CreateClient(); var request = new HttpRequestMessage(HttpMethod.Post, postUrl); request.Headers.Add("Area", item.Area); // 直接添加到本次请求头 request.Headers.Add("HostIP", item.HostIP); // 直接添加到本次请求头 request.Content = new StringContent(JsonSerializer.Serialize(postData), Encoding.UTF8, "application/json"); responseMessage = await apiClent.SendAsync(request); if (responseMessage.IsSuccessStatusCode) { var res = await responseMessage.Content.ReadAsStringAsync(); Console.WriteLine($"{DateTime.Now} 执行=>文件包任务 回调结果 {res}"); await nodePackageInfoDB.AsUpdateable(postData) .UpdateColumns(it => new { it.SuccessTime }) .ExecuteCommandAsync(); } else { var res = await responseMessage.Content.ReadAsStringAsync(); Console.WriteLine($"{DateTime.Now} 执行=>文件包任务 回调失败!!! {responseMessage.StatusCode} {res}"); } } catch (Exception ex) { var res = await responseMessage?.Content?.ReadAsStringAsync()??string.Empty; Console.WriteLine($"{DateTime.Now} 执行=>文件包任务 回调失败!!! {responseMessage.StatusCode} {res}"); Console.WriteLine(ex); } await Task.Delay(1000); } Console.WriteLine($"{DateTime.Now} 执行=>文件包任务 已完成任务回调 数量{postData.Count}"); } } }