BaseStorageProvider.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. using qdr.fnd.core.pqueue.Enums;
  2. using qdr.fnd.core.pqueue.Exceptions;
  3. using Quadarax.Foundation.Core.Data;
  4. using Quadarax.Foundation.Core.Logging;
  5. using Quadarax.Foundation.Core.Object;
  6. using Quadarax.Foundation.Core.Value.Generators;
  7. using System.Xml.Linq;
  8. namespace qdr.fnd.core.pqueue.Storages
  9. {
  10. public abstract class BaseStorageProvider : DisposableObject, IPsQueueStorage
  11. {
  12. #region *** Properties ***
  13. protected ILog? Log => _log;
  14. #endregion
  15. #region *** Private Fields ***
  16. private ILog? _log;
  17. private IIdGenerator<string> _tranGenerator;
  18. #endregion
  19. #region *** Constructors ***
  20. protected BaseStorageProvider(ILogger? logger)
  21. {
  22. _log = logger?.GetLogger(GetType().Name);
  23. _tranGenerator = new TinyHash(10);
  24. }
  25. #endregion
  26. #region Implementation of IPsQueueStorage
  27. /// <inheritdoc />
  28. public string CreateItem(string owner, string ownerRef, string providerClass, int initialAttempts, DateTime? created)
  29. {
  30. if (string.IsNullOrWhiteSpace(owner))
  31. throw new ArgumentNullException(nameof(owner));
  32. if (string.IsNullOrWhiteSpace(ownerRef))
  33. throw new ArgumentNullException(nameof(ownerRef));
  34. if (string.IsNullOrWhiteSpace(providerClass))
  35. throw new ArgumentNullException(nameof(providerClass));
  36. if (created == null)
  37. created = DateTime.Now;
  38. var id = _tranGenerator.NewId();
  39. var newId = OnCreateItem(id, owner, ownerRef, providerClass, initialAttempts, created.Value);
  40. _log?.Log(LogSeverityEnum.Trace, $"Item [id={id}, owner={owner}, ownerRef{ownerRef}] created.");
  41. return newId;
  42. }
  43. protected abstract string OnCreateItem(string id, string owner, string ownerRef, string providerClass, int initialAttempts, DateTime created);
  44. /// <inheritdoc />
  45. public void DeleteItem(string itemId)
  46. {
  47. if (string.IsNullOrWhiteSpace(itemId))
  48. throw new ArgumentNullException(nameof(itemId));
  49. if (!ContainsItem(itemId))
  50. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  51. OnDeleteItem(itemId);
  52. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] deleted.");
  53. }
  54. protected abstract void OnDeleteItem(string itemId);
  55. /// <inheritdoc />
  56. public void UpdateItemPriority(string itemId, int priority)
  57. {
  58. if (string.IsNullOrWhiteSpace(itemId))
  59. throw new ArgumentNullException(nameof(itemId));
  60. if (!ContainsItem(itemId))
  61. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  62. OnUpdateItemPriority(itemId, priority);
  63. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] priority[{priority}] updated.");
  64. }
  65. protected abstract void OnUpdateItemPriority(string itemId, int priority);
  66. /// <inheritdoc />
  67. public void UpdateItemValidity(string itemId, DateTime validForm, DateTime? validTo)
  68. {
  69. if (string.IsNullOrWhiteSpace(itemId))
  70. throw new ArgumentNullException(nameof(itemId));
  71. if (!ContainsItem(itemId))
  72. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  73. OnUpdateItemValidity(itemId, validForm, validTo);
  74. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] validFrom[{validForm}], validTo[{(validTo == null ? "NULL" : validTo.ToString())}] updated.");
  75. }
  76. protected abstract void OnUpdateItemValidity(string itemId, DateTime validForm, DateTime? validTo);
  77. /// <inheritdoc />
  78. public void UpdateItemStatus(string itemId, PsStatusEnum? status, bool isTransient)
  79. {
  80. if (string.IsNullOrWhiteSpace(itemId))
  81. throw new ArgumentNullException(nameof(itemId));
  82. if (!ContainsItem(itemId))
  83. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  84. OnUpdateItemStatus(itemId, status, isTransient);
  85. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] isTransient[{isTransient}], status[{(status == null ? "NULL" : status.ToString())}] updated.");
  86. }
  87. protected abstract void OnUpdateItemStatus(string itemId, PsStatusEnum? status, bool isTransient);
  88. /// <inheritdoc />
  89. public void UpdateItemAttempts(string itemId, int attemptsLeft)
  90. {
  91. if (string.IsNullOrWhiteSpace(itemId))
  92. throw new ArgumentNullException(nameof(itemId));
  93. if (!ContainsItem(itemId))
  94. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  95. OnUpdateItemAttempts(itemId, attemptsLeft);
  96. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] attemptsLeft[{attemptsLeft}] updated.");
  97. }
  98. protected abstract void OnUpdateItemAttempts(string itemId, int attemptsLeft);
  99. /// <inheritdoc />
  100. public void UpdateItemStarted(string itemId, DateTime? started)
  101. {
  102. if (string.IsNullOrWhiteSpace(itemId))
  103. throw new ArgumentNullException(nameof(itemId));
  104. if (!ContainsItem(itemId))
  105. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  106. OnUpdateItemStarted(itemId, started);
  107. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] started[{(started == null ? "NULL" : started.ToString())}] updated.");
  108. }
  109. protected abstract void OnUpdateItemStarted(string itemId, DateTime? started);
  110. /// <inheritdoc />
  111. public void UpdateItemFinished(string itemId, DateTime? finished)
  112. {
  113. if (string.IsNullOrWhiteSpace(itemId))
  114. throw new ArgumentNullException(nameof(itemId));
  115. if (!ContainsItem(itemId))
  116. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  117. OnUpdateItemFinished(itemId, finished);
  118. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] finished[{(finished == null ? "NULL" : finished.ToString())}] updated.");
  119. }
  120. protected abstract void OnUpdateItemFinished(string itemId, DateTime? finished);
  121. /// <inheritdoc />
  122. public void CreateItemAttribute(string itemId, string name, string? value, string valueTypeClass, bool isOutput)
  123. {
  124. if (string.IsNullOrWhiteSpace(itemId))
  125. throw new ArgumentNullException(nameof(itemId));
  126. var item = GetItem(itemId);
  127. if (item.Arguments.Any(x=>string.Equals(x.Name, name, StringComparison.CurrentCultureIgnoreCase)))
  128. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemArgumentAlreadyExists, itemId, name);
  129. OnCreateItemAttribute(itemId, name, value, valueTypeClass, isOutput);
  130. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] argument name[{name}], value[{value}], valueType[{valueTypeClass}] updated.");
  131. }
  132. protected abstract void OnCreateItemAttribute(string itemId, string name, string? value, string valueTypeClass, bool isOutput);
  133. /// <inheritdoc />
  134. public void UpdateItemAttribute(string itemId, string name, string value)
  135. {
  136. if (string.IsNullOrWhiteSpace(itemId))
  137. throw new ArgumentNullException(nameof(itemId));
  138. if (string.IsNullOrWhiteSpace(name))
  139. throw new ArgumentNullException(nameof(name));
  140. var item = GetItem(itemId);
  141. if (!item.Arguments.Any(x=>string.Equals(x.Name, name, StringComparison.CurrentCultureIgnoreCase)))
  142. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemArgumentNotFound, itemId, name);
  143. OnUpdateItemAttribute(itemId, name, value);
  144. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] argument name[{name}], value[{value}] updated.");
  145. }
  146. protected abstract void OnUpdateItemAttribute(string itemId, string name, string value);
  147. /// <inheritdoc />
  148. public void DeleteItemAttribute(string itemId, string name)
  149. {
  150. if (string.IsNullOrWhiteSpace(itemId))
  151. throw new ArgumentNullException(nameof(itemId));
  152. if (string.IsNullOrWhiteSpace(name))
  153. throw new ArgumentNullException(nameof(name));
  154. var item = GetItem(itemId);
  155. if (!item.Arguments.Any(x=>string.Equals(x.Name, name, StringComparison.CurrentCultureIgnoreCase)))
  156. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemArgumentNotFound, itemId, name);
  157. OnDeleteItemAttribute(itemId, name);
  158. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] argument name[{name}] deleted.");
  159. }
  160. protected abstract void OnDeleteItemAttribute(string itemId, string name);
  161. /// <inheritdoc />
  162. public void AppendLog(string itemId, string message, int code, ErrorLevelEnum messageType)
  163. {
  164. if (string.IsNullOrWhiteSpace(itemId))
  165. throw new ArgumentNullException(nameof(itemId));
  166. if (string.IsNullOrWhiteSpace(message))
  167. throw new ArgumentNullException(nameof(message));
  168. var item = GetItem(itemId);
  169. OnAppendLog(itemId, message, code, messageType);
  170. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] log message[{message}], code[{code}], messageType[{messageType}] appended.");
  171. }
  172. protected abstract void OnAppendLog(string itemId, string message, int code, ErrorLevelEnum messageType);
  173. /// <inheritdoc />
  174. public void AppendStatusJournal(string itemId, PsStatusEnum status, bool isTransient, string message)
  175. {
  176. if (string.IsNullOrWhiteSpace(itemId))
  177. throw new ArgumentNullException(nameof(itemId));
  178. var item = GetItem(itemId);
  179. OnAppendStatusJournal(itemId, status, isTransient, message);
  180. _log?.Log(LogSeverityEnum.Trace, $"Item [id={itemId}] log message[{message}], status[{status}], isTransient[{isTransient}] appended.");
  181. }
  182. protected abstract void OnAppendStatusJournal(string itemId, PsStatusEnum status, bool isTransient, string message);
  183. /// <inheritdoc />
  184. public IQueryable<IPsQueueItem> Query(string? owner, string? ownerRef, params Tuple<bool, PsStatusEnum>[] status)
  185. {
  186. var query = OnQuery();
  187. if (status.Length>0)
  188. query = query.Where(x => status.Any(s => s.Item1 == x.IsStatusTransient && s.Item2 == x.Status));
  189. if (!string.IsNullOrWhiteSpace(owner))
  190. query = query.Where(x => string.Equals(x.Owner, owner, StringComparison.InvariantCulture));
  191. if (!string.IsNullOrWhiteSpace(ownerRef))
  192. query = query.Where(x => string.Equals(x.OwnerRef, ownerRef, StringComparison.InvariantCulture));
  193. owner = owner ?? "NULL";
  194. ownerRef = ownerRef ?? "NULL";
  195. var statuses = string.Join(",", status.Select(x => $"[IsTransient:{x.Item1},Status:{x.Item2}]"));
  196. _log?.Log(LogSeverityEnum.Trace, $"Query [owner={owner}], ownerRef[{ownerRef}], statuses[{statuses}] executed.");
  197. return query;
  198. }
  199. protected abstract IQueryable<IPsQueueItem> OnQuery();
  200. /// <inheritdoc />
  201. public IPsQueueItem GetItem(string itemId)
  202. {
  203. if (string.IsNullOrWhiteSpace(itemId))
  204. throw new ArgumentNullException(nameof(itemId));
  205. if (!ContainsItem(itemId))
  206. throw new PSStorageException(PSStorageException.ErrorCodes.PsStorageItemNotFound, itemId);
  207. return OnGetItem(itemId);
  208. }
  209. protected abstract IPsQueueItem OnGetItem(string itemId);
  210. /// <inheritdoc />
  211. public abstract bool ContainsItem(string itemId);
  212. #endregion
  213. }
  214. }