public void ExecuteAction_Retry_And_Success()
        {
            var failUntilCount = 3;
            var mgr            = new RetryAction <string>(() => "foo", UpdateWeight);

            var result = mgr.ExecuteAction((str, attemptCount) =>
            {
                if (attemptCount < failUntilCount)
                {
                    throw new InvalidOperationException();
                }
                return(attemptCount);
            }, (attempt, exception) =>
            {
                Assert.LessOrEqual(attempt, failUntilCount);
                Assert.IsInstanceOf <InvalidOperationException>(exception);

                return(true);
            });

            Assert.AreEqual(failUntilCount, result);

            Assert.AreEqual(failUntilCount, _updateWeightSequence.Count);
            Assert.IsFalse(_updateWeightSequence[0]);
            Assert.IsFalse(_updateWeightSequence[1]);
            Assert.IsTrue(_updateWeightSequence[2]);
        }
Exemplo n.º 2
0
        public void RetryAction_keeps_trying_to_run_the_action_until_an_attempt_succeeds()
        {
            var count       = 0;
            var lockObject  = new object();
            var initializer = new RetryAction <string>(
                i =>
            {
                // Locking here to ensure that count is incremented correctly even if RetryAction isn't working correctly.
                lock (lockObject)
                {
                    count++;
                    if (count <= 5)
                    {
                        throw new Exception("Fail!");
                    }
                }
            });

            try
            {
                ExecuteInParallel(() => initializer.PerformAction(""));
            }
            catch (AggregateException ex)
            {
                Assert.Equal(5, ex.InnerExceptions.Count);
                foreach (var innerException in ex.InnerExceptions)
                {
                    Assert.Equal("Fail!", innerException.Message);
                }
            }
            Assert.Equal(6, count);
        }
Exemplo n.º 3
0
        public void WriteVariablesToDiskCache(GitVersionCacheKey cacheKey, VersionVariables variablesFromCache)
        {
            var cacheDir      = PrepareCacheDirectory();
            var cacheFileName = GetCacheFileName(cacheKey, cacheDir);

            variablesFromCache.FileName = cacheFileName;

            Dictionary <string, string> dictionary;

            using (log.IndentLog("Creating dictionary"))
            {
                dictionary = variablesFromCache.ToDictionary(x => x.Key, x => x.Value);
            }

            void WriteCacheOperation()
            {
                using var stream = fileSystem.OpenWrite(cacheFileName);
                using var sw     = new StreamWriter(stream);
                using (log.IndentLog("Storing version variables to cache file " + cacheFileName))
                {
                    var serializer = new Serializer();
                    serializer.Serialize(sw, dictionary);
                }
            }

            var retryOperation = new RetryAction <IOException>(maxRetries: 6);

            retryOperation.Execute(WriteCacheOperation);
        }
        public async Task ExecuteAsync_Retry_And_Success()
        {
            var failUntilCount = 3;
            var mgr            = new RetryAction <string>(() => "foo", UpdateWeight);

            var result = await mgr.ExecuteAsync(
                (str, attemptCount) =>
            {
                if (attemptCount < failUntilCount)
                {
                    return(Task.FromException <int>(new InvalidOperationException()));
                }

                return(Task.FromResult(attemptCount));
            }, (attempt, exception) =>
            {
                Assert.LessOrEqual(attempt, failUntilCount);
                Assert.IsInstanceOf <InvalidOperationException>(exception);

                return(true);
            });

            Assert.AreEqual(failUntilCount, result);

            Assert.AreEqual(failUntilCount, _updateWeightSequence.Count);
            Assert.IsFalse(_updateWeightSequence[0]);
            Assert.IsFalse(_updateWeightSequence[1]);
            Assert.IsTrue(_updateWeightSequence[2]);
        }
Exemplo n.º 5
0
    public void Execute(VersionVariables variables, OutputContext context)
    {
        var gitVersionOptions = this.options.Value;

        if (gitVersionOptions.Output.Contains(OutputType.BuildServer))
        {
            this.buildAgent.WriteIntegration(this.console.WriteLine, variables, context.UpdateBuildNumber ?? true);
        }
        if (gitVersionOptions.Output.Contains(OutputType.File))
        {
            var retryOperation = new RetryAction <IOException>();
            retryOperation.Execute(() => this.fileSystem.WriteAllText(context.OutputFile, variables.ToString()));
        }

        if (!gitVersionOptions.Output.Contains(OutputType.Json))
        {
            return;
        }

        switch (gitVersionOptions.ShowVariable)
        {
        case null:
            this.console.WriteLine(variables.ToString());
            break;

        default:
            if (!variables.TryGetValue(gitVersionOptions.ShowVariable, out var part))
            {
                throw new WarningException($"'{gitVersionOptions.ShowVariable}' variable does not exist");
            }

            this.console.WriteLine(part);
            break;
        }
    }
