GenPDFService.cs 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916
  1. using Azure.Messaging.ServiceBus;
  2. using Azure.Storage.Blobs.Models;
  3. using Microsoft.Azure.Cosmos;
  4. using StackExchange.Redis;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.ComponentModel;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Net;
  11. using System.Net.Http;
  12. using System.Net.Http.Json;
  13. using System.Text;
  14. using System.Text.Json;
  15. using System.Text.Json.Nodes;
  16. using System.Threading.Tasks;
  17. using System.Web;
  18. using TEAMModelOS.SDK.DI;
  19. using TEAMModelOS.SDK.Extension;
  20. using static TEAMModelOS.SDK.CoreAPIHttpService;
  21. using TEAMModelOS.SDK.Models;
  22. using Microsoft.Extensions.Configuration;
  23. using System.Net.Http.Headers;
  24. using Azure.Storage.Sas;
  25. namespace TEAMModelOS.SDK
  26. {
  27. public static class GenPDFService
  28. {
  29. public static async Task<(PDFGenRedis genRedis, PDFGenQueue genQueue,string msg )> SentTask(AzureRedisFactory _azureRedis, AzureStorageFactory _azureStorage)
  30. {
  31. string msg = string.Empty;
  32. //从尾部弹出元素,队列先进先出
  33. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  34. var queueValue = await _azureRedis.GetRedisClient(8).ListRightPopAsync("PDFGen:Queue");
  35. PDFGenRedis genRedis = null;
  36. PDFGenQueue genQueue = null;
  37. if (queueValue!=default && queueValue.HasValue)
  38. {
  39. genQueue = queueValue.ToString().ToObject<PDFGenQueue>();
  40. RedisValue redisValue = await _azureRedis.GetRedisClient(8).HashGetAsync($"PDFGen:{genQueue.sessionId}", genQueue.id);
  41. if (redisValue!=default)
  42. {
  43. genRedis = redisValue.ToString().ToObject<PDFGenRedis>();
  44. //计算等待了多久的时间才开始生成。
  45. var wait = now- genRedis.join;
  46. genRedis.wait = wait;
  47. genRedis.status = 1;
  48. try
  49. {
  50. Uri uri = new Uri(genQueue.pageUrl);
  51. var query = HttpUtility.ParseQueryString(uri.Query);
  52. string? url = query["url"];
  53. if (!string.IsNullOrWhiteSpace(url))
  54. {
  55. url= HttpUtility.UrlDecode(url);
  56. uri = new Uri(url);
  57. string host = uri.Host;
  58. var blobServiceClient = _azureStorage.GetBlobServiceClient();
  59. if (host.Equals(blobServiceClient.Uri.Host))
  60. {
  61. // 获取容器名,它是路径的第一个部分
  62. string containerName = uri.Segments[1].TrimEnd('/');
  63. // 获取文件的完整同级目录,这是文件路径中除了文件名和扩展名之外的部分
  64. // 由于文件名是路径的最后一个部分,我们可以通过连接除了最后一个部分之外的所有部分来获取目录路径
  65. string directoryPath = string.Join("", uri.Segments, 2, uri.Segments.Length - 3);
  66. string? fileName = Path.GetFileNameWithoutExtension(uri.AbsolutePath);
  67. string blobPath = $"{directoryPath}{fileName}.pdf";
  68. var urlSas = _azureStorage.GetBlobSAS(containerName, blobPath, BlobSasPermissions.Write, hour: 1);
  69. genQueue.blobSas = urlSas.sas;
  70. genQueue.blobName=blobPath;
  71. genQueue.cntName=containerName;
  72. genQueue.blobFullUrl=urlSas.fullUri;
  73. }
  74. else
  75. {
  76. msg="数据地址与服务提供的站点不一致!";
  77. genRedis.status=3;
  78. genRedis.msg = msg;
  79. }
  80. }
  81. else
  82. {
  83. msg="数据地址解析异常!";
  84. genRedis.status=3;
  85. genRedis.msg = msg;
  86. }
  87. }
  88. catch (Exception ex)
  89. {
  90. msg=$"数据地址处理异常,异常信息:{ex.Message}";
  91. genRedis.status=3;
  92. genRedis.msg = ex.Message;
  93. }
  94. await _azureRedis.GetRedisClient(8).HashSetAsync($"PDFGen:{genQueue.sessionId}", genQueue.id, genRedis.ToJsonString());
  95. }
  96. else {
  97. msg="队列任务关联数据为空!";
  98. }
  99. }
  100. else
  101. {
  102. msg="队列暂无任务!";
  103. }
  104. return (genRedis, genQueue, msg);
  105. }
  106. public static async Task GenPdf( AzureRedisFactory _azureRedis, AzureCosmosFactory _azureCosmos,
  107. IConfiguration _configuration, IHttpClientFactory _httpClient, AzureStorageFactory _azureStorage,
  108. DingDing _dingDing, SnowflakeId _snowflakeId,string json )
  109. {
  110. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  111. Console.WriteLine($"開始:{now}");
  112. string apiUri = "http://52.130.252.100:13000";
  113. PDFGenQueue genQueueData =json.ToObject<PDFGenQueue>();
  114. RedisValue redisValue = await _azureRedis.GetRedisClient(8).HashGetAsync($"PDFGen:{genQueueData.sessionId}", genQueueData.id);
  115. PDFGenRedis genRedis = null;
  116. if (redisValue!=default)
  117. {
  118. genRedis = redisValue.ToString().ToObject<PDFGenRedis>();
  119. //计算等待了多久的时间才开始生成。
  120. var wait = now- genRedis.join;
  121. genRedis.wait = wait;
  122. genRedis.status= 1;
  123. await _azureRedis.GetRedisClient(8).HashSetAsync($"PDFGen:{genQueueData.sessionId}", genQueueData.id, genRedis.ToJsonString());
  124. var client = _httpClient.CreateClient();
  125. ///再加5秒
  126. client.Timeout= TimeSpan.FromMilliseconds(genQueueData.timeout+ genQueueData.delay+5000);
  127. try
  128. {
  129. string urlpdf = $"{apiUri}/api/pdf";
  130. string jsonElement = new
  131. {
  132. pageUrl = genQueueData.pageUrl,
  133. timeout = genQueueData.timeout,
  134. delay = genQueueData.delay,
  135. checkPageCompleteJs = genQueueData.checkPageCompleteJs
  136. }.ToJsonString();
  137. var request = new HttpRequestMessage
  138. {
  139. Method = new HttpMethod("POST"),
  140. RequestUri = new Uri(urlpdf),
  141. Content = new StringContent(jsonElement)
  142. };
  143. var mediaTypeHeader = new MediaTypeHeaderValue("application/json")
  144. {
  145. CharSet = "UTF-8"
  146. };
  147. request.Content.Headers.ContentType = mediaTypeHeader;
  148. HttpResponseMessage responseMessage = await client.SendAsync(request);
  149. if (responseMessage.IsSuccessStatusCode)
  150. {
  151. string content = await responseMessage.Content.ReadAsStringAsync();
  152. JsonNode jsonNode = content.ToObject<JsonNode>();
  153. var code = jsonNode["code"];
  154. var file = jsonNode["data"]?["file"];
  155. if (code!=null && $"{code}".Equals("0") && file!= null && !string.IsNullOrWhiteSpace($"{file}"))
  156. {
  157. try
  158. {
  159. Stream stream = await client.GetStreamAsync($"{apiUri}/{file}");
  160. Uri uri = new Uri(genQueueData.pageUrl);
  161. var query = HttpUtility.ParseQueryString(uri.Query);
  162. string? url = query["url"];
  163. if (!string.IsNullOrWhiteSpace(url))
  164. {
  165. url= HttpUtility.UrlDecode(url);
  166. uri = new Uri(url);
  167. string host = uri.Host;
  168. var blobServiceClient = _azureStorage.GetBlobServiceClient();
  169. if (blobServiceClient.Uri.Host.Equals(host))
  170. {
  171. // 获取容器名,它是路径的第一个部分
  172. string containerName = uri.Segments[1].TrimEnd('/');
  173. // 获取文件的完整同级目录,这是文件路径中除了文件名和扩展名之外的部分
  174. // 由于文件名是路径的最后一个部分,我们可以通过连接除了最后一个部分之外的所有部分来获取目录路径
  175. string directoryPath = string.Join("", uri.Segments, 2, uri.Segments.Length - 3);
  176. string? fileName = Path.GetFileNameWithoutExtension(uri.AbsolutePath);
  177. string blobPath = $"{directoryPath}{fileName}.pdf";
  178. var blockBlob = _azureStorage.GetBlobContainerClient(containerName).GetBlobClient(blobPath);
  179. string content_type = "application/octet-stream";
  180. ContentTypeDict.dict.TryGetValue(".pdf", out string? contenttype);
  181. if (!string.IsNullOrEmpty(contenttype))
  182. {
  183. content_type = contenttype;
  184. }
  185. await blockBlob.UploadAsync(stream, true);
  186. blockBlob.SetHttpHeaders(new BlobHttpHeaders { ContentType = content_type });
  187. genRedis.blob= blockBlob.Name;
  188. genRedis.status=2;
  189. }
  190. else
  191. {
  192. genRedis.status=6;
  193. }
  194. }
  195. else
  196. {
  197. genRedis.status=6;
  198. }
  199. }
  200. catch (Exception ex)
  201. {
  202. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,GenPDF()\n{ex.Message}\n{ex.StackTrace}\n\n{json}", GroupNames.醍摩豆服務運維群組);
  203. genRedis.status=6;
  204. }
  205. }
  206. else
  207. {
  208. if (code!= null && $"{code}".Equals("99999"))
  209. {
  210. genRedis.status= 4;
  211. }
  212. else
  213. {
  214. genRedis.status= 3;
  215. }
  216. }
  217. }
  218. else
  219. {
  220. genRedis.status= 3;
  221. }
  222. }
  223. catch (TaskCanceledException ex)
  224. {
  225. if (ex.CancellationToken.IsCancellationRequested)
  226. {
  227. // Console.WriteLine("请求被取消。");
  228. genRedis.status= 5;
  229. }
  230. else
  231. {
  232. //Console.WriteLine("请求超时。");
  233. genRedis.status= 4;
  234. }
  235. }
  236. catch (Exception ex)
  237. {
  238. genRedis.status=3;
  239. await _dingDing.SendBotMsg($"{Environment.GetEnvironmentVariable("Option:Location")}-ServiceBus,GenPDF()\n{ex.Message}\n{ex.StackTrace}\n\n{json}", GroupNames.醍摩豆服務運維群組);
  240. }
  241. }
  242. else
  243. {
  244. genRedis= new PDFGenRedis { id =genQueueData.id, status= 5, cost=0, join=now, wait=0, name= genQueueData.name };
  245. //被取消的
  246. }
  247. long nowNew = DateTimeOffset.Now.ToUnixTimeMilliseconds();
  248. genRedis.cost=nowNew-now;
  249. await _azureRedis.GetRedisClient(8).HashSetAsync($"PDFGen:{genQueueData.sessionId}", genQueueData.id, genRedis.ToJsonString());
  250. //如果全部 生成,需要发送通知
  251. HashEntry[] datas = await _azureRedis.GetRedisClient(8).HashGetAllAsync($"PDFGen:{genQueueData.sessionId}");
  252. List<PDFGenRedis> dbgenRedis = new List<PDFGenRedis>();
  253. List<string> notifyUsers = new List<string>();
  254. string taskName = string.Empty;
  255. string taskType = string.Empty;
  256. if (datas!= null && datas.Length > 0)
  257. {
  258. foreach (var item in datas)
  259. {
  260. if (!$"{item.Name}".Contains("notifyUsers"))
  261. {
  262. dbgenRedis.Add(item.Value.ToString().ToObject<PDFGenRedis>());
  263. }
  264. else
  265. {
  266. var jsonData = item.Value.ToString().ToObject<JsonElement>();
  267. notifyUsers= jsonData.GetProperty("notifyUsers").ToObject<List<string>>();
  268. taskType = jsonData.GetProperty("taskType").ToString();
  269. taskName = jsonData.GetProperty("taskName").ToString();
  270. }
  271. }
  272. }
  273. if (notifyUsers.IsNotEmpty())
  274. {
  275. string lang = Environment.GetEnvironmentVariable("Option:Location")!.Contains("China") ? "zh-cn" : "en-us";
  276. string sql = $"select c.id, c.name ,c.lang as code from c where c.id in ({string.Join(",", notifyUsers.Select(x => $"'{x}'"))})";
  277. List<IdNameCode> idNameCodes = new List<IdNameCode>();
  278. await foreach (var item in _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, Constant.Teacher)
  279. .GetItemQueryIteratorSql<IdNameCode>(queryText: sql, requestOptions: new QueryRequestOptions { PartitionKey = new PartitionKey("Base") }))
  280. {
  281. idNameCodes.Add(item);
  282. }
  283. idNameCodes.FindAll(x => string.IsNullOrWhiteSpace(x.code) || (!x.code.Equals("zh-cn") && !x.code.Equals("zh-tw") && !x.code.Equals("en-us"))).ForEach(x => { x.code = lang; });
  284. var clientID = _configuration.GetValue<string>("HaBookAuth:CoreService:clientID");
  285. var clientSecret = _configuration.GetValue<string>("HaBookAuth:CoreService:clientSecret");
  286. var url = _configuration.GetValue<string>("HaBookAuth:CoreAPI");
  287. string location = "China";
  288. if (Environment.GetEnvironmentVariable("Option:Location")!.Contains("China"))
  289. {
  290. location = "China";
  291. }
  292. else if (Environment.GetEnvironmentVariable("Option:Location")!.Contains("Global"))
  293. {
  294. location = "Global";
  295. }
  296. var token = await CoreTokenExtensions.CreateAccessToken(clientID, clientSecret, location);
  297. var client = _httpClient.CreateClient();
  298. if (client.DefaultRequestHeaders.Contains("Authorization"))
  299. {
  300. client.DefaultRequestHeaders.Remove("Authorization");
  301. client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}");
  302. }
  303. else
  304. {
  305. client.DefaultRequestHeaders.Add("Authorization", $"Bearer {token.AccessToken}");
  306. }
  307. //检查dbgenRedis的状态是否全部已经不是0和1
  308. var unfinished = dbgenRedis.FindAll(x => (x.status==0||x.status==1));
  309. if (unfinished==null || unfinished.Count==0)
  310. {
  311. // 数据大于60一个班,发送报告的总耗时等概要信息, 小于60的发送 生成报告的细则消息,小于五个的,可以列出报告的链接.
  312. long joinTime = dbgenRedis.Min(x => x.join);
  313. long totalTime = (now-joinTime)/1000;
  314. long costTime = dbgenRedis.Sum(x => x.cost)/1000;
  315. long avgTime = costTime / dbgenRedis.Count;
  316. long maxTime = dbgenRedis.Max(x => x.cost)/1000;
  317. long minTime = dbgenRedis.Min(x => x.cost)/1000;
  318. long statusOk = dbgenRedis.Where(x => x.status==2).Count();
  319. long statusFailed = dbgenRedis.Where(x => x.status!=2).Count();
  320. string key = "pdf-gen-notify-higher60";
  321. if (dbgenRedis.Count>60)
  322. {
  323. key = "pdf-gen-notify-higher60";
  324. }
  325. else
  326. {
  327. key ="pdf-gen-notify-below60";
  328. }
  329. foreach (var teacher in idNameCodes)
  330. {
  331. string path = Path.Combine("", $"Lang/{teacher.code}.json");
  332. var sampleJson = File.ReadAllText(path);
  333. JsonElement jsonElement = sampleJson.ToObject<JsonElement>();
  334. JsonElement msgsJson = jsonElement.GetProperty(key);
  335. List<string> msgs = msgsJson.ToObject<List<string>>();
  336. string msg1 = msgs[1].Replace("{tmdname}", teacher.name).Replace("{taskName}", taskName).Replace("{totalTime}", $"{totalTime}")
  337. .Replace("{avgTime}", $"{avgTime}").Replace("{maxTime}", $"{maxTime}").Replace("{minTime}", $"{minTime}").Replace("{statusOk}", $"{statusOk}").Replace("{statusFailed}", $"{statusFailed}");
  338. StringBuilder sb = new StringBuilder($"{msg1}");
  339. if (dbgenRedis.Count<=60)
  340. {
  341. string status = string.Empty;
  342. dbgenRedis.ForEach(x => {
  343. switch (x.status)
  344. {
  345. case 0:
  346. status= teacher.code.Equals("zh-cn") ? "未执行" : teacher.code.Equals("zh-tw") ? "未執行" : "unexecuted";
  347. break;
  348. case 1:
  349. status= teacher.code.Equals("zh-cn") ? "执行中" : teacher.code.Equals("zh-tw") ? "執行中" : "executing";
  350. break;
  351. case 2:
  352. status= teacher.code.Equals("zh-cn") ? "成功" : teacher.code.Equals("zh-tw") ? "成功" : "success";
  353. break;
  354. case 3:
  355. status= teacher.code.Equals("zh-cn") ? "失败" : teacher.code.Equals("zh-tw") ? "失敗" : "failed";
  356. break;
  357. case 4:
  358. status= teacher.code.Equals("zh-cn") ? "超时" : teacher.code.Equals("zh-tw") ? "超時" : "timeout";
  359. break;
  360. case 5:
  361. status= teacher.code.Equals("zh-cn") ? "取消" : teacher.code.Equals("zh-tw") ? "取消" : "canceled";
  362. break;
  363. case 6:
  364. status= teacher.code.Equals("zh-cn") ? "存放异常" : teacher.code.Equals("zh-tw") ? "存放異常" : "SaveError";
  365. break;
  366. default:
  367. status= teacher.code.Equals("zh-cn") ? "失败" : teacher.code.Equals("zh-tw") ? "失敗" : "failed";
  368. break;
  369. }
  370. string msg2 = msgs[2].Replace("{studentName}", x.name).Replace("{status}", $"{status}").Replace("{wait}", $"{x.wait/1000}").Replace("{cost}", $"{x.cost/1000}").Replace("{total}", $"{(x.wait+x.cost)/1000}");
  371. sb.Append(msg2);
  372. });
  373. }
  374. NotifyData notifyData = new NotifyData
  375. {
  376. hubName = "hita5",
  377. sender = "IES",
  378. tags = new List<string> { $"{teacher.id}_{Constant.NotifyType_IES5_Course}" },
  379. title = msgs[0],
  380. eventId = $"{key}_{_snowflakeId.NextId()}",
  381. eventName =msgs[0],
  382. data = "{\"value\":{}}",
  383. body=sb.ToString(),
  384. };
  385. string result = "";
  386. try
  387. {
  388. HttpResponseMessage responseMessage = await client.PostAsJsonAsync($"{url}/service/PushNotify", notifyData);
  389. if (responseMessage.StatusCode == HttpStatusCode.OK)
  390. {
  391. string content = await responseMessage.Content.ReadAsStringAsync();
  392. result = content;
  393. }
  394. else
  395. {
  396. result = $"{responseMessage.StatusCode},推送返回的状态码。";
  397. }
  398. }
  399. catch (Exception exm)
  400. {
  401. _= _dingDing.SendBotMsg($"{location}站点发送消息异常,{exm.Message}\n{exm.StackTrace}:\n{url}/service/PushNotify \nheader: {token.AccessToken} \nresult:{result}\n params:{notifyData.ToJsonString()}", GroupNames.成都开发測試群組);
  402. }
  403. }
  404. }
  405. }
  406. Console.WriteLine($"結束:{now}");
  407. return;
  408. }
  409. /// <summary>
  410. /// 加入PDF生成队列服务
  411. /// https://github.com/wuxue107/bookjs-eazy https://github.com/wuxue107/screenshot-api-server
  412. /// </summary>
  413. /// <param name="azureServiceBus"></param>
  414. /// <param name="azureRedis"></param>
  415. /// <param name="data"></param>
  416. /// <returns></returns>
  417. public static async Task<(int total,int add )> AddGenPdfQueue(AzureServiceBusFactory azureServiceBus , AzureRedisFactory azureRedis, GenPDFData data)
  418. {
  419. long now = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  420. List<PDFGenRedis> genRedis = new List<PDFGenRedis>();
  421. List<PDFGenRedis> dbgenRedis = new List<PDFGenRedis>();
  422. HashEntry[] datas = await azureRedis.GetRedisClient(8).HashGetAllAsync($"PDFGen:{data.sessionId}");
  423. List<string> notifyUsers = new List<string>();
  424. if (datas!= null && datas.Length > 0)
  425. {
  426. foreach (var item in datas)
  427. {
  428. if (!$"{item.Name}".Contains("notifyUsers"))
  429. {
  430. dbgenRedis.Add(item.Value.ToString().ToObject<PDFGenRedis>());
  431. }
  432. else {
  433. var json = item.Value.ToString().ToObject<JsonElement>();
  434. notifyUsers= json.GetProperty("notifyUsers").ToObject<List<string>>();
  435. }
  436. }
  437. }
  438. if (data.notifyUsers.IsNotEmpty())
  439. {
  440. notifyUsers.AddRange(data.notifyUsers);
  441. notifyUsers= notifyUsers.Distinct().ToList();
  442. }
  443. var dataVal = new GenPDFSchema { notifyUsers= notifyUsers, taskName= data.taskName, taskType = data.taskType };
  444. //Redis记录需要通知的用户
  445. await azureRedis.GetRedisClient(8).HashSetAsync($"PDFGen:{data.sessionId}", "notifyUsers",JsonSerializer.Serialize(dataVal));
  446. //Redis记录需要生成PDF的数量
  447. int countProcess= 0;
  448. foreach (var item in data.datas)
  449. {
  450. var dbData = dbgenRedis.Find(x => x.id.Equals(item.id));
  451. if (dbData!=null)
  452. {
  453. if (dbData.status==0|| dbData.status==1)
  454. {
  455. //不变的
  456. countProcess+=1;
  457. }
  458. else
  459. {
  460. //需要变更的
  461. dbData.status = 0;
  462. dbData.cost=0;
  463. dbData.join= now;
  464. dbData.wait=0;
  465. dbData.name=item.name;
  466. dbData.url= item.url;
  467. dbData.scope = data.scope;
  468. dbData.owner= data.owner;
  469. genRedis.Add(dbData);
  470. }
  471. }
  472. else {
  473. genRedis.Add(new PDFGenRedis {
  474. id = item.id,
  475. status=0,
  476. cost=0,
  477. wait=0,
  478. join=now,
  479. name=item.name,
  480. url= item.url,
  481. scope=data.scope,
  482. owner= data.owner
  483. });
  484. }
  485. }
  486. //过期时间 当前个数+ reddis的个数
  487. foreach (var item in genRedis)
  488. {
  489. countProcess+=1;
  490. await azureRedis.GetRedisClient(8).HashSetAsync($"PDFGen:{data.sessionId}", item.id, item.ToJsonString());
  491. PDFGenQueue genQueue = new PDFGenQueue
  492. {
  493. id = item.id,
  494. checkPageCompleteJs= data.checkPageCompleteJs,
  495. delay=data.delay,
  496. html= data.html,
  497. pageUrl= $"{data.pageUrl}?url={HttpUtility.UrlEncode(item.url)}",
  498. sessionId= data.sessionId,
  499. timeout =data.timeout,
  500. name=item.name,
  501. };
  502. //string message = JsonSerializer.Serialize(genQueue);
  503. //从头部压入元素,队列先进先出
  504. await azureRedis.GetRedisClient(8).ListLeftPushAsync($"PDFGen:Queue", genQueue.ToJsonString());
  505. //var serviceBusMessage = new ServiceBusMessage(message);
  506. //serviceBusMessage.ApplicationProperties.Add("name", "BlobRoot");
  507. //await azureServiceBus.GetServiceBusClient().SendMessageAsync("dep-genpdf", serviceBusMessage);
  508. }
  509. long expire = (data.delay + data.timeout) * countProcess+30*60*1000;
  510. var tiemSpan = TimeSpan.FromMilliseconds(expire);
  511. await azureRedis.GetRedisClient(8).KeyExpireAsync($"PDFGen:{data.sessionId}", tiemSpan);
  512. return ( countProcess , genRedis.Count() );
  513. }
  514. }
  515. public class GenPDFData
  516. {
  517. /// <summary>
  518. /// 数据装载后的页面 要制作为PDF的网页 (pageUrl 、html 参数二选一)
  519. /// </summary>
  520. public string pageUrl { get; set; }
  521. /// <summary>
  522. /// 要截图的网页HTML (pageUrl 、html 参数二选一) "html" : "<div>bookjs-eazy</div>",
  523. /// </summary>
  524. public string html { get; set; }
  525. /// <summary>
  526. /// 超时时间
  527. /// </summary>
  528. public long timeout { get; set; } = 30000;
  529. /// <summary>
  530. /// 页面完成后(checkPageCompleteJs返回为true后)延迟的时间,可选,默认:0
  531. /// </summary>
  532. public long delay { get; set; } = 2000;
  533. /// <summary>
  534. /// // 检查页面是否渲染完成的js表达式,可选,默认: "true"
  535. /// </summary>
  536. public bool checkPageCompleteJs { get; set; }
  537. /// <summary>
  538. /// 生成会话id, 活动id
  539. /// </summary>
  540. public string sessionId { get; set; }
  541. public List<PDFData> datas { get; set; } = new List<PDFData>();
  542. /// <summary>
  543. /// 通知用户
  544. /// </summary>
  545. public List<string> notifyUsers { get; set; } = new List<string>();
  546. /// <summary>
  547. /// 活动的名称
  548. /// </summary>
  549. public string taskName { get; set; }
  550. /// <summary>
  551. /// 艺术评测报告Art,评测报告Exam,问卷报告Survey,投票报告Vote,为空 则无法回调更新状态
  552. /// </summary>
  553. public string taskType { get; set;}
  554. /// <summary>
  555. /// 数据所有者
  556. /// </summary>
  557. public string owner { get; set; }
  558. /// <summary>
  559. /// 数据范围
  560. /// </summary>
  561. public string scope { get; set; }
  562. }
  563. public class GenPDFSchema
  564. {
  565. public List<string> notifyUsers { get; set; } = new List<string>();
  566. public string taskName { get; set; }
  567. public string taskType { get; set; }
  568. }
  569. public class PDFData{
  570. /// <summary>
  571. /// 学生id
  572. /// </summary>
  573. public string id { get; set; }
  574. /// <summary>
  575. /// 数据链接
  576. /// </summary>
  577. public string url { get; set; }
  578. /// <summary>
  579. /// 学生名称
  580. /// </summary>
  581. public string name { get; set; }
  582. }
  583. /// <summary>
  584. /// redis 存储数据,超时时间设置为status=0 的count timeout* count+ delay*count
  585. /// </summary>
  586. public class PDFGenRedis
  587. {
  588. /// <summary>
  589. /// 学生id
  590. /// </summary>
  591. public string id { get; set; }
  592. /// <summary>
  593. /// 学生名称
  594. /// </summary>
  595. public string name { get; set; }
  596. /// <summary>
  597. /// 执行生成 毫秒
  598. /// </summary>
  599. public long cost { get; set;}
  600. /// <summary>
  601. /// 等候时间
  602. /// </summary>
  603. public long wait { get; set;}
  604. /// <summary>
  605. /// 加入时间
  606. /// </summary>
  607. public long join { get; set; }
  608. /// <summary>
  609. /// 上传成功后的文件
  610. /// </summary>
  611. public string blob { get; set; }
  612. /// <summary>
  613. /// 數據的url
  614. /// </summary>
  615. public string url { get; set; }
  616. /// <summary>
  617. /// 0 未执行,1 执行中,2 执行成功,3 执行失败,4超时,5 取消,6 存放异常
  618. /// </summary>
  619. public int status { get; set; } = 0;
  620. /// <summary>
  621. /// 状态信息
  622. /// </summary>
  623. public string msg { get; set; }
  624. /// <summary>
  625. /// 数据所有者
  626. /// </summary>
  627. public string owner { get; set; }
  628. /// <summary>
  629. /// 数据范围
  630. /// </summary>
  631. public string scope { get; set; }
  632. }
  633. /// <summary>
  634. /// // 拼接上接口的前缀 http://localhost:3000/ 就是完整PDF地址
  635. // http://localhost:3000/pdf/1614458263411-glduu.pdf
  636. // 拼接上接口的前缀 http://localhost:3000/download/可以就可生成在浏览器上的下载链接
  637. // http://localhost:3000/download/pdf/1614458263411-glduu.pdf
  638. // 拼接上http://localhost:3000/static/js/pdfjs/web/viewer.html?file=/pdf/1614458263411-glduu.pdf
  639. // 可使用pdfjs库进行预览
  640. //
  641. //## -e MAX_BROWSER=[num] 环境变量可选,最大的puppeteer实例数,忽略选项则默认值:1 , 值auto:[可用内存]/200M
  642. //## -e PDF_KEEP_DAY=[num] 自动删除num天之前产生的文件目录,默认0: 不删除文件
  643. //docker run -p 13000:3000 -td --rm -e MAX_BROWSER=2 -e PDF_KEEP_DAY=1 -v ${PWD}:/screenshot-api-server/public --name=screenshot-api-server wuxue107/screenshot-api-server
  644. /// </summary>
  645. public class PDFGenQueue
  646. {
  647. /// <summary>
  648. /// 姓名
  649. /// </summary>
  650. public string name { get; set; }
  651. /// <summary>
  652. /// 学生id
  653. /// </summary>
  654. public string id { get; set; }
  655. /// <summary>
  656. /// 数据装载后的页面 要制作为PDF的网页 (pageUrl 、html 参数二选一)
  657. /// </summary>
  658. public string pageUrl { get; set; }
  659. /// <summary>
  660. /// 要截图的网页HTML (pageUrl 、html 参数二选一) "html" : "<div>bookjs-eazy</div>",
  661. /// </summary>
  662. public string html { get; set; }
  663. /// <summary>
  664. /// 超时时间
  665. /// </summary>
  666. public long timeout { get; set; } = 30000;
  667. /// <summary>
  668. /// 页面完成后(checkPageCompleteJs返回为true后)延迟的时间,可选,默认:0
  669. /// </summary>
  670. public long delay { get; set; } = 2000;
  671. /// <summary>
  672. /// // 检查页面是否渲染完成的js表达式,可选,默认: "true"
  673. /// </summary>
  674. public bool checkPageCompleteJs { get; set; }
  675. /// <summary>
  676. /// 生成会话id, 活动id
  677. /// </summary>
  678. public string sessionId { get; set; }
  679. /// <summary>
  680. /// blob的sas
  681. /// </summary>
  682. public string blobSas { get; set; }
  683. /// <summary>
  684. /// blob名称
  685. /// </summary>
  686. public string blobName { get; set; }
  687. /// <summary>
  688. /// 容器名称
  689. /// </summary>
  690. public string cntName { get; set; }
  691. /// <summary>
  692. /// 完整blob地址
  693. /// </summary>
  694. public string blobFullUrl { get; set; }
  695. }
  696. public class ScreenClient : ClientDevice
  697. {
  698. /// <summary>
  699. /// 授权类型,bookjs_api
  700. /// </summary>
  701. public string? grant_type { get; set; }
  702. /// <summary>
  703. /// 客户端id
  704. /// </summary>
  705. public string? clientid { get; set; }
  706. /// <summary>
  707. /// SignalR的连接ID 不建议暴露。
  708. /// </summary>
  709. public string? connid { get; set; }
  710. /// <summary>
  711. /// 状态 busy 忙碌,free 空闲,down 离线,error 错误
  712. /// </summary>
  713. public string? status { get; set; }
  714. /// <summary>
  715. /// 最后更新时间
  716. /// </summary>
  717. public long last_time { get; set; }
  718. /// <summary>
  719. /// 任务完成数
  720. /// </summary>
  721. public int taskComplete { get; set; }
  722. }
  723. public class SignalRClient
  724. {
  725. /// <summary>
  726. /// 授权类型,bookjs_api
  727. /// </summary>
  728. public string? grant_type { get; set; }
  729. /// <summary>
  730. /// 客户端id
  731. /// </summary>
  732. public string? clientid { get; set; }
  733. /// <summary>
  734. /// SignalR的连接ID 不建议暴露。
  735. /// </summary>
  736. public string? connid { get; set; }
  737. }
  738. public interface IClient
  739. {
  740. Task ReceiveMessage(MessageBody message);
  741. Task ReceiveConnection(MessageBody message);
  742. Task ReceiveDisConnection(MessageBody message);
  743. }
  744. public abstract class MessageBody
  745. {
  746. public MessageBody()
  747. {
  748. time = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
  749. }
  750. /// <summary>
  751. /// 连接id
  752. /// </summary>
  753. public virtual string? connid { get; set; }
  754. /// <summary>
  755. /// 客户端id
  756. /// </summary>
  757. public virtual string? clientid { get; set; }
  758. /// <summary>
  759. /// 状态 busy 忙碌,free 空闲,down 离线,error 错误
  760. /// </summary>
  761. public virtual string? status { get; set; }
  762. /// <summary>
  763. /// 消息内容
  764. /// </summary>
  765. public virtual string? content { get; set; }
  766. /// <summary>
  767. /// 消息创建时间
  768. /// </summary>
  769. public virtual long time { get; }
  770. /// <summary>
  771. /// 授权类型,bookjs_api
  772. /// </summary>
  773. public virtual string? grant_type { get; set; }
  774. /// <summary>
  775. /// 消息类型
  776. /// </summary>
  777. public virtual MessageType message_type { get; set; }
  778. }
  779. /// <summary>
  780. /// 连接消息
  781. /// </summary>
  782. public class ConnectionMessage : MessageBody
  783. {
  784. }
  785. /// <summary>
  786. /// 断开连接消息
  787. /// </summary>
  788. public class DisConnectionMessage : MessageBody
  789. {
  790. }
  791. /// <summary>
  792. /// 业务处理消息
  793. /// </summary>
  794. public class ScreenProcessMessage : MessageBody
  795. {
  796. public string msg { get; set; }
  797. public int result { get; set; }
  798. }
  799. public static class ScreenConstant
  800. {
  801. public static readonly string busy = "busy";
  802. public static readonly string idle = "idle";
  803. public static readonly string error = "error";
  804. public static readonly string offline = "offline";
  805. public static readonly string grant_type = "bookjs_api";
  806. /// <summary>
  807. /// 冗余时间
  808. /// </summary>
  809. public static readonly long time_excess = 5000;
  810. }
  811. public enum MessageType {
  812. conn_success,//连接成功
  813. conn_error,// 连接失败
  814. task_send_success,// 任务发送成功
  815. task_send_error,// 任务发送失败
  816. task_execute_success,// 任务执行成功
  817. task_execute_error,// 任务执行失败
  818. }
  819. public class ClientDevice
  820. {
  821. /// <summary>
  822. /// 机器名
  823. /// </summary>
  824. public string? name { get; set; }
  825. /// <summary>
  826. /// 操作系统
  827. /// </summary>
  828. public string? os { get; set; }
  829. /// <summary>
  830. /// 远程ip
  831. /// </summary>
  832. public string? remote { get; set; }
  833. /// <summary>
  834. /// 端口,可能有多个端口
  835. /// </summary>
  836. public string? port { get; set; }
  837. /// <summary>
  838. /// 地区
  839. /// </summary>
  840. public string? region { get; set; }
  841. /// <summary>
  842. /// 网卡 IP信息
  843. /// </summary>
  844. public List<Network> networks { get; set; } = new List<Network>();
  845. /// <summary>
  846. /// 超时时间,单位毫秒
  847. /// </summary>
  848. public long timeout { get; set; } = 30000;
  849. /// <summary>
  850. /// 延迟时间,单位毫秒
  851. /// </summary>
  852. public long delay { get; set; } = 3000;
  853. /// <summary>
  854. /// PDF服务地址
  855. /// </summary>
  856. public string? screenUrl { get; set; }
  857. }
  858. public class Network
  859. {
  860. public string? mac { get; set; }
  861. public string? ip { get; set; }
  862. }
  863. }