ProcessQueue.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Quadarax.Foundation.Core.Business.Processor.Enums;
  7. using Quadarax.Foundation.Core.Business.Processor.Providers;
  8. using Quadarax.Foundation.Core.Business.Processor.Task;
  9. using Quadarax.Foundation.Core.Logging;
  10. using Quadarax.Foundation.Core.Object;
  11. using Quadarax.Foundation.Core.Value.Extensions;
  12. namespace Quadarax.Foundation.Core.Business.Processor.Queue
  13. {
  14. public class ProcessQueue : DisposableObject
  15. {
  16. #region *** Properties ***
  17. public bool IsRunning { get; private set; }
  18. public string QueueIdentifier { get; private set; }
  19. #endregion
  20. #region *** Private fields ***
  21. private readonly CallbackDelegates _delegates;
  22. private readonly ILog _log;
  23. private readonly ProcessQueueConfiguration _configuration;
  24. private readonly IProcessorStorageProvider _storageProvider;
  25. private bool _isTransient;
  26. #endregion
  27. #region *** Constructors ***
  28. public ProcessQueue(string processQueueIdentifierToken, IProcessorStorageProvider storage, ProcessQueueConfiguration configuration, CallbackDelegates? delegates = null)
  29. {
  30. if (string.IsNullOrEmpty(processQueueIdentifierToken)) throw new ArgumentNullException(nameof(processQueueIdentifierToken));
  31. QueueIdentifier = processQueueIdentifierToken;
  32. _delegates = delegates ?? new CallbackDelegates();
  33. _log = new ConsoleLogger().GetLogger(GetType());
  34. _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
  35. _storageProvider = storage ?? throw new ArgumentNullException(nameof(storage));
  36. _configuration.Validate();
  37. }
  38. #endregion
  39. #region *** Public Operations ***
  40. public void Start()
  41. {
  42. if (IsRunning || _isTransient)
  43. {
  44. Log(LogSeverityEnum.Warn, $"ProcessQueue [{QueueIdentifier}] already running! Skip start.");
  45. return;
  46. }
  47. _isTransient = true;
  48. var now = DateTime.Now;
  49. Log(LogSeverityEnum.Debug, $"ProcessQueue [{QueueIdentifier}] starting...");
  50. //TODO: start mechanism here
  51. Log(LogSeverityEnum.Info, $"ProcessQueue [{QueueIdentifier}] started @{DateTime.Now.Subtract(now).ToReadableString()}");
  52. _isTransient = false;
  53. IsRunning = true;
  54. }
  55. public void Stop()
  56. {
  57. if (!IsRunning || _isTransient)
  58. {
  59. Log(LogSeverityEnum.Warn, $"ProcessQueue [{QueueIdentifier}] already stopped! Skip stop.");
  60. return;
  61. }
  62. _isTransient = true;
  63. var now = DateTime.Now;
  64. Log(LogSeverityEnum.Debug, $"ProcessQueue [{QueueIdentifier}] stopping...");
  65. //TODO: start mechanism here
  66. Log(LogSeverityEnum.Info, $"ProcessQueue [{QueueIdentifier}] stopped @{DateTime.Now.Subtract(now).ToReadableString()}");
  67. _isTransient = false;
  68. IsRunning = false;
  69. }
  70. public ITaskIdentifier CreateTask(string reference, string providerClass, TypedArgument[] initialArguments, byte priority, int initialAttempts, TimeSpan attemptTimeout, DateTime validFrom, DateTime? validTo)
  71. {
  72. try
  73. {
  74. var task = new TaskItem(this,reference, providerClass,initialArguments,priority,initialAttempts,attemptTimeout,validFrom,validTo, null);
  75. var newId = _storageProvider.Create(task);
  76. task.Id = newId.Id;
  77. return task;
  78. }
  79. catch (Exception e)
  80. {
  81. var message = $"Error creating task [Ref:{reference}, Provider:{providerClass}] for queue [{QueueIdentifier}]";
  82. Log(LogSeverityEnum.Error, message, e);
  83. throw new Exception(message, e);
  84. }
  85. }
  86. public ITaskItem GetTask(ITaskIdentifier id)
  87. {
  88. if (id == null) throw new ArgumentNullException(nameof(id));
  89. var result = _storageProvider.Get(id).FirstOrDefault();
  90. return result == null ? throw new Exception($"Task [{id.Id}] not found!") : (ITaskItem)result;
  91. }
  92. public ITaskItem[] GetTask(ITaskIdentifier[] ids)
  93. {
  94. var result = _storageProvider.Get(ids);
  95. var diff = result.Select(x => x.Id).CompareDiff(ids.Select(x => x.Id));
  96. if (diff.Any())
  97. throw new Exception($"Tasks [{string.Join(",", diff)}] not found!");
  98. return (ITaskItem[])result;
  99. }
  100. public ITaskIdentifier[] QueryTasks(ProcessStatusEnum[] states,int maxItems, bool onlyTimeValid = true, bool includeDeleted = false)
  101. {
  102. if (states == null || !states.Any()) throw new ArgumentNullException(nameof(states));
  103. var now = DateTime.Now;
  104. if (!states.Contains(ProcessStatusEnum.Deleted) && includeDeleted)
  105. states = states.Concat(new[] { ProcessStatusEnum.Deleted }).ToArray();
  106. var items = _storageProvider.Query(states);
  107. if (onlyTimeValid)
  108. items = items.Where(x=> x.ProcessingValidFrom <= now && x.ProcessingValidTo.GetValueOrDefault(DateTime.MaxValue) >= now);
  109. var result = items.OrderBy(x => x.ProcessingValidFrom)
  110. .ThenBy(x => x.Priority)
  111. .Take(maxItems)
  112. .Select(x=>(ITaskIdentifier)x)
  113. .ToArray();
  114. Log(LogSeverityEnum.Debug, $"ProcessQueue [{QueueIdentifier}] Query tasks {result.Length} of {maxItems}@{DateTime.Now.Subtract(now).ToReadableString()}");
  115. return result;
  116. }
  117. #endregion
  118. #region *** Private Operations ***
  119. protected void Log(LogSeverityEnum type, string message, Exception? e = null)
  120. {
  121. if (type == LogSeverityEnum.Error || type == LogSeverityEnum.Fatal)
  122. {
  123. _delegates.OnProcessorError?.Invoke(message, e);
  124. }
  125. _log.Log(type, message,e);
  126. }
  127. protected override void OnDisposing()
  128. {
  129. if (IsRunning)
  130. Stop();
  131. _delegates?.Reset();
  132. Log(LogSeverityEnum.Debug, $"ProcessQueue [{QueueIdentifier}] disposed.");
  133. }
  134. #endregion
  135. }
  136. }