Example #1
0
        protected abstract void LogToDatabase(string strMessage); // Add other parameters later.

        private void ReadTargetsFromDatabase()
        {
            List <Target>   lstEnabledTargets = new List <Target>();
            MySqlConnection con = DatabaseConnectionProvider.GetMySqlConnection();

            // ThAW TO_DO 2009/08/26 : Use a Select command that only selects enabled targets that are associated with enabled accounts.
            string strCommandText = Target.SelectAllCommand;

            using (MySqlCommand cmd = new MySqlCommand(strCommandText, con))
            {
                using (MySqlDataReader reader = cmd.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        Target target = new Target(reader, false);  // Do not close the reader after constructing the object

                        if (target.Enabled)
                        {
                            lstEnabledTargets.Add(target);
                        }
                    }
                }
            }

            foreach (Target target in lstEnabledTargets)
            {
                target.FinishConstruction(con);
                m_pqWaitingTargets.Enqueue(target);
                LogToConsole(string.Format(@"Constructed and enqueued the enabled target named '{0}'", target.Name));
            }
        }
Example #2
0
        /// <summary>
        ///
        /// </summary>
        public void Update()
        {
            using (var connection = new DatabaseConnectionProvider().GetConnection(_context.SqlConnectionString))
            {
                connection.Open();

                var exproc = connection.ExecuteScalar <int>("select object_id('" + _context.MetadataTable + "Register')");
                if (0 == exproc)
                {
                    throw new Exception("В целевой БД отсутвует процедура " + _context.MetadataTable + "Register");
                }
                //сначала надо прорегистрировать скрипт, и только потом его выполнять, так как на него могут идти ссылки
                var register = "exec " + _context.MetadataTable +
                               "Register @code=@code,@name=@code,@content=@content, @hash=@hash,@revision=@revision,@filetime=@filetime,@comment=@comment";

                connection.ExecuteNonQuery(register, new { code = _context.ProjectName + ".project", comment = _context.FullUpdate || !_context.GitBaseRevision.ToBool()?"full":"diff", content = string.Join("\r\nGO\r\n", _context.SqlScripts), filetime = DateTime.Now, hash = _context.ResolvedUpdateRevision, revision = _context.ResolvedUpdateRevision.Substring(0, 7) });
                if (_context.OnlyRegister)
                {
                    _context.Log.Trace("Включен режим 'только регистрация', сам скрипт обновления не выполняется");
                }
                else
                {
                    foreach (var script in _context.SqlScripts)
                    {
                        _context.Log.Trace("Begin ExecuteSql : '" + script.Substring(0, Math.Min(100, script.Length)) + "' at " + DateTime.Now.ToString());
                        connection.ExecuteNonQuery(script, timeout: 30000);
                        _context.Log.Trace("End ExecuteSql : '" + script.Substring(0, Math.Min(100, script.Length)) + "' at " + DateTime.Now.ToString());
                    }
                }
            }
        }
Example #3
0
        public void CanCreateConnection()
        {
            var dcp        = new DatabaseConnectionProvider();
            var connection = dcp.GetConnection(connectionString);

            Assert.AreEqual("NpgsqlConnection", connection.GetType().Name);
        }
Example #4
0
        public void CanOpenConnection()
        {
            var dcp = new DatabaseConnectionProvider();

            using (var c = dcp.GetConnection(connectionString)) {
                c.Open();
            }
        }
Example #5
0
        public static TaskListViewModel Create()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
            var    provider         = new DatabaseConnectionProvider(connectionString);

            ITaskFactory    taskFactory    = new SqlTaskFactory(provider);
            ITaskRepository taskRepository = new SqlTaskRepository(provider);

            return(new TaskListViewModel(taskFactory, taskRepository));
        }
Example #6
0
        public void DatabaseConnectionProvider_Instance_Method_Returns_Instance()
        {
            //Arrange
            ComponentFactory.Container = new SimpleContainer();
            ComponentFactory.RegisterComponentInstance <DatabaseConnectionProvider>(new FakeDbConnectionProvider());

            //Act
            var provider = DatabaseConnectionProvider.Instance();

            //Assert
            Assert.IsInstanceOf <DatabaseConnectionProvider>(provider);
            Assert.IsInstanceOf <FakeDbConnectionProvider>(provider);
        }