Exemplo n.º 6
0
 public RetryTask()
 {
     CalendarItem = null;
     Calendar     = "";
     Action       = 0;
     Delay        = 0;
     LastRun      = DateTime.MinValue;
     m_retryCount = 0;
     m_retryCount = 0;
 }
        public RetryAction RetryAfter(IRequest request, RetryReason reason)
        {
            if (request.Idempotent || reason.AllowsNonIdempotentRetries())
            {
                var backoffDuration = _backoffCalculator.CalculateBackoff(request);
                return(RetryAction.Duration(backoffDuration));
            }

            return(RetryAction.Duration(null));
        }
Exemplo n.º 8
0
        public ICommit FindMergeBase(ICommit commit, ICommit otherCommit)
        {
            var retryAction = new RetryAction <LockedFileException, ICommit>();

            return(retryAction.Execute(() =>
            {
                var mergeBase = repositoryInstance.ObjectDatabase.FindMergeBase((Commit)commit, (Commit)otherCommit);
                return new Commit(mergeBase);
            }));
        }
Exemplo n.º 9
0
 public RetryTask(CalendarItem item, string calendar, RetryAction action, int delay = 1, int retryCount = 8)
 {
     CalendarItem   = item;
     Calendar       = calendar;
     Action         = action;
     Delay          = delay;
     LastRun        = DateTime.Now;
     m_retryCount   = retryCount;
     m_currentRetry = 0;
 }
Exemplo n.º 10
0
 public GitPreparer(ILog log, IEnvironment environment, ICurrentBuildAgent buildAgent, IOptions <GitVersionOptions> options,
                    IMutatingGitRepository repository, IGitRepositoryInfo repositoryInfo, IRepositoryStore repositoryStore)
 {
     this.log             = log ?? throw new ArgumentNullException(nameof(log));
     this.environment     = environment ?? throw new ArgumentNullException(nameof(environment));
     this.repository      = repository ?? throw new ArgumentNullException(nameof(repository));
     this.options         = options ?? throw new ArgumentNullException(nameof(options));
     this.repositoryInfo  = repositoryInfo ?? throw new ArgumentNullException(nameof(repositoryInfo));
     this.repositoryStore = repositoryStore ?? throw new ArgumentNullException(nameof(repositoryStore));
     this.buildAgent      = buildAgent;
     this.retryAction     = new RetryAction <LockedFileException>();
 }
Exemplo n.º 11
0
 public GitPreparer(ILog log, IEnvironment environment, ICurrentBuildAgent?buildAgent, IOptions <GitVersionOptions> options,
                    IMutatingGitRepository repository, IGitRepositoryInfo repositoryInfo, IRepositoryStore repositoryStore)
 {
     this.log             = log.NotNull();
     this.environment     = environment.NotNull();
     this.repository      = repository.NotNull();
     this.options         = options.NotNull();
     this.repositoryInfo  = repositoryInfo.NotNull();
     this.repositoryStore = repositoryStore.NotNull();
     this.buildAgent      = buildAgent;
     this.retryAction     = new RetryAction <LockedFileException>();
 }
        public void ExecuteAction_Deny_Async_Result()
        {
            var errMsg = "Async action should be executed with ExecuteAsync.";
            var mgr    = new RetryAction <string>(() => "foo", UpdateWeight);

            Assert.Throws <ArgumentException>(
                () => mgr.ExecuteAction <Task>((_1, _2) => Task.FromResult("Task"), (_1, _2) => true),
                errMsg);
            Assert.Throws <ArgumentException>(
                () => mgr.ExecuteAction <Task <string> >((_1, _2) => Task.FromResult("Task<T>"), (_1, _2) => true),
                errMsg);
        }
