Exemplo n.º 1
0
        private void longPoll()
        {
            if (Utils.isStartingFromWindowsRestart())
            {
                Thread.Sleep(this.config.wait_before_start_from_windows_restart * 1000);
            }

            logger.write("Checking internet/network connection...", Logger.LOGLEVEL.INFO);

            while (!NetworkMonitoring.IsInternetAvailable())
            {
                logger.write(String.Format("No internet connection. Will try again after {0} secs.", this.config.try_again_after), Logger.LOGLEVEL.ERROR);
                Thread.Sleep(this.config.try_again_after * 1000);
            }

            logger.write("Internet/network connection is succesful.", Logger.LOGLEVEL.INFO);

            this.logger.ensureConnect();

            logger.write("Checking SQL SERVER connection...", Logger.LOGLEVEL.INFO);

            string errorMessage;

            while (!Utils.try_connect_to_db(this.config.cprDatabaseConnectionString, out errorMessage))
            {
                logger.write(String.Format("Had an error while trying to connect to SQL SERVER. Will try again after {0} secs.", this.config.try_again_after), Logger.LOGLEVEL.ERROR);
                logger.write(errorMessage, Logger.LOGLEVEL.ERROR);
                Thread.Sleep(this.config.try_again_after * 1000);
            }

            logger.write("SQL SERVER connection is succesful.", Logger.LOGLEVEL.INFO);

            logger.write("STARTING ECPR service...", Logger.LOGLEVEL.INFO);

            this.serviceControlCenter = new ServiceControlCenter(this.config, this.logger);
            this.serviceControlCenter.addManager(this);

            this.logger.write(String.Format("Connecting to database {0} ...", this.config.cprDatabase), Logger.LOGLEVEL.INFO);

            this.sqlConnection = new SqlConnection(connectionString);
            Utils.maybe_reconnect(this, ref this.sqlConnection, this.config.try_again_after, this.logger);

            System.Timers.Timer ecprTimer = new System.Timers.Timer();
            ecprTimer.Elapsed  += (sender, e) => Utils.maybe_reconnect(this, ref this.sqlConnection, this.config.try_again_after, this.logger);
            ecprTimer.Interval += (this.config.defaultDatabaseConnectionTimeout * 1000 + 5000);
            ecprTimer.Enabled   = true;

            if (isReadyToBeStarted)
            {
                this.ensureTablesExisting();
                this.ensureFieldsExisting();
                this.getIgnoreFieldsPK();
            }

            if (sqlConnection.State == ConnectionState.Open)
            {
                this.syncControlCenter     = new SyncControlCenter(this.config, this.logger);
                this.fullsyncControlCenter = new FullSyncControlCenter(this.config, this.logger);
            }


            if ((syncControlCenter != null && !syncControlCenter.isSubscribed) ||
                (serviceControlCenter != null && !serviceControlCenter.isSubscribed))
            {
                logger.write(
                    String.Format("Cannot subscribed to amqp channels, will try again in {0} secs !!!!!!!", intervalSecs / 1000),
                    Logger.LOGLEVEL.ERROR);
            }

            if (!this.config.needToFetchSchemaAndData)
            {
                dbSchemaPush();
                dbDataPush();
                this.config.needToFetchSchemaAndData = true;
            }

            if (sqlConnection.State == ConnectionState.Open)
            {
                this.isReadyToBeStarted = true;

                foreach (string tableName in this.config.cprTables.Keys)
                {
                    try
                    {
                        // Make sure table is exists before subscribe
                        bool          tableExists   = false;
                        SqlDataReader sqlDataReader = this.doSQL(String.Format("select TABLE_NAME from INFORMATION_SCHEMA.TABLES where TABLE_NAME ='{0}'", tableName));

                        if (sqlDataReader != null)
                        {
                            if (sqlDataReader.HasRows)
                            {
                                tableExists = true;
                            }
                            sqlDataReader.Close();
                        }

                        if (tableExists)
                        {
                            var qN = new QueryNotification(
                                this.config.cprTables[tableName].ToArray(),
                                tableName,
                                this.logger,
                                this.config,
                                this.config.restListAPIJSONMapping,
                                this.config.autoCleanupIfRestart);
                            qN.subscribe();
                        }
                        else
                        {
                            this.logger.write(String.Format("Cannot subscribed the table {0} for query notification because it doesn't exists in database. Ignored it!", tableName), Logger.LOGLEVEL.ERROR);
                        }
                    }
                    catch (Exception error)
                    {
                        this.logger.write(ECPRERROR.ExceptionDetail(error), Logger.LOGLEVEL.ERROR);
                        this.isReadyToBeStarted = false;
                    }
                }
            }

            if (this.config.diff_tables != null && this.isReadyToBeStarted)
            {
                this.logger.write("Enable DIFF Tables feature.", Logger.LOGLEVEL.INFO);
                cprDataAnalysis = new CPRDataAnalysis(this.config, this.logger);
                cprDataAnalysis.Init();
            }


            this._eventsThread = new Thread(eventsProcess);
            this._eventsThread.IsBackground = false;
            this._eventsThread.Start();

            this._eventsTimer           = new System.Timers.Timer(this.intervalSecs);
            this._eventsTimer.AutoReset = false;
            this._eventsTimer.Elapsed  += (sender, e) => this._inProcessEvent.Set();
            this._eventsTimer.Start();

            logger.write("SERVICE STARTED!!!", Logger.LOGLEVEL.INFO);
        }
