MemoryStorageProvider.cs 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. using System.Collections.Concurrent;
  2. using qdr.fnd.core.pqueue.Enums;
  3. using qdr.fnd.core.pqueue.Exceptions;
  4. using Quadarax.Foundation.Core.Data;
  5. using Quadarax.Foundation.Core.Logging;
  6. namespace qdr.fnd.core.pqueue.Storages
  7. {
  8. public class MemoryStorageProvider : BaseStorageProvider
  9. {
  10. private ConcurrentDictionary<string, IPsQueueItem> _items = new ConcurrentDictionary<string, IPsQueueItem>();
  11. /// <inheritdoc />
  12. public MemoryStorageProvider(ILogger? logger) : base(logger)
  13. {
  14. }
  15. #region Overrides of BaseStorageProvider
  16. #endregion
  17. #region Overrides of DisposableObject
  18. /// <inheritdoc />
  19. protected override void OnDisposing()
  20. {
  21. _items.Clear();
  22. _items = null!;
  23. }
  24. #endregion
  25. #region Overrides of BaseStorageProvider
  26. /// <inheritdoc />
  27. protected override string OnCreateItem(string id, string owner, string ownerRef, string providerClass, int initialAttempts,
  28. DateTime created, string s)
  29. {
  30. var item = new PsQueueItem(id, owner, ownerRef, providerClass, created)
  31. {
  32. AttemptsLeft = initialAttempts
  33. };
  34. if(!_items.TryAdd(id, item))
  35. Log?.Log(LogSeverityEnum.Warn, $"Item id={id} already exists in storage (id conflict)");
  36. return id;
  37. }
  38. /// <inheritdoc />
  39. protected override void OnDeleteItem(string itemId)
  40. {
  41. if(!_items.TryRemove(itemId, out _))
  42. Log?.Log(LogSeverityEnum.Warn, $"Item id={itemId} wasn't delete from storage (not exists)");
  43. }
  44. /// <inheritdoc />
  45. protected override void OnUpdateItemPriority(string itemId, int priority)
  46. {
  47. var item = (PsQueueItem)GetItem(itemId);
  48. if (item == null)
  49. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  50. item.Priority = priority;
  51. }
  52. /// <inheritdoc />
  53. protected override void OnUpdateItemValidity(string itemId, DateTime validForm, DateTime? validTo)
  54. {
  55. var item = (PsQueueItem)GetItem(itemId);
  56. if (item == null)
  57. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  58. item.ValidFrom = validForm;
  59. item.ValidTo = validTo;
  60. }
  61. /// <inheritdoc />
  62. protected override void OnUpdateItemStatus(string itemId, PsStatusEnum? status, bool isTransient)
  63. {
  64. var item = (PsQueueItem)GetItem(itemId);
  65. if (item == null)
  66. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  67. if (status!= null)
  68. item.Status = status.Value;
  69. item.IsStatusTransient = isTransient;
  70. }
  71. /// <inheritdoc />
  72. protected override void OnUpdateItemAttempts(string itemId, int attemptsLeft)
  73. {
  74. var item = (PsQueueItem)GetItem(itemId);
  75. if (item == null)
  76. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  77. item.AttemptsLeft = attemptsLeft;
  78. }
  79. /// <inheritdoc />
  80. protected override void OnUpdateItemStarted(string itemId, DateTime? started)
  81. {
  82. var item = (PsQueueItem)GetItem(itemId);
  83. if (item == null)
  84. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  85. item.Started = started;
  86. }
  87. /// <inheritdoc />
  88. protected override void OnUpdateItemFinished(string itemId, DateTime? finished)
  89. {
  90. var item = (PsQueueItem)GetItem(itemId);
  91. if (item == null)
  92. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  93. item.Finished = finished;
  94. }
  95. /// <inheritdoc />
  96. protected override void OnCreateItemAttribute(string itemId, string name, string? value, string valueTypeClass, bool isOutput)
  97. {
  98. var item = (PsQueueItem)GetItem(itemId);
  99. if (item == null)
  100. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  101. var type = Type.GetType(valueTypeClass);
  102. ((List<ITypedArgument>)item.Arguments).Add(new TypedArgument(name, value, type,isOutput));
  103. }
  104. /// <inheritdoc />
  105. protected override void OnUpdateItemAttribute(string itemId, string name, string value)
  106. {
  107. var item = (PsQueueItem)GetItem(itemId);
  108. if (item == null)
  109. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  110. var attr = item.Arguments.FirstOrDefault(a => string.Equals(a.Name , name,StringComparison.CurrentCultureIgnoreCase));
  111. if(attr== null)
  112. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemArgumentNotFound, itemId, name);
  113. ((TypedArgument)attr).Value = value;
  114. }
  115. /// <inheritdoc />
  116. protected override void OnDeleteItemAttribute(string itemId, string name)
  117. {
  118. var item = (PsQueueItem)GetItem(itemId);
  119. if (item == null)
  120. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  121. var attr = item.Arguments.FirstOrDefault(a => string.Equals(a.Name , name,StringComparison.CurrentCultureIgnoreCase));
  122. if(attr== null)
  123. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemArgumentNotFound, itemId, name);
  124. ((List<ITypedArgument>)item.Arguments).Remove(attr);
  125. }
  126. /// <inheritdoc />
  127. protected override void OnAppendLog(string itemId, string message, int code, ErrorLevelEnum messageType)
  128. {
  129. var item = (PsQueueItem)GetItem(itemId);
  130. if (item == null)
  131. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  132. ((List<IError>)item.Log).Add(new Error(message, code, messageType, DateTime.Now));
  133. }
  134. /// <inheritdoc />
  135. protected override void OnAppendStatusJournal(string itemId, PsStatusEnum status, bool isTransient, string message)
  136. {
  137. var item = (PsQueueItem)GetItem(itemId);
  138. if (item == null)
  139. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  140. ((List<PsStatusHistory>)item.StatusJournal).Add(new PsStatusHistory(status, isTransient,message));
  141. }
  142. /// <inheritdoc />
  143. protected override IQueryable<IPsQueueItem> OnQuery()
  144. {
  145. return _items.Values.AsQueryable();
  146. }
  147. /// <inheritdoc />
  148. protected override IPsQueueItem OnGetItem(string itemId)
  149. {
  150. _items.TryGetValue(itemId,out var item);
  151. if (item== null)
  152. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  153. return item;
  154. }
  155. /// <inheritdoc />
  156. public override bool ContainsItem(string itemId)
  157. {
  158. return _items.ContainsKey(itemId);
  159. }
  160. #endregion
  161. }
  162. }