Skip to content

Commit b5b2a36

Browse files
TCBroadbarclayadam
authored andcommitted
Added retry policy interface and default linear policy
1 parent 64e5365 commit b5b2a36

File tree

5 files changed

+63
-3
lines changed

5 files changed

+63
-3
lines changed

src/HangFire.Azure.ServiceBusQueue/HangFire.Azure.ServiceBusQueue.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@
6969
<Reference Include="System.Xml" />
7070
</ItemGroup>
7171
<ItemGroup>
72+
<Compile Include="IRetryPolicy.cs" />
73+
<Compile Include="LinearRetryPolicy.cs" />
7274
<Compile Include="ServiceBusQueueFetchedJob.cs" />
7375
<Compile Include="Properties\AssemblyInfo.cs" />
7476
<Compile Include="ServiceBusQueueJobQueue.cs" />
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using System;
2+
3+
namespace Hangfire.Azure.ServiceBusQueue
4+
{
5+
public interface IRetryPolicy
6+
{
7+
void Execute(Action action);
8+
}
9+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using System;
2+
using System.Threading;
3+
4+
namespace Hangfire.Azure.ServiceBusQueue
5+
{
6+
public class LinearRetryPolicy : IRetryPolicy
7+
{
8+
public LinearRetryPolicy(int retryCount, TimeSpan retryDelay)
9+
{
10+
this.RetryDelay = retryDelay;
11+
this.RetryCount = retryCount;
12+
}
13+
14+
public TimeSpan RetryDelay { get; private set; }
15+
16+
public int RetryCount { get; private set; }
17+
18+
public void Execute(Action action)
19+
{
20+
for (var i = 0; i < this.RetryCount; i++)
21+
{
22+
try
23+
{
24+
action();
25+
26+
return;
27+
}
28+
catch (TimeoutException)
29+
{
30+
if (i == this.RetryCount - 1)
31+
{
32+
throw;
33+
}
34+
35+
Thread.Sleep(this.RetryDelay);
36+
}
37+
}
38+
}
39+
}
40+
}

src/HangFire.Azure.ServiceBusQueue/ServiceBusQueueJobQueue.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ public IFetchedJob Dequeue(string[] queues, CancellationToken cancellationToken)
5252
var errorMessage = string.Format(
5353
"Queue {0} could not be found. Either create the queue manually, " +
5454
"or grant the Manage permission and set ServiceBusQueueOptions.CheckAndCreateQueues to true",
55-
5655
clients[queueIndex].Path);
5756

5857
throw new UnauthorizedAccessException(errorMessage, ex);
@@ -75,9 +74,9 @@ public void Enqueue(IDbConnection connection, string queue, string jobId)
7574

7675
using (var message = new BrokeredMessage(jobId))
7776
{
78-
client.Send(message);
77+
_manager.Options.RetryPolicy.Execute(() => client.Send(message));
7978
}
8079
}
8180
}
8281
}
83-
}
82+
}

src/HangFire.Azure.ServiceBusQueue/ServiceBusQueueOptions.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ public ServiceBusQueueOptions()
99
{
1010
this.CheckAndCreateQueues = true;
1111
this.LoopReceiveTimeout = TimeSpan.FromMilliseconds(500);
12+
this.RetryPolicy = new LinearRetryPolicy(3, TimeSpan.FromSeconds(1));
1213
}
1314

1415
/// <summary>
@@ -50,6 +51,15 @@ public ServiceBusQueueOptions()
5051
/// <value>Defaults to <c>TimeSpan.FromMilliseconds(500)</c></value>
5152
public TimeSpan LoopReceiveTimeout { get; set; }
5253

54+
/// <summary>
55+
/// Gets or sets the retry policy for enqueueing messages
56+
/// </summary>
57+
/// <remarks>
58+
/// The default policy is a <see cref="LinearRetryPolicy" /> with <see cref="LinearRetryPolicy.RetryCount"/> of 3
59+
/// and a <see cref="LinearRetryPolicy.RetryDelay"/> of 1 second.
60+
/// </remarks>
61+
public IRetryPolicy RetryPolicy { get; set; }
62+
5363
internal string GetQueueName(string name)
5464
{
5565
if (QueuePrefix != null)

0 commit comments

Comments
 (0)