Exemple #1
0
 public void dbSchemaPush(string tableName = null)
 {
     if (sqlConnection.State == ConnectionState.Open)
     {
         QueryNotification fortheFistStart = new QueryNotification(
             null,
             null,
             this.logger,
             this.config,
             this.config.restListAPIJSONMapping, null);
         fortheFistStart.collect_current_schema(this.config.cprTables, this.config.cprTableOrder, tableName);
         fortheFistStart = null;
         GC.Collect();
     }
 }
Exemple #2
0
        public void dbFullSync(string tableName, dynamic filter = null)
        {
            if (sqlConnection.State == ConnectionState.Open)
            {
                QueryNotification fortheFistStart = new QueryNotification(
                    this.config.cprTables[tableName].ToArray(),
                    tableName,
                    this.logger,
                    this.config,
                    this.config.restListAPIJSONMapping, filter);

                fortheFistStart.collect_current_data(this.fullsyncControlCenter);
                fortheFistStart = null;

                GC.Collect();
            }
        }
Exemple #3
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);
        }