Exemplo n.º 2
0
        public void collect_current_data(FullSyncControlCenter syncControlCenter)
        {
            string strQuery = String.Format("SELECT {0} FROM {1}", string.Join(",", this.tableFieldsNotification), this.tableName);
            List <SqlParameter> sqlParams = new List <SqlParameter>();

            if (this.tableFilter != null)
            {
                string  _field = this.tableFilter["field"];
                string  _op    = this.tableFilter["op"];
                dynamic _value = this.tableFilter["value"];

                _field = _field.Trim();
                _op    = _op.Trim();

                if (_field == String.Empty || _op == String.Empty || _value == null)
                {
                    this.logger.write("Invalid sync.filter params.", Logger.LOGLEVEL.ERROR);
                    return;
                }
                else if (!this.tableFilterOperators.Contains(_op.ToUpper()))
                {
                    this.logger.write(String.Format("Invalid sync.filter.op operator. It should be in [{0}]",
                                                    String.Join(",", this.tableFilterOperators)),
                                      Logger.LOGLEVEL.ERROR);
                    return;
                }
                else if (_op.ToUpper() == "IN" && !(_value is Newtonsoft.Json.Linq.JArray))
                {
                    this.logger.write("Invalid sync.filter.value. It should be a list of values because sync.filter.op is 'IN'", Logger.LOGLEVEL.ERROR);
                    return;
                }
                else if (_op.ToUpper() != "IN" && _value is Newtonsoft.Json.Linq.JArray)
                {
                    this.logger.write(String.Format("Invalid sync.filter.value. It should be a value, NOT a list of values because sync.filter.op is '{0}'", _op.ToUpper()),
                                      Logger.LOGLEVEL.ERROR);
                    return;
                }
                else
                {
                    if (_value is Newtonsoft.Json.Linq.JArray)
                    {
                        List <string> bindParams = new List <string>();
                        int           i          = 1;
                        foreach (Newtonsoft.Json.Linq.JValue jValue in _value)
                        {
                            var          bindParam = String.Format("@FIELD_VALUE_{0}", i);
                            SqlParameter sqlParam  = new SqlParameter();
                            sqlParam.ParameterName = bindParam;
                            sqlParam.Value         = jValue.Value;
                            sqlParams.Add(sqlParam);
                            bindParams.Add(bindParam);
                            i++;
                        }

                        strQuery += String.Format(" WHERE {0} IN ({1})", _field, string.Join(",", bindParams));
                    }
                    else
                    {
                        SqlParameter sqlParam = new SqlParameter();
                        sqlParam.ParameterName = "@FIELD_VALUE";

                        strQuery      += String.Format(" WHERE {0} {1} @FIELD_VALUE", _field, _op);
                        sqlParam.Value = _value.Value;

                        sqlParams.Add(sqlParam);
                    }
                }
            }

            using (SqlConnection _sqlConnection = new SqlConnection(config.cprDatabaseConnectionString))
            {
                SqlDataReader sqlDataReader = this.executeQuery(strQuery, _sqlConnection, CommandBehavior.Default, sqlParams);

                if (sqlDataReader != null)
                {
                    if (sqlDataReader.HasRows)
                    {
                        while (sqlDataReader.Read())
                        {
                            var data = new Dictionary <string, object>();

                            for (int i = 0; i < sqlDataReader.FieldCount; i++)
                            {
                                //if (!sqlDataReader.IsDBNull(i))
                                try
                                {
                                    data[sqlDataReader.GetName(i).ToLower()] = sqlDataReader[i];
                                }
                                catch (InvalidOperationException)
                                {
                                    //FIXME: Transaction lose!!!
                                    //Need to push this message to data cache
                                    //To get it push to rabbitmq in the next
                                    //retry. *sigh*
                                }
                            }

                            var cprRowData = new CPRRowData()
                            {
                                type     = "fullsync",
                                datetime = Utils.getUnixTimeStamp(),
                                source   = this.tableName,
                                uuid     = Guid.NewGuid().ToString(),
                                data     = data,
                                customer = this.config.cprCustomerCode
                            };

                            syncControlCenter.publishMessage(JsonConvert.SerializeObject(cprRowData, this.jsonSerializerSettings));
                        }
                        this.logger.write(String.Format("Sent COLLECTALLDATA_{0} to amqp broker server.", this.tableName), Logger.LOGLEVEL.INFO);
                    }

                    sqlDataReader.Close();
                }
            }
        }