Exemplo n.º 1
0
        private Task EstablishConnection()
        {
            if (disposed)
            {
                return(new CompletedTask());
            }

            var requestParams = new CreateHttpJsonRequestParams(null, url + "/changes/events?id=" + id, "GET", credentials, conventions)
            {
                AvoidCachingRequest = true
            };

            return(jsonRequestFactory.CreateHttpJsonRequest(requestParams)
                   .ServerPullAsync()
                   .ContinueWith(task =>
            {
                if (task.IsFaulted && reconnectAttemptsRemaining > 0)
                {
                    logger.WarnException("Could not connect to server, will retry", task.Exception);
                    Connected = false;
                    ConnectionStatusCahnged(this, EventArgs.Empty);

                    reconnectAttemptsRemaining--;

                    return Time.Delay(TimeSpan.FromSeconds(15))
                    .ContinueWith(_ => EstablishConnection())
                    .Unwrap();
                }

                Connected = true;
                ConnectionStatusCahnged(this, EventArgs.Empty);
                reconnectAttemptsRemaining = 3;                                                         // after the first successful try, we will retry 3 times before giving up
                connection = (IDisposable)task.Result;
                task.Result.Subscribe(this);

                Task prev = watchAllDocs ? Send("watch-docs", null) : new CompletedTask();

                if (watchAllIndexes)
                {
                    prev = prev.ContinueWith(_ => Send("watch-indexes", null));
                }

                prev = watchedDocs.Aggregate(prev, (cur, docId) => cur.ContinueWith(task1 => Send("watch-doc", docId)));

                prev = watchedPrefixes.Aggregate(prev, (cur, prefix) => cur.ContinueWith(task1 => Send("watch-prefix", prefix)));

                prev = watchedIndexes.Aggregate(prev, (cur, index) => cur.ContinueWith(task1 => Send("watch-indexes", index)));

                return prev;
            })
                   .Unwrap());
        }
Exemplo n.º 2
0
        private Task EstablishConnection()
        {
            if (disposed)
            {
                return(new CompletedTask());
            }

            var requestParams = new CreateHttpJsonRequestParams(null, url + "/changes/events?id=" + id, "GET", credentials, conventions)
            {
                AvoidCachingRequest = true
            };

            return(jsonRequestFactory.CreateHttpJsonRequest(requestParams)
                   .ServerPullAsync()
                   .ContinueWith(task =>
            {
                if (disposed)
                {
                    throw new ObjectDisposedException("RemoteDatabaseChanges");
                }
                if (task.IsFaulted)
                {
                    logger.WarnException("Could not connect to server, will retry", task.Exception);
                    Connected = false;
                    ConnectionStatusChanged(this, EventArgs.Empty);

                    if (disposed)
                    {
                        return task;
                    }


                    if (replicationInformer.IsServerDown(task.Exception) == false)
                    {
                        return task;
                    }

                    if (replicationInformer.IsHttpStatus(task.Exception,
                                                         HttpStatusCode.NotFound,
                                                         HttpStatusCode.Forbidden))
                    {
                        return task;
                    }

                    return Time.Delay(TimeSpan.FromSeconds(15))
                    .ContinueWith(_ => EstablishConnection())
                    .Unwrap();
                }

                Connected = true;
                ConnectionStatusChanged(this, EventArgs.Empty);
                connection = (IDisposable)task.Result;
                task.Result.Subscribe(this);

                Task prev = watchAllDocs ? Send("watch-docs", null) : new CompletedTask();

                if (watchAllIndexes)
                {
                    prev = prev.ContinueWith(_ => Send("watch-indexes", null));
                }

                prev = watchedDocs.Aggregate(prev, (cur, docId) => cur.ContinueWith(task1 => Send("watch-doc", docId)));

                prev = watchedPrefixes.Aggregate(prev, (cur, prefix) => cur.ContinueWith(task1 => Send("watch-prefix", prefix)));

                prev = watchedIndexes.Aggregate(prev, (cur, index) => cur.ContinueWith(task1 => Send("watch-indexes", index)));

                return prev;
            })
                   .Unwrap());
        }