Example #7
0
 /// <summary>
 ///
 /// </summary>
 public void Determine()
 {
     using (var connection = new DatabaseConnectionProvider().GetConnection(_context.SqlConnectionString)){
         connection.Open();
         var exproc = connection.ExecuteScalar <int>("select object_id('" + _context.MetadataTable + "')");
         if (0 == exproc)
         {
             throw new Exception("В целевой БД отсутвует таблица " + _context.MetadataTable);
         }
         var currev =
             connection.ExecuteScalar <string>("select ActiveRevisionRevision from " + _context.MetadataTable +
                                               "Full where Code='" + _context.ProjectName + ".project'");
         _context.GitBaseRevision = currev;
     }
 }
Example #8
0
 public CircleQueryService(DatabaseConnectionProvider provider)
 {
     this.provider = provider;
 }
Example #9
0
 public SqlUserRepository(DatabaseConnectionProvider provider)
 {
     this.provider = provider;
 }
 public SqlCircleQueryService(DatabaseConnectionProvider provider, ICircleRepository circleRepository)
 {
     this.provider         = provider;
     this.circleRepository = circleRepository;
 }
Example #11
0
 public SqlCircleGetSummariesInteractor(DatabaseConnectionProvider provider)
 {
     this.provider = provider;
 }
Example #12
0
 public SqlCircleFactory(DatabaseConnectionProvider provider)
 {
     this.provider = provider;
 }
Example #13
0
        public void TestPreValidateSqlExecution()
        {
            using (var connection = new DatabaseConnectionProvider().GetConnection())
            {
                var target = CreatePrivateAccessor();
                var actual = target.PreValidateSqlExecution(connection, "Some Text");
                Assert.IsTrue(actual);

                actual = target.PreValidateSqlExecutionTx(connection.BeginTransaction(), "Some Text");
                Assert.IsTrue(actual);
            }
        }
