|
| 1 | +namespace net.openstack.Core.Synchronous |
| 2 | +{ |
| 3 | + using System; |
| 4 | + using System.Collections.ObjectModel; |
| 5 | + using net.openstack.Core.Domain; |
| 6 | + using net.openstack.Core.Domain.Queues; |
| 7 | + using CancellationToken = System.Threading.CancellationToken; |
| 8 | + using IQueueingService = net.openstack.Core.Providers.IQueueingService; |
| 9 | + using WebException = System.Net.WebException; |
| 10 | + |
| 11 | + /// <summary> |
| 12 | + /// Provides extension methods to allow synchronous calls to the methods in <see cref="Claim"/>. |
| 13 | + /// </summary> |
| 14 | + /// <threadsafety static="true" instance="false"/> |
| 15 | + /// <preliminary/> |
| 16 | + public static class ClaimExtensions |
| 17 | + { |
| 18 | + /// <summary> |
| 19 | + /// Refreshes the current claim. |
| 20 | + /// </summary> |
| 21 | + /// <remarks> |
| 22 | + /// This method calls <see cref="IQueueingService.QueryClaimAsync"/> to obtain updated |
| 23 | + /// information about the current claim, and then synchronously invokes <see cref="Claim.RefreshImpl"/> |
| 24 | + /// to update the current instance to match the results. |
| 25 | + /// </remarks> |
| 26 | + /// <param name="claim">The claim.</param> |
| 27 | + /// <exception cref="ArgumentNullException">If <paramref name="claim"/> is <c>null</c>.</exception> |
| 28 | + /// <exception cref="WebException">If the REST request does not return successfully.</exception> |
| 29 | + public static void Refresh(this Claim claim) |
| 30 | + { |
| 31 | + if (claim == null) |
| 32 | + throw new ArgumentNullException("claim"); |
| 33 | + |
| 34 | + try |
| 35 | + { |
| 36 | + claim.RefreshAsync(CancellationToken.None).Wait(); |
| 37 | + } |
| 38 | + catch (AggregateException ex) |
| 39 | + { |
| 40 | + ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions; |
| 41 | + if (innerExceptions.Count == 1) |
| 42 | + throw innerExceptions[0]; |
| 43 | + |
| 44 | + throw; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + /// <summary> |
| 49 | + /// Renews the claim by resetting the age and updating the TTL for the claim. |
| 50 | + /// </summary> |
| 51 | + /// <remarks> |
| 52 | + /// This method calls <see cref="IQueueingService.UpdateClaimAsync"/> to renew the |
| 53 | + /// current claim, and then synchronously updates the current instance to reflect |
| 54 | + /// the new age and time-to-live values. |
| 55 | + /// </remarks> |
| 56 | + /// <param name="claim">The claim.</param> |
| 57 | + /// <param name="timeToLive"> |
| 58 | + /// The new Time-To-Live value for the claim. This value may differ from the original TTL of the claim. |
| 59 | + /// </param> |
| 60 | + /// <exception cref="ArgumentNullException">If <paramref name="claim"/> is <c>null</c>.</exception> |
| 61 | + /// <exception cref="ArgumentOutOfRangeException">If <paramref name="timeToLive"/> is negative or <see cref="TimeSpan.Zero"/>.</exception> |
| 62 | + /// <exception cref="InvalidOperationException">If the claim is empty (i.e. <see cref="Claim.Messages"/> is empty).</exception> |
| 63 | + /// <exception cref="WebException">If the REST request does not return successfully.</exception> |
| 64 | + public static void Renew(this Claim claim, TimeSpan timeToLive) |
| 65 | + { |
| 66 | + if (claim == null) |
| 67 | + throw new ArgumentNullException("claim"); |
| 68 | + |
| 69 | + try |
| 70 | + { |
| 71 | + claim.RenewAsync(timeToLive, CancellationToken.None).Wait(); |
| 72 | + } |
| 73 | + catch (AggregateException ex) |
| 74 | + { |
| 75 | + ReadOnlyCollection<Exception> innerExceptions = ex.Flatten().InnerExceptions; |
| 76 | + if (innerExceptions.Count == 1) |
| 77 | + throw innerExceptions[0]; |
| 78 | + |
| 79 | + throw; |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments