PullDocumentsWorker.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO.Abstractions;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using BO.AppServer.Metadata.Enums;
  7. using BO.ProcessServer.Business.StatisticCounters;
  8. using BO.ProcessServer.Business.Workers.Contexts;
  9. using Connector.PS;
  10. using Quadarax.Foundation.Core.Logging;
  11. using Quadarax.Foundation.Core.Thread;
  12. namespace BO.ProcessServer.Business.Workers
  13. {
  14. internal class PullDocumentsWorker : LoopWorker
  15. {
  16. #region *** Constants ***
  17. private const string CS_WK_NAME = "PullDocument";
  18. #endregion
  19. #region *** Constructors ***
  20. public PullDocumentsWorker(IConnectionPS connection, TimeSpan loopEveryTimeSpan, IFileSystem fileSystem, Statistics statistics,Pool pool, ILogger logger) : base(CS_WK_NAME, new PullCtx(connection, fileSystem, statistics, pool), logger)
  21. {
  22. IterationsDelayBetween = loopEveryTimeSpan;
  23. }
  24. public PullDocumentsWorker(string workerName) : base(workerName)
  25. {
  26. }
  27. public PullDocumentsWorker(string workerName, IWorkerContext context, ILogger logger = null) : base(workerName, context, logger)
  28. {
  29. }
  30. #endregion
  31. #region *** Iteration ***
  32. protected override async Task DoIteration(IWorkerContext context)
  33. {
  34. var ctx = (PullCtx)context;
  35. var documents = await ctx.Connection.GetDocumentsAsync(DocumentsScopeEnums.Pending);
  36. foreach (var document in documents)
  37. {
  38. var downloadedFiles = new List<string>();
  39. foreach (var art in document.Artifacts.Where(x=>x.Type == ArtifactTypeEnum.Input))
  40. {
  41. await using (var content = await ctx.Connection.GetDocumentContentAsync(document.Id, art.Id))
  42. {
  43. var fileName = ctx.FileSystem.Path.ChangeExtension(art.Name, art.Extension);
  44. fileName = await ctx.Pool.PutDocumentAsync(document.Id, fileName, content);
  45. downloadedFiles.Add(fileName);
  46. Log(LogSeverityEnum.Info, $"Document '{document.Name}' [{document.Id}] file '{fileName}' downloaded. (size {content.Length} bytes)");
  47. }
  48. }
  49. if (!downloadedFiles.All(x => ctx.FileSystem.File.Exists(x)))
  50. {
  51. // not all downloaded -> rollback
  52. ctx.Pool.DeleteDocumentFiles(Pool.PoolTypeEnum.Pending, document.Id);
  53. await ctx.Connection.SetDocumentStateAsync(document.Id, DocumentStatusEnum.New);
  54. Log(LogSeverityEnum.Warn, $"Document '{document.Name}' [{document.Id}] roll-backed.");
  55. }
  56. }
  57. }
  58. #endregion
  59. #region *** Private Operations ***
  60. #endregion
  61. }
  62. }