Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Add automatic retry options for certain functions
#1
Recently, some of the issues I encountered seem to be related to execution speed. When errors occur, simply retrying once can resolve them.

Could we add a retry count attribute for certain functions as shown in the diagram below?
   

Although achievable with try...catch, it's a bit cumbersome.
#2
Try Polly.
Get: https://www.nuget.org/packages/Polly.Core/
Learn: https://www.pollydocs.org/

Example:
Code:
Copy      Help
// script "nuget Polly.cs"
/*/ nuget -\Polly.Core; /*/
using Polly;
using Polly.Retry;

//Create Polly object that executes "retry".
var ro = new RetryStrategyOptions(); //default options: max 3 retries, every 2 s
//var ro = new RetryStrategyOptions { MaxRetryAttempts = 10, Delay = TimeSpan.FromMilliseconds(100) }; //custom options

var retry = new ResiliencePipelineBuilder().AddRetry(ro).Build();

//example. Find window. On exception wait/retry several times.
var w = retry.Execute(() => wnd.find(0, "*Notepad"));
print.it(w);

//example. Activate window. On exception wait/retry several times.
retry.Execute(() => { w.Activate(); });
#3
Thank you for sharing. It's very useful. 

It would be very convenient if the details were hidden, for example, by directly using the following code,

wnd.find(0, "*Notepad").retry(2, 300); //retry 2 times. every 300ms
#4
Many LA functions already have "retry" parameters. It's the Seconds wait parameter, eg in wnd.find. You can post a list of other functions that should have such parameter.

One alternative - use universal "retry" functions, for example Polly.

Other alternative - LA could have a command "Generate retry code with try/catch" in menu Edit > Surround.
#5
In next LA added wait.retry. It executes your code and waits/retries while it throws an exception.
 
Code:
Copy      Help
string file = @"C:\Test\test.txt";
string s = wait.retry(5, () => File.ReadAllText(file));
s = s.Upper();
wait.retry(5, () => { File.WriteAllText(file, s); });
print.it("ok");

Code:
Copy      Help
wait.retry(new(5) { Period = 100 }, () => { File.WriteAllText(file, s); });

wait.retry(5, () => { File.WriteAllText(file, s); }, e => e is IOException);


Forum Jump:


Users browsing this thread: 2 Guest(s)