PullDocumentsWorker.cs 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 BO.ProcessServer.Options;
  10. using Connector.PS;
  11. using Quadarax.Foundation.Core.Logging;
  12. using Quadarax.Foundation.Core.Thread;
  13. namespace BO.ProcessServer.Business.Workers
  14. {
  15. internal class PullDocumentsWorker : LoopWorker
  16. {
  17. #region *** Constants ***
  18. private const string CS_WK_NAME = "PullDocument";
  19. #endregion
  20. #region *** Constructors ***
  21. public PullDocumentsWorker(IConnectionPS connection, TimeSpan loopEveryTimeSpan, IFileSystem fileSystem,Configuration configuration, Statistics statistics,Pool pool, ILogger logger) : base(CS_WK_NAME, new PullCtx(connection, fileSystem,configuration, statistics, pool), logger)
  22. {
  23. IterationsDelayBetween = loopEveryTimeSpan;
  24. }
  25. public PullDocumentsWorker(string workerName) : base(workerName)
  26. {
  27. }
  28. public PullDocumentsWorker(string workerName, IWorkerContext context, ILogger logger = null) : base(workerName, context, logger)
  29. {
  30. }
  31. #endregion
  32. #region *** Iteration ***
  33. protected override async Task DoIteration(IWorkerContext context)
  34. {
  35. var ctx = (PullCtx)context;
  36. var scenarioMan = new Scenarios.Scenarios(ctx.Configuration, GetLogger());
  37. // get all pending document and set lock status (pending->working)
  38. var documents = await ctx.Connection.GetDocumentsAsync(DocumentsScopeEnums.Pending);
  39. foreach (var document in documents)
  40. {
  41. if (scenarioMan.ExistsScenario(document))
  42. {
  43. // scenario for document exists
  44. var downloadedFiles = new List<string>();
  45. foreach (var art in document.Artifacts.Where(x => x.Type == ArtifactTypeEnum.Input))
  46. {
  47. await using (var content = await ctx.Connection.GetDocumentContentAsync(document.Id, art.Id))
  48. {
  49. var fileName = ctx.FileSystem.Path.ChangeExtension(art.Name, art.Extension);
  50. fileName = await ctx.Pool.PutDocumentAsync(document.Id, fileName, content);
  51. downloadedFiles.Add(fileName);
  52. Log(LogSeverityEnum.Info,
  53. $"Document '{document.Name}' [{document.Id}] file '{fileName}' downloaded. (size {content.Length} bytes)");
  54. }
  55. }
  56. if (!downloadedFiles.All(x => ctx.FileSystem.File.Exists(x)))
  57. {
  58. // not all downloaded -> rollback
  59. ctx.Pool.DeleteDocumentFiles(Pool.PoolTypeEnum.Pending, document.Id);
  60. await ctx.Connection.SetDocumentStateAsync(document.Id, DocumentStatusEnum.New);
  61. Log(LogSeverityEnum.Warn, $"Document '{document.Name}' [{document.Id}] roll-backed.");
  62. }
  63. }
  64. else
  65. {
  66. // scenario for document doesn't exists => set document for pending (working->pending)
  67. await ctx.Connection.SetDocumentStateAsync(document.Id, DocumentStatusEnum.Pending);
  68. }
  69. }
  70. }
  71. #endregion
  72. #region *** Private Operations ***
  73. #endregion
  74. }
  75. }