예제 #1
0
        public async override Task <object> HandleReplicatePushRequest(string databaseName, IDictionary <string, object> parameters, C8oResponseListener c8oResponseListener)
        {
            var fullSyncDatabase = await GetOrCreateFullSyncDatabase(databaseName);

            try
            {
                fullSyncDatabase.StartPushReplication(parameters, c8oResponseListener);
            }
            catch (Exception e)
            {
                throw new C8oException(C8oExceptionMessage.FullSyncReplicationFail(databaseName, "push"), e);
            }
            return(VoidResponse.GetInstance());
        }
예제 #2
0
        private async Task <object> postReplicate(string fullSyncDatatbaseName, IDictionary <string, object> parameters, C8oResponseListener c8oResponseListener, bool isPull)
        {
            bool createTarget = true;
            bool continuous   = false;
            bool cancel       = false;

            if (parameters.ContainsKey("create_target"))
            {
                createTarget = parameters["create_target"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase);
            }

            if (parameters.ContainsKey("continuous"))
            {
                continuous = parameters["continuous"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase);
            }

            if (parameters.ContainsKey("cancel"))
            {
                cancel = parameters["cancel"].ToString().Equals("true", StringComparison.OrdinalIgnoreCase);
            }

            JToken local  = fullSyncDatatbaseName + localSuffix;
            var    remote = new JObject();

            remote["url"] = fullSyncDatabaseUrlBase + fullSyncDatatbaseName + '/';

            var cookies = c8o.CookieStore;

            if (cookies.Count > 0)
            {
                var headers      = new JObject();
                var cookieHeader = new StringBuilder();

                foreach (Cookie cookie in cookies.GetCookies(new Uri(c8o.EndpointConvertigo + '/')))
                {
                    cookieHeader.Append(cookie.Name).Append("=").Append(cookie.Value).Append("; ");
                }

                cookieHeader.Remove(cookieHeader.Length - 2, 2);

                headers["Cookie"] = cookieHeader.ToString();
                remote["headers"] = headers;
            }

            var request = HttpWebRequest.CreateHttp(serverUrl + "/_replicate");

            request.Method = "POST";

            var json = new JObject();

            string sourceId = (isPull ? remote["url"] : local).ToString();
            string targetId = (isPull ? local : remote["url"]).ToString();

            json["source"]        = isPull ? remote : local;
            json["target"]        = isPull ? local : remote;
            json["create_target"] = createTarget;
            json["continuous"]    = false;
            json["cancel"]        = true;

            var response = await Execute(request, json);

            c8o.Log._Warn("CANCEL REPLICATE:\n" + response.ToString());

            if (cancel)
            {
                return(response);
            }

            json["cancel"] = false;

            request        = HttpWebRequest.CreateHttp(serverUrl + "/_replicate");
            request.Method = "POST";

            response = null;

            var param    = new Dictionary <string, object>(parameters);
            var progress = new C8oProgress();

            progress.Pull     = isPull;
            progress.Status   = "Active";
            progress.Finished = false;


            Task.Run(async() =>
            {
                long checkPoint_Interval = 1000;

                while (response == null)
                {
                    await Task.Delay(TimeSpan.FromMilliseconds(checkPoint_Interval));

                    if (response != null)
                    {
                        break;
                    }

                    var req    = HttpWebRequest.CreateHttp(serverUrl + "/_active_tasks");
                    req.Method = "GET";

                    var res = await Execute(req);

                    if (response != null)
                    {
                        break;
                    }

                    c8o.Log._Warn(res.ToString());

                    JObject task = null;
                    foreach (JToken item in res["item"])
                    {
                        if (item["target"].ToString() == targetId && item["source"].ToString() == sourceId)
                        {
                            task = item as JObject;
                            break;
                        }
                    }

                    if (task != null)
                    {
                        checkPoint_Interval = (long)task["checkpoint_interval"].ToObject(typeof(long));

                        progress.Raw      = task;
                        progress.Total    = task["source_seq"].Value <long>();
                        progress.Current  = task["revisions_checked"].Value <long>();
                        progress.TaskInfo = task.ToString();

                        c8o.Log._Warn(progress.ToString());

                        if (progress.Changed)
                        {
                            var newProgress = progress;
                            progress        = new C8oProgress(progress);

                            if (c8oResponseListener != null && c8oResponseListener is C8oResponseProgressListener)
                            {
                                param[C8o.ENGINE_PARAMETER_PROGRESS] = newProgress;
                                (c8oResponseListener as C8oResponseProgressListener).OnProgressResponse(progress, param);
                            }
                        }
                    }
                }
            }).GetAwaiter();

            response = await Execute(request, json);

            response.Remove("_c8oMeta");

            progress.Total    = response["source_last_seq"].Value <long>();
            progress.Current  = response["source_last_seq"].Value <long>();
            progress.TaskInfo = response.ToString();
            progress.Status   = "Stopped";
            progress.Finished = true;

            if (c8oResponseListener != null && c8oResponseListener is C8oResponseProgressListener)
            {
                (c8oResponseListener as C8oResponseProgressListener).OnProgressResponse(progress, param);
            }

            if (continuous)
            {
                progress.Status    = "Idle";
                json["continuous"] = true;

                request        = HttpWebRequest.CreateHttp(serverUrl + "/_replicate");
                request.Method = "POST";

                response = await Execute(request, json);

                c8o.Log._Warn(response.ToString());

                /*
                 * string localId = response["_local_id"].ToString();
                 * localId = localId.Substring(0, localId.IndexOf('+'));
                 *
                 * do {
                 *  request = HttpWebRequest.CreateHttp(GetDatabaseUrl(fullSyncDatatbaseName) + "/_local/" + localId);
                 *  c8o.Log(C8oLogLevel.WARN, request.RequestUri.ToString());
                 *  request.Method = "GET";
                 *
                 *  response = await Execute(request);
                 *  c8o.Log(C8oLogLevel.WARN, response.ToString());
                 * } while(response["hystory"] != null);
                 */
            }

            return(VoidResponse.GetInstance());
        }