Example #14
0
        public void Run()
        {
            LogToConsole(@"Flare main loop: start");

            lock (this)
            {
                m_eServiceStatus = FlareServiceStatus.Started;
            }

            try
            {
                m_pqWaitingTargets.Clear();
                m_qTargetsReadyForTesting.Clear();

                ReadTargetsFromDatabase();

                MySqlConnection con = DatabaseConnectionProvider.GetMySqlConnection();

                m_lstAllContacts = Contact.GetAllContacts(con);
                m_dictSystemConfigurationEntries = SystemConfigurationEntry.GetDictionary(con);

                if (m_pqWaitingTargets.IsEmpty())
                {
                    LogToConsole(@"Aborting: The priority queue is empty; there are no servers to monitor.");
                    return;
                }

                // The main loop begins here.
                TimeSpan tsOneSecond  = new TimeSpan(0, 0, 1);
                TimeSpan tsLoopPeriod = new TimeSpan(0, 0, 1);
                bool     bLimitNumberOfMainLoopIterations = (NumberOfMainLoopIterations > 0);

                LogToConsole(@"Starting the main loop...");

                for (int nMainLoopIterationNumber = 0;
                     !bLimitNumberOfMainLoopIterations || nMainLoopIterationNumber < NumberOfMainLoopIterations;
                     ++nMainLoopIterationNumber)
                {
                    DateTime dtNow = DateTime.UtcNow;

                    LogToConsole(string.Format(@"It is now {0}.", dtNow.ToLongTimeString()));

                    if (m_pqWaitingTargets.IsEmpty())
                    {
                        LogToConsole(@"The priority queue is empty; sleeping for one second.");
                        System.Threading.Thread.Sleep(tsOneSecond);
                        continue;
                    }

                    Target targetHead = m_pqWaitingTargets.Peek();

                    LogToConsole(string.Format(@"The target at the head of the queue ('{0}') is due to be tested at {1}.",
                                               targetHead.Name, targetHead.DateTimeOfNextMonitor.ToLongTimeString()));

                    if (dtNow < targetHead.DateTimeOfNextMonitor)
                    {
                        TimeSpan tsDifference      = targetHead.DateTimeOfNextMonitor - dtNow;
                        TimeSpan tsTimeSpanToSleep = (tsDifference < tsLoopPeriod) ? tsDifference : tsLoopPeriod;

                        LogToConsole(string.Format(@"It is not yet time to test the target at the head of the queue.  Sleeping for {0} seconds and {1} milliseconds...",
                                                   tsTimeSpanToSleep.Seconds, tsTimeSpanToSleep.Milliseconds));

                        System.Threading.Thread.Sleep(tsTimeSpanToSleep);
                        continue;
                    }

                    lock (m_qTargetsReadyForTesting)
                    {
                        m_qTargetsReadyForTesting.Enqueue(m_pqWaitingTargets.Dequeue());
                    }

#if MULTITHREAD_TARGET_TESTING
                    // Note: There is also a delegate named ParameterizedThreadStart; perhaps it may be useful
                    // in allowing us to pass a TargetInfo object as a parameter, thus avoiding the m_qTargetsReadyForTesting queue.
                    Thread thread = new Thread(new ThreadStart(ThreadMain_TestTarget));

                    thread.Start();
#else
                    ThreadMain_TestTarget();
#endif

                    lock (this)
                    {
                        if (m_eServiceStatus == FlareServiceStatus.StopRequested)
                        {
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                LogToConsole(string.Format(@"{0} caught: {1}", ex.GetType().FullName, ex.Message));
            }

            lock (this)
            {
                m_eServiceStatus = FlareServiceStatus.Stopped;
            }

            LogToConsole(@"Flare main loop: end");
        }
Example #15
0
        private void ThreadMain_TestTarget()
        {
            LogToConsole(@"**** Thread starting ****");

            Target target = null;

            lock (m_qTargetsReadyForTesting)
            {
                if (m_qTargetsReadyForTesting.Count <= 0)
                {
                    // Throw an exception when we attempt to Dequeue() ?
                    return;
                }

                target = m_qTargetsReadyForTesting.Dequeue();
            }

            LogToConsole(string.Format(@"Testing the target {0} (at {1}).", target.Name, target.URL));

            // In a separate thread:
            // - HTTP Post to the target
            // - Handle any errors (by logging to a database and sending e-mail(s))
            // - Re-enqueue the TargetInfo object while the priority queue is locked.
            TargetLogRecord tlr = null;

            switch (target.MonitorType)
            {
            case MonitorType.eHTTPGet:
                tlr = GetFromTarget(target);
                break;

            case MonitorType.eHTTPPost:
                tlr = PostToTarget(target);
                break;

            case MonitorType.ePing:
                tlr = PingTarget(target);
                break;

                // Default case: Throw an exception?
            }

            if (tlr != null)
            {
                MySqlConnection con = DatabaseConnectionProvider.GetMySqlConnection();

                tlr.Insert(con);
                target.UpdateLastTargetLogID(tlr.LogID, con);

                if (tlr.Status == TargetLogRecordStatus.Fail)
                {
                    // Send an e-mail to each of the target's account's contacts.
                    SendFailureNotificationEmails(target, tlr);
                }
            }

            //target.LastMonitoredAt = DateTime.UtcNow;
            target.UpdateLastMonitoredAt(DateTime.UtcNow, DatabaseConnectionProvider.GetMySqlConnection());
            target.DateTimeOfNextMonitor = target.LastMonitoredAt.Value + target.MonitorIntervalAsTimeSpan;   // or += target.MonitorIntervalAsTimeSpan;
            // Update the target's LastMonitoredAt in the database
            m_pqWaitingTargets.Enqueue(target);

            LogToConsole(string.Format(@"The target {0} has been dequeued, bumped, and re-enqueued.", target.Name));

            LogToConsole(@"**** Thread ending ****");
        }
 public SqlCircleGetCandidatesInteractor(DatabaseConnectionProvider provider, ICircleRepository circleRepository)
 {
     this.provider         = provider;
     this.circleRepository = circleRepository;
 }
Example #17
0
        public void TestPreValidateSqlExecutionNullStatement()
        {
            using (var connection = new DatabaseConnectionProvider().GetConnection())
            {
                var target = CreatePrivateAccessor();
                var actual = target.PreValidateSqlExecution(connection, null);
                Assert.IsFalse(actual);

                actual = target.PreValidateSqlExecutionTx(connection.BeginTransaction(), null);
                Assert.IsFalse(actual);
            }
        }