PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/redistributable/openstack.net/src/corelib/Core/Domain/Queues/Claim.cs

https://gitlab.com/rekby-archive/onlyoffice-CommunityServer
C# | 304 lines | 138 code | 26 blank | 140 comment | 21 complexity | 6f451e9eee23afc4d23df2431ee2c531 MD5 | raw file
  1. namespace net.openstack.Core.Domain.Queues
  2. {
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Collections.ObjectModel;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using net.openstack.Core;
  9. using net.openstack.Core.Providers;
  10. using CancellationToken = System.Threading.CancellationToken;
  11. /// <summary>
  12. /// Represents a claim of messages in a queue.
  13. /// </summary>
  14. /// <remarks>
  15. /// The claim is released when <see cref="Dispose()"/> or <see cref="DisposeAsync"/>
  16. /// is called. At that time, any messages belonging to this claim which have not
  17. /// been deleted will be eligible for claiming by another node in the system.
  18. /// Messages belonging to this claim may be deleted by calling
  19. /// <see cref="IQueueingService.DeleteMessageAsync"/> or
  20. /// <see cref="IQueueingService.DeleteMessagesAsync"/>.
  21. /// </remarks>
  22. /// <seealso cref="IQueueingService"/>
  23. /// <threadsafety static="true" instance="false"/>
  24. /// <preliminary/>
  25. public class Claim : IDisposable
  26. {
  27. /// <summary>
  28. /// A private object used to ensure <see cref="_releaseTask"/> is only
  29. /// initialized once in <see cref="DisposeAsync"/>.
  30. /// </summary>
  31. private readonly object _lock = new object();
  32. /// <summary>
  33. /// The queueing service instance used for commands related to this claim.
  34. /// </summary>
  35. private readonly IQueueingService _service;
  36. /// <summary>
  37. /// The name of the queue this claim belongs to.
  38. /// </summary>
  39. private readonly QueueName _queueName;
  40. /// <summary>
  41. /// The backing field for the <see cref="Location"/> property.
  42. /// </summary>
  43. private readonly Uri _location;
  44. /// <summary>
  45. /// The backing field for the <see cref="Age"/> property.
  46. /// </summary>
  47. private TimeSpan _age;
  48. /// <summary>
  49. /// The backing field for the <see cref="TimeToLive"/> property.
  50. /// </summary>
  51. private TimeSpan _timeToLive;
  52. /// <summary>
  53. /// The backing field for the <see cref="Messages"/> property.
  54. /// </summary>
  55. private QueuedMessage[] _messages;
  56. /// <summary>
  57. /// The <see cref="Task"/> object representing the asynchronous release of this claim.
  58. /// Prior to calling <see cref="Dispose()"/> or <see cref="DisposeAsync"/>, the value of
  59. /// this field is <see langword="null"/>.
  60. /// </summary>
  61. private Task _releaseTask;
  62. /// <summary>
  63. /// Initializes a new instance of the <see cref="Claim"/> class using the provided values.
  64. /// </summary>
  65. /// <param name="service">The queueing service.</param>
  66. /// <param name="queueName">The name of the queue.</param>
  67. /// <param name="location">The absolute URI of the claim resource. If no claim was allocated by the server, this value is <see langword="null"/>.</param>
  68. /// <param name="timeToLive">The time to live of the claim.</param>
  69. /// <param name="age">The age of the claim.</param>
  70. /// <param name="owner"><see langword="true"/> if the current instance owns the claim (and is responsible for releasing it); otherwise, <see langword="false"/>.</param>
  71. /// <param name="messages">A collection of messages belonging to the claim.</param>
  72. /// <exception cref="ArgumentNullException">
  73. /// If <paramref name="service"/> is <see langword="null"/>.
  74. /// <para>-or-</para>
  75. /// <para>If <paramref name="queueName"/> is <see langword="null"/>.</para>
  76. /// <para>-or-</para>
  77. /// <para>If <paramref name="messages"/> is <see langword="null"/>.</para>
  78. /// </exception>
  79. public Claim(IQueueingService service, QueueName queueName, Uri location, TimeSpan timeToLive, TimeSpan age, bool owner, IEnumerable<QueuedMessage> messages)
  80. {
  81. if (service == null)
  82. throw new ArgumentNullException("service");
  83. if (queueName == null)
  84. throw new ArgumentNullException("queueName");
  85. if (messages == null)
  86. throw new ArgumentNullException("messages");
  87. _service = service;
  88. _queueName = queueName;
  89. _location = location;
  90. _timeToLive = timeToLive;
  91. _age = age;
  92. _messages = messages.ToArray();
  93. if (!owner)
  94. {
  95. // prevent this object from releasing the resource
  96. _releaseTask = InternalTaskExtensions.CompletedTask();
  97. }
  98. }
  99. /// <summary>
  100. /// Gets the claim ID.
  101. /// </summary>
  102. /// <remarks>
  103. /// The claim ID is derived from the <see cref="Location"/> property according to the
  104. /// URI template documented in the <see href="https://wiki.openstack.org/w/index.php?title=Marconi/specs/api/v1">OpenStack Marconi API v1 Blueprint</see>.
  105. /// </remarks>
  106. /// <value>
  107. /// The ID of this claim. If the claim is empty (i.e. the queue did not have any unclaimed messages), this value is <see langword="null"/>.
  108. /// </value>
  109. public ClaimId Id
  110. {
  111. get
  112. {
  113. if (_location == null)
  114. return null;
  115. string locationPath = _location.AbsolutePath;
  116. return new ClaimId(locationPath.Substring(locationPath.LastIndexOf('/') + 1));
  117. }
  118. }
  119. /// <summary>
  120. /// Gets the absolute URI for this claim.
  121. /// </summary>
  122. /// <value>
  123. /// The absolute URI of this claim. If the claim is empty (i.e. the queue did not have any unclaimed messages), this value is <see langword="null"/>.
  124. /// </value>
  125. public Uri Location
  126. {
  127. get
  128. {
  129. return _location;
  130. }
  131. }
  132. /// <summary>
  133. /// Gets the age of the claim as returned by the server.
  134. /// </summary>
  135. /// <remarks>
  136. /// This value does not automatically update. To obtain the age of a claim after a period of time elapses,
  137. /// use <see cref="IQueueingService.QueryClaimAsync"/>.
  138. /// </remarks>
  139. public TimeSpan Age
  140. {
  141. get
  142. {
  143. return _age;
  144. }
  145. }
  146. /// <summary>
  147. /// Gets the Time To Live (TTL) of the claim.
  148. /// </summary>
  149. public TimeSpan TimeToLive
  150. {
  151. get
  152. {
  153. return _timeToLive;
  154. }
  155. private set
  156. {
  157. _timeToLive = value;
  158. }
  159. }
  160. /// <summary>
  161. /// Gets the messages which are included in this claim.
  162. /// </summary>
  163. public ReadOnlyCollection<QueuedMessage> Messages
  164. {
  165. get
  166. {
  167. return new ReadOnlyCollection<QueuedMessage>(_messages);
  168. }
  169. }
  170. /// <summary>
  171. /// Refreshes the current claim.
  172. /// </summary>
  173. /// <remarks>
  174. /// This method calls <see cref="IQueueingService.QueryClaimAsync"/> to obtain updated
  175. /// information about the current claim, and then synchronously invokes <see cref="RefreshImpl"/>
  176. /// to update the current instance to match the results.
  177. /// </remarks>
  178. /// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
  179. /// <returns>A <see cref="Task"/> object representing the asynchronous operation.</returns>
  180. public Task RefreshAsync(CancellationToken cancellationToken)
  181. {
  182. Action<Task<Claim>> applyChanges = task => RefreshImpl(task.Result);
  183. return _service.QueryClaimAsync(_queueName, this, cancellationToken).Select(applyChanges);
  184. }
  185. /// <summary>
  186. /// Renews the claim by resetting the age and updating the TTL for the claim.
  187. /// </summary>
  188. /// <remarks>
  189. /// This method calls <see cref="IQueueingService.UpdateClaimAsync"/> to renew the
  190. /// current claim, and then synchronously updates the current instance to reflect
  191. /// the new age and time-to-live values.
  192. /// </remarks>
  193. /// <param name="timeToLive">
  194. /// The new Time-To-Live value for the claim. This value may differ from the original TTL of the claim.
  195. /// </param>
  196. /// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
  197. /// <returns>A <see cref="Task"/> object representing the asynchronous operation.</returns>
  198. /// <exception cref="ArgumentOutOfRangeException">If <paramref name="timeToLive"/> is negative or <see cref="TimeSpan.Zero"/>.</exception>
  199. /// <exception cref="InvalidOperationException">If the claim is empty (i.e. <see cref="Messages"/> is empty).</exception>
  200. public Task RenewAsync(TimeSpan timeToLive, CancellationToken cancellationToken)
  201. {
  202. if (timeToLive <= TimeSpan.Zero)
  203. throw new ArgumentOutOfRangeException("timeToLive");
  204. if (_location == null)
  205. throw new InvalidOperationException("Empty claims cannot be renewed.");
  206. Action<Task> applyChanges =
  207. task =>
  208. {
  209. _age = TimeSpan.Zero;
  210. TimeToLive = timeToLive;
  211. };
  212. return _service.UpdateClaimAsync(_queueName, this, timeToLive, cancellationToken).Select(applyChanges);
  213. }
  214. /// <inheritdoc/>
  215. /// <remarks>
  216. /// This method calls <see cref="IQueueingService.ReleaseClaimAsync"/> to release messages
  217. /// claimed by this claim. To prevent other subscribers from re-claiming the messages, make
  218. /// sure to delete the messages before calling <see cref="Dispose()"/>.
  219. /// </remarks>
  220. /// <seealso cref="IQueueingService.ReleaseClaimAsync"/>
  221. public void Dispose()
  222. {
  223. Dispose(true);
  224. GC.SuppressFinalize(this);
  225. }
  226. /// <summary>
  227. /// Asynchronously releases resources owned by this <see cref="Claim"/>.
  228. /// </summary>
  229. /// <remarks>
  230. /// This method calls <see cref="IQueueingService.ReleaseClaimAsync"/> to release messages
  231. /// claimed by this claim. To prevent other subscribers from re-claiming the messages, make
  232. /// sure to delete the messages before calling <see cref="DisposeAsync"/>.
  233. /// </remarks>
  234. /// <param name="cancellationToken">The <see cref="CancellationToken"/> that the task will observe.</param>
  235. /// <returns>A <see cref="Task"/> object representing the asynchronous operation.</returns>
  236. public Task DisposeAsync(CancellationToken cancellationToken)
  237. {
  238. lock (_lock)
  239. {
  240. if (_releaseTask == null)
  241. {
  242. if (_messages.Length == 0)
  243. _releaseTask = InternalTaskExtensions.CompletedTask();
  244. else
  245. _releaseTask = _service.ReleaseClaimAsync(_queueName, this, cancellationToken);
  246. }
  247. }
  248. return _releaseTask;
  249. }
  250. /// <summary>
  251. /// Releases resources owned by this <see cref="Claim"/>.
  252. /// </summary>
  253. /// <param name="disposing"><see langword="true"/> if this method was called from <see cref="Dispose()"/>; otherwise, <see langword="false"/> if this method was called from a finalizer.</param>
  254. protected virtual void Dispose(bool disposing)
  255. {
  256. if (disposing)
  257. {
  258. DisposeAsync(CancellationToken.None).Wait();
  259. }
  260. }
  261. /// <summary>
  262. /// Refresh the current claim to match the updated information in <paramref name="claim"/>.
  263. /// </summary>
  264. /// <param name="claim">A <see cref="Claim"/> object containing updated claim information.</param>
  265. /// <exception cref="ArgumentNullException">If <paramref name="claim"/> is <see langword="null"/>.</exception>
  266. /// <exception cref="ArgumentException">If the specified <paramref name="claim"/> does not represent the same claim as the current instance.</exception>
  267. protected virtual void RefreshImpl(Claim claim)
  268. {
  269. if (claim == null)
  270. throw new ArgumentNullException("claim");
  271. if (Location != claim.Location)
  272. throw new ArgumentException("The specified claim does not represent the same claim as the current instance.", "claim");
  273. this._age = claim._age;
  274. this._messages = claim._messages;
  275. this._timeToLive = claim._timeToLive;
  276. }
  277. }
  278. }