Exemplo n.º 13
0
        public void RetryAction_uses_the_given_input_for_each_attempt_until_an_attempt_succeeds()
        {
            var count      = 0;
            var lockObject = new object();
            var inputs     = new List <int>();

            var initializer = new RetryAction <int>(
                i =>
            {
                // Locking here to ensure that count is incremented correctly even if RetryAction isn't working correctly.
                lock (lockObject)
                {
                    inputs.Add(i);
                    count++;
                    if (count <= 5)
                    {
                        throw new Exception("Fail!");
                    }
                }
            });

            var tests = new Action[20];

            for (var i = 0; i < 20; i++)
            {
                var outside = i; // Make sure i is used from outside the closure
                tests[i] = () => initializer.PerformAction(outside);
            }

            try
            {
                Parallel.Invoke(tests);
            }
            catch (AggregateException ex)
            {
                Assert.Equal(5, ex.InnerExceptions.Count);
            }

            Assert.Equal(6, count);
            Assert.Equal(6, inputs.Count);

            for (var i = 0; i < inputs.Count; i++)
            {
                for (var j = 0; j < inputs.Count; j++)
                {
                    if (i != j)
                    {
                        Assert.NotEqual(inputs[i], inputs[j]);
                    }
                }
            }
        }
Exemplo n.º 14
0
 private void CreateRetry(CalendarItem item, RetryAction action)
 {
     if (Retry == null)
     {
         var retry = new RetryTask(item, m_currentCalendar, action);
         Scheduler.Scheduler.Instance.AddRetry(retry);
     }
     else
     {
         Retry.RetryFailed();
         Retry = null;
     }
 }
Exemplo n.º 15
0
        private RetryAction OnContextLost(Exception dbException)
        {
            RetryAction retryAction      = RetryAction.None;
            bool        hasBeenProcessed = false;

            if (_TransactionManager.Transaction == null)
            {
                OnOracleContextLost(dbException, ref retryAction, ref hasBeenProcessed);
                OnSqlContextLost(dbException, ref retryAction, ref hasBeenProcessed);
            }

            return(retryAction);
        }
Exemplo n.º 16
0
    public ICommit FindMergeBase(ICommit commit, ICommit otherCommit)
    {
        _ = commit ?? throw new ArgumentNullException(nameof(commit));
        _ = otherCommit ?? throw new ArgumentNullException(nameof(otherCommit));

        var retryAction = new RetryAction <LockedFileException, ICommit>();

        return(retryAction.Execute(() =>
        {
            var mergeBase = repositoryInstance.ObjectDatabase.FindMergeBase((Commit)commit, (Commit)otherCommit);
            return new Commit(mergeBase);
        }));
    }
Exemplo n.º 17
0
    public ICommit?FindMergeBase(ICommit commit, ICommit otherCommit)
    {
        _ = commit.NotNull();
        _ = otherCommit.NotNull();

        var retryAction = new RetryAction <LockedFileException, ICommit?>();

        return(retryAction.Execute(() =>
        {
            var mergeBase = repositoryInstance.ObjectDatabase.FindMergeBase((Commit)commit, (Commit)otherCommit);
            return mergeBase == null ? null : new Commit(mergeBase);
        }));
    }
Exemplo n.º 18
0
    public ICommit?FindMergeBase(ICommit commit, ICommit otherCommit)
    {
        commit      = commit.NotNull();
        otherCommit = otherCommit.NotNull();

        var retryAction = new RetryAction <LockedFileException, ICommit?>();

        return(retryAction.Execute(() =>
        {
            var first = (Commit)commit;
            var second = (Commit)otherCommit;
            var mergeBase = RepositoryInstance.ObjectDatabase.FindMergeBase(first, second);
            return mergeBase == null ? null : new Commit(mergeBase);
        }));
    }
Exemplo n.º 19
0
 private static bool Retry(RetryAction action)
 {
     for (int i = 0; ; i++)
     {
         bool lastRetry = i == MaxRetries;
         if (action(lastRetry))
         {
             return(true);
         }
         if (lastRetry)
         {
             return(false);
         }
         Thread.Sleep(RetryDelayMillis);
     }
 }
Exemplo n.º 20
0
        public void RetryAction_only_runs_the_action_once_even_when_called_from_multiple_threads()
        {
            var count       = 0;
            var lockObject  = new object();
            var initializer = new RetryAction <string>(
                i =>
            {
                // Locking here to ensure that count is incremented correctly even if RetryAction isn't working correctly.
                lock (lockObject)
                {
                    count++;
                }
            });

            ExecuteInParallel(() => initializer.PerformAction(""));
            Assert.Equal(1, count);
        }
