Exemplo n.º 1
0
        /// <summary>
        /// Retry a task composition using the specified retry policy
        /// </summary>
        /// <param name="composer">The task composer</param>
        /// <param name="interval">The interval between each repetition</param>
        /// <param name="callback">The task composition callback</param>
        /// <param name="repeatCancellationToken">A cancellation token for the repeat-portion of the task composer</param>
        /// <returns>The original task composer</returns>
        public static Composer Repeat(this Composer composer, TimeSpan interval, Action <Composer> callback,
                                      CancellationToken repeatCancellationToken = default(CancellationToken))
        {
            if (interval < TimeSpan.Zero)
            {
                throw new ArgumentOutOfRangeException("interval",
                                                      "The interval must be non-negative, and it must be less than or equal to Int32.MaxValue.");
            }

            composer.ComposeTask(taskComposer => Attempt(taskComposer, interval, callback, repeatCancellationToken));

            return(composer);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Retry a task composition using the specified retry policy
        /// </summary>
        /// <param name="composer">The task composer</param>
        /// <param name="millisecondsInterval"></param>
        /// <param name="callback">The task composition callback</param>
        /// <param name="repeatCancellationToken"></param>
        /// <returns>The original task composer</returns>
        public static Composer Repeat(this Composer composer, int millisecondsInterval, Action <Composer> callback,
                                      CancellationToken repeatCancellationToken)
        {
            if (millisecondsInterval < 0)
            {
                throw new ArgumentOutOfRangeException("millisecondsInterval",
                                                      "The interval must be non-negative, and it must be less than or equal to Int32.MaxValue.");
            }

            composer.ComposeTask(
                taskComposer => Attempt(taskComposer, TimeSpan.FromMilliseconds(millisecondsInterval), callback, repeatCancellationToken));

            return(composer);
        }
Exemplo n.º 3
0
        static void Attempt(Composer composer, TimeSpan interval, Action <Composer> callback, CancellationToken repeatCancellationToken)
        {
            composer.ComposeTask(callback);

            composer.ExecuteAsync(token =>
            {
                if (repeatCancellationToken.IsCancellationRequested)
                {
                    return(TaskUtil.Completed());
                }

                return(ComposerFactory.Compose(x =>
                {
                    x.Delay(interval);

                    Attempt(x, interval, callback, repeatCancellationToken);
                }, token));
            }, ExecuteOptions.RunSynchronously);
        }
Exemplo n.º 4
0
        static void Attempt <T>(Composer <T> composer, IRetryContext retryContext, Action <Composer <T> > callback)
        {
            composer.ComposeTask(callback);

            composer.Compensate(compensation =>
            {
                TimeSpan delay;
                if (!retryContext.CanRetry(compensation.Exception, out delay))
                {
                    return(compensation.Throw());
                }

                return(compensation.ComposeTask(x =>
                {
                    x.Delay(delay);

                    Attempt(x, retryContext, callback);
                }));
            });
        }
Exemplo n.º 5
0
        /// <summary>
        /// Retry a task composition using the specified retry policy
        /// </summary>
        /// <typeparam name="T">The composer payload type</typeparam>
        /// <param name="composer">The task composer</param>
        /// <param name="retryPolicy">The retry policy</param>
        /// <param name="callback">The task composition callback</param>
        /// <returns>The original task composer</returns>
        public static Composer <T> Retry <T>(this Composer <T> composer, IRetryPolicy retryPolicy, Action <Composer <T> > callback)
        {
            IRetryContext retryContext = null;

            composer.ComposeTask(taskComposer =>
            {
                retryContext = retryPolicy.GetRetryContext();

                Attempt(taskComposer, retryContext, callback);
            });

            composer.Finally((_, status) =>
            {
                if (retryContext != null)
                {
                    retryContext.Complete(status);
                }
            });

            return(composer);
        }
Exemplo n.º 6
0
 public void Compose <TPayload>(Composer <Payload <TPayload> > composer, Carrier <Tuple <TPayload, T> > next)
 {
     composer.ComposeTask(p => composer.Payload.MergeRight(new T()), next.Compose);
 }
Exemplo n.º 7
0
 public static Composer <Payload <T> > ComposeCarrier <T, TPayload>(this Composer <Payload <T> > composer,
                                                                    Carrier <Tuple <T, TPayload> > next,
                                                                    Func <Payload <T>, Payload <Tuple <T, TPayload> > > payload)
 {
     return(composer.ComposeTask(payload, next.Compose));
 }
Exemplo n.º 8
0
        static void Attempt(Composer composer, TimeSpan interval, Action<Composer> callback, CancellationToken repeatCancellationToken)
        {
            composer.ComposeTask(callback);

            composer.ExecuteAsync(token =>
            {
                if (repeatCancellationToken.IsCancellationRequested)
                    return TaskUtil.Completed();

                return ComposerFactory.Compose(x =>
                {
                    x.Delay(interval);

                    Attempt(x, interval, callback, repeatCancellationToken);
                }, token);
            }, ExecuteOptions.RunSynchronously);
        }