Exemplo n.º 1
0
        private async Task SendAsync(Notify notify, MessageControl act)
        {
            var        n       = notify.Notification;
            HttpClient http    = new HttpClient();
            var        message = new HttpRequestMessage(HttpMethod.Post, n.Subscription.Url);

            n.Tried++;
            var content = new StringContent(n.ToString(), Encoding.UTF8, "application/json");

            message.Content = content;
            CancellationTokenSource tcs = new CancellationTokenSource();

            tcs.CancelAfter(10000);

            var subscription = await Configuration.GetSubscriptionsTable().ReadOneAsync(n.Subscription.Id).ConfigureAwait(false);

            if (subscription == null)
            {
                return;
            }

            bool failed = false;

            try
            {
                var response = await http.SendAsync(message, tcs.Token).ConfigureAwait(false);

                failed = !response.IsSuccessStatusCode;
            }
            catch
            {
                failed = true;
            }
            var tries = new[]
            {
                TimeSpan.FromSeconds(0.0),
                TimeSpan.FromMinutes(1.0),
                TimeSpan.FromMinutes(5.0),
                TimeSpan.FromMinutes(30.0),
                TimeSpan.FromMinutes(60.0),
                TimeSpan.FromHours(7.0),
                TimeSpan.FromHours(14.0),
                TimeSpan.FromHours(24.0),
                TimeSpan.FromHours(24.0),
                TimeSpan.FromHours(24.0),
                TimeSpan.FromHours(24.0),
                TimeSpan.FromHours(24.0)
            };

            if (!notify.SendAndForget && failed && (n.Tried - 1) <= tries.Length - 1)
            {
                var wait = tries[n.Tried - 1];
                act.RescheduleIn(wait);
            }
        }
Exemplo n.º 2
0
        public IDisposable OnMessageAsync(Func <T, MessageControl, Task> evt, OnMessageOptions options = null)
        {
            if (options == null)
            {
                options = new OnMessageOptions()
                {
                    AutoComplete       = true,
                    MaxConcurrentCalls = 10,
                    AutoRenewTimeout   = TimeSpan.Zero
                }
            }
            ;

            return(OnMessageAsyncCore(async bm =>
            {
                var control = new MessageControl();
                control.Options = options;
                var obj = ToObject(bm);
                if (obj == null)
                {
                    return;
                }
                await evt(obj, control).ConfigureAwait(false);
                if (control._Scheduled != null)
                {
                    BrokeredMessage message = new BrokeredMessage(Serializer.ToString(obj));
                    message.MessageId = Encoders.Hex.EncodeData(RandomUtils.GetBytes(32));
                    message.ScheduledEnqueueTimeUtc = control._Scheduled.Value;
                    await SendAsync(message).ConfigureAwait(false);
                    if (!options.AutoComplete)
                    {
                        if (control._Complete)
                        {
                            try
                            {
                                await bm.CompleteAsync().ConfigureAwait(false);
                            }
                            catch (ObjectDisposedException)
                            {
                                ListenerTrace.Error("Brokered message already disposed", null);
                            }
                        }
                    }
                }
            }, options));
        }