Exemplo n.º 21
0
        public static void Retry <TException>([NotNull, InstantHandle] RetryAction action, int maxRetries = 2)
            where TException : Exception
        {
            #region Sanity checks
            if (action == null)
            {
                throw new ArgumentNullException(nameof(action));
            }
            #endregion

            int retryCounter = 0;
Retry:
            if (retryCounter >= maxRetries)
            {
                action(lastAttempt: true);
            }
            else
            {
                try
                {
                    action(lastAttempt: false);
                }
                catch (TException ex)
                {
                    Log.Info(ex);

                    Random random;
                    try
                    {
                        // Use process ID as a seed to ensure we get different values than other competing processes on the same machine
                        random = new Random(Process.GetCurrentProcess().Id);
                    }
                    catch (Exception)
                    {
                        random = new Random();
                    }

                    int delay = random.Next(50, 1000 * (1 << retryCounter));
                    Log.Info("Retrying in " + delay + " milliseconds");
                    Thread.Sleep(delay);

                    retryCounter++;
                    goto Retry;
                }
            }
        }
Exemplo n.º 22
0
 public static VersionVariables FromFile(string filePath, IFileSystem fileSystem)
 {
     try
     {
         var retryAction = new RetryAction <IOException, VersionVariables>();
         return(retryAction.Execute(() => FromFileInternal(filePath, fileSystem)));
     }
     catch (AggregateException ex)
     {
         var lastException = ex.InnerExceptions?.LastOrDefault() ?? ex.InnerException;
         if (lastException != null)
         {
             throw lastException;
         }
         throw;
     }
 }
Exemplo n.º 23
0
        partial void OnSqlContextLost(Exception dbException, ref RetryAction retryAction, ref bool processed)
        {
            if (processed)
            {
                return;
            }

            if (_Connection is SqlConnection)
            {
                SqlException e = dbException as SqlException;

                if (e == null)
                {
                    retryAction = RetryAction.None;
                }
                else
                {
                    switch (e.Number)                           // sys.messages
                    {
                    case 233:
                    case -2:
                    case 10054:
                        retryAction = RetryAction.Reconnect;
                        break;

                    case 201:                                           // Procedure or function '%.*ls' expects parameter '%.*ls', which was not supplied.
                    case 206:                                           // Operand type clash: %ls is incompatible with %ls.
                    case 257:                                           // Implicit conversion from data type %ls to %ls is not allowed. Use the CONVERT function to run this query.
                    case 8144:                                          // Procedure or function %.*ls has too many arguments specified.
                        retryAction = RetryAction.RefreshParameters;
                        break;

                    // To add other cases
                    default:
                        retryAction = RetryAction.None;
                        break;
                    }
                }

                processed = true;
            }
        }
        public async Task ExecuteAsync_Return()
        {
            var mgr = new RetryAction <string>(() => "foo", UpdateWeight);

            var retryCount = 0;
            var result     = await mgr.ExecuteAsync(
                (str, _) => Task.FromResult(str),
                (_1, _2) =>
            {
                retryCount++;
                return(true);
            });

            Assert.AreEqual("foo", result);

            Assert.AreEqual(1, _updateWeightSequence.Count);
            Assert.IsTrue(_updateWeightSequence[0]);

            Assert.AreEqual(0, retryCount);
        }
        public void ExecuteAsync_Retry_And_Fail_First_Attempt()
        {
            var outerRetryCount = -1;
            var mgr             = new RetryAction <string>(() => "foo", UpdateWeight);

            Assert.ThrowsAsync <InvalidOperationException>(() =>
            {
                return(mgr.ExecuteAsync <int>((str, attemptCount) =>
                {
                    throw new InvalidOperationException();
                }, (attempt, exception) =>
                {
                    outerRetryCount = attempt;
                    return false;
                }));
            });

            Assert.AreEqual(1, outerRetryCount);
            Assert.AreEqual(1, _updateWeightSequence.Count);
            Assert.IsFalse(_updateWeightSequence[0]);
        }
Exemplo n.º 26
0
        partial void OnOracleContextLost(Exception dbException, ref RetryAction retryAction, ref bool processed)
        {
            if (processed)
            {
                return;
            }

            if (_Connection is OracleConnection)
            {
                OracleException e = dbException as OracleException;

                if (e == null)
                {
                    retryAction = RetryAction.None;
                }
                else
                {
                    switch (e.Number)
                    {
                    case 2396:                                  // ORA-02396: exceeded maximum idle time, please connect again
                    case 3113:                                  // ORA-03113: end-of-file on communication channel
                    case 3135:                                  // ORA-03135: connection lost contact
                    case 4068:                                  // ORA-04068: existing state of packagesstringstringstring has been discarded
                        retryAction = RetryAction.Reconnect;
                        break;

                    case 6550:
                        retryAction = (e.Errors.Count == 1 && e.Message.Contains("PLS-00306: ")) ? RetryAction.RefreshParameters : RetryAction.None;
                        break;

                    // To add other cases
                    default:
                        retryAction = RetryAction.None;
                        break;
                    }
                }

                processed = true;
            }
        }
