Skip to content

Commit

Permalink
Added iterative backoff in retry timers
Browse files Browse the repository at this point in the history
  • Loading branch information
tznind committed Jan 26, 2022
1 parent 73e61d4 commit 889473b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
16 changes: 16 additions & 0 deletions Config.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using YamlDotNet.Serialization;

Expand All @@ -17,6 +18,8 @@ internal class Config

public string RdmpCli { get; private set; }

public int[] RetryBackOffsInMinutes { get; set; } = new int[] { 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144 };

internal static Config MakeUserType()
{
Config instance = new Config();
Expand All @@ -41,5 +44,18 @@ internal static Config MakeUserType()

return instance;
}

internal bool ShouldTry(int retryIdx)
{
return retryIdx >=0 && retryIdx < RetryBackOffsInMinutes.Length;
}

internal void RetrySleep(int retryIdx)
{
var sleepFor = RetryBackOffsInMinutes[retryIdx];

Console.WriteLine($"Sleeping for {sleepFor} minutes");
Thread.Sleep(TimeSpan.FromMinutes(sleepFor));
}
}
}
15 changes: 9 additions & 6 deletions Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
const string ProgressFilename = "LastMonthDone.txt";
const string ConfigFile = "Config.yaml";

Config config = new Config();
Config config;


if(!File.Exists(ConfigFile))
{
Expand Down Expand Up @@ -81,7 +82,7 @@
File.WriteAllText(Path.Combine(config.ForLoading, "LoadMe.txt"), sb.ToString());


int retryCount = 10;
int retryIdx = 0;
Retry:

var pCheck = Process.Start(config.RdmpCli, $"dle -l {config.LoadMetadataID} --command check");
Expand All @@ -90,9 +91,10 @@
if(pCheck.ExitCode != 0)
{
Console.WriteLine("Checking failed");
if(retryCount >0)
if(config.ShouldTry(retryIdx))
{
retryCount--;
config.RetrySleep(retryIdx);
retryIdx++;
Console.WriteLine("Retrying checks");
goto Retry;
}
Expand All @@ -109,9 +111,10 @@
if (pRun.ExitCode != 0)
{
Console.WriteLine("Running failed");
if (retryCount > 0)
if (config.ShouldTry(retryIdx))
{
retryCount--;
config.RetrySleep(retryIdx);
retryIdx++;
Console.WriteLine("Retrying (starting with rechecking)");
goto Retry;
}
Expand Down

0 comments on commit 889473b

Please sign in to comment.