示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="PluginRetrySetting" /> class.
        /// </summary>
        /// <param name="state">The <see cref="PluginResult" /> for which this behavior should be applied.</param>
        /// <param name="retryAction">The <see cref="PluginRetryAction" /> to take when the specified state occurs.</param>
        /// <param name="retryLimit">The maximum number of times the plugin should retry.</param>
        /// <param name="delayBeforeRetry">The amount of time the plugin should wait between retries.</param>
        /// <param name="limitExceededAction">The <see cref="PluginRetryAction" /> to take when the retry limit is reached. (<see cref="PluginRetryAction.Retry" /> is not a valid action.)</param>
        public PluginRetrySetting(PluginResult state, PluginRetryAction retryAction, int retryLimit, TimeSpan delayBeforeRetry, PluginRetryAction limitExceededAction)
        {
            if (limitExceededAction == PluginRetryAction.Retry)
            {
                throw new ArgumentException("Retry limit exceeded action cannot be retry.", nameof(limitExceededAction));
            }

            _state                    = state;
            _retryAction              = retryAction;
            _delayBeforeRetry         = delayBeforeRetry;
            _retryLimit               = retryLimit;
            _retryLimitExceededAction = limitExceededAction;
        }
示例#2
0
        /// <summary>
        /// Executes the specified plugin execution method, handling retries as configured in the <see cref="PluginExecutionData" />.
        /// </summary>
        /// <param name="action">The plugin method to execute.</param>
        /// <returns>A <see cref="PluginExecutionResult" /> indicating the outcome of the execution.</returns>
        public PluginExecutionResult Run(Func <PluginExecutionResult> action)
        {
            PluginExecutionResult executionResult = InvokeAction(action);
            PluginRetryAction     retryAction     = GetRetryAction(executionResult.Result);

            // Only retry if the action specifies we should AND the action did not already
            while (retryAction == PluginRetryAction.Retry && executionResult.RetryStatus == PluginRetryStatus.DidNotRetry)
            {
                PluginResult currentResult = executionResult.Result;

                OnStatusMessageUpdate($"Retry triggered for state {currentResult}).");
                _retryCounts.Increment(currentResult);
                LogRetryData(executionResult);
                DelayBeforeRetry(_executionData.RetrySettings[currentResult]);

                OnStatusMessageUpdate($"Starting retry number {TotalRetries}.");
                executionResult = InvokeAction(action);
                retryAction     = GetRetryAction(executionResult.Result);
            }

            // Update the retry status if it is still set to "did not retry"
            if (executionResult.RetryStatus == PluginRetryStatus.DidNotRetry)
            {
                // Determine the final retry status based on the specified retry action
                switch (retryAction)
                {
                case PluginRetryAction.Halt:
                    OnStatusMessageUpdate("Halting execution per retry settings.");
                    executionResult.RetryStatus = PluginRetryStatus.Halt;
                    break;

                case PluginRetryAction.Continue:
                default:
                    executionResult.RetryStatus = PluginRetryStatus.Continue;
                    break;
                }
            }

            return(executionResult);
        }