Exemplo n.º 27
0
 /// <summary>
 /// Retry a method that takes 4 parameters and does not return a value.
 /// </summary>
 /// <typeparam name="T1">The type of the first parameter of the action.</typeparam>
 /// <typeparam name="T2">The type of the secord parameter of the action.</typeparam>
 /// <typeparam name="T3">The type of the third parameter of the action.</typeparam>
 /// <typeparam name="T4">The type of the fourth parameter of the action.</typeparam>
 /// <param name="action">A method to retry.</param>
 /// <param name="t1">The first parameter of the action.</param>
 /// <param name="t2">The second parameter of the action.</param>
 /// <param name="t3">The third parameter of the action.</param>
 /// <param name="t4">The fourth parameter of the action.</param>
 public void Retry <T1, T2, T3, T4>(RetryAction <T1, T2, T3, T4> action, T1 t1, T2 t2, T3 t3, T4 t4)
 {
     for (int i = 0; i < RetryCount; i++)
     {
         try
         {
             action(t1, t2, t3, t4);
             break;
         }
         catch
         {
             if (i == RetryCount - 1)
             {
                 throw;
             }
             else
             {
                 Thread.Sleep(TimeSpan.FromSeconds(IntervalSeconds));
             }
         }
     }
 }
Exemplo n.º 28
0
 /// <summary>
 /// Retry a method that takes a single parameter and does not return a value.
 /// </summary>
 /// <typeparam name="T">The type of the parameter of the action.</typeparam>
 /// <param name="action">An action to retry.</param>
 /// <param name="t">The parameter of the action.</param>
 public void Retry <T>(RetryAction <T> action, T t)
 {
     for (int i = 0; i < RetryCount; i++)
     {
         try
         {
             action(t);
             break;
         }
         catch
         {
             if (i == RetryCount - 1)
             {
                 throw;
             }
             else
             {
                 Thread.Sleep(TimeSpan.FromSeconds(IntervalSeconds));
             }
         }
     }
 }
Exemplo n.º 29
0
        partial void OnOracleContextLost(Exception dbException, ref RetryAction retryAction, ref bool processed)
        {
            if (processed)
            {
                return;
            }

            if (_Connection is OracleConnection)
            {
                OracleException e = dbException as OracleException;

                if (e == null)
                {
                    retryAction = RetryAction.None;
                }
                else
                {
                    switch (e.Number)
                    {
                    case 3113:
                    case 4068:
                        retryAction = RetryAction.Reconnect;
                        break;

                    case 6550:
                        retryAction = (e.Errors.Count == 1 && e.Message.Contains("PLS-00306: ")) ? RetryAction.RefreshParameters : RetryAction.None;
                        break;

                    // To add other cases
                    default:
                        retryAction = RetryAction.None;
                        break;
                    }
                }

                processed = true;
            }
        }
        public void ExecuteAction_Retry_And_Fail()
        {
            var outerRetryCount = -1;
            var maxRetryCount   = 3;
            var mgr             = new RetryAction <string>(() => "foo", UpdateWeight);

            Assert.Throws <InvalidOperationException>(() =>
            {
                mgr.ExecuteAction <int>((str, attemptCount) =>
                {
                    throw new InvalidOperationException();
                }, (attempt, exception) =>
                {
                    outerRetryCount = attempt;
                    return(attempt < maxRetryCount);
                });
            });

            Assert.AreEqual(maxRetryCount, outerRetryCount);
            Assert.AreEqual(maxRetryCount, _updateWeightSequence.Count);
            Assert.IsFalse(_updateWeightSequence[0]);
            Assert.IsFalse(_updateWeightSequence[1]);
            Assert.IsFalse(_updateWeightSequence[2]);
        }
Exemplo n.º 31
0
 private static bool Retry(RetryAction action)
 {
     for (int i = 0; ; i++)
     {
         bool lastRetry = i == MaxRetries;
         if (action(lastRetry))
             return true;
         if (lastRetry)
             return false;
         Thread.Sleep(RetryDelayMillis);
     }
 }