Ejemplo n.º 1
0
        /// <summary>
        /// This timer will crawl through the data and find any item with no hash
        /// It will then then select records to activate the hash generation routine
        /// </summary>
        private static void _timerCheck_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            //If the system is turned off then do nothing
            if (!ConfigHelper.EnabledDataManager)
            {
                return;
            }
            if (!IsActive)
            {
                return;
            }
            if (!EnableHouseKeeping)
            {
                return;
            }

            _timerCheck.Enabled = false;
            try
            {
                const int BlockCount = 50;
                const int WaitTime   = 250;

                var core = ((SystemCore)RepositoryManager.SystemCore);
                while (_highPriority.Any())
                {
                    //Try to get an ID from the high priority list
                    //if none found then grab an arbitrary one from the global list
                    Guid ID = Guid.Empty;
                    if (!_highPriority.TryTake(out ID))
                    {
                        return;
                    }
                    if (ID == Guid.Empty)
                    {
                        return;
                    }

                    //If there was an error it is in the skip list so never handle this ID again
                    if (!_skipList.Any(x => x == ID))
                    {
                        var sb     = new StringBuilder();
                        var schema = RepositoryManager.GetSchema(ID);
                        if (schema != null)
                        {
                            DataQueryResults results = new DataQueryResults();
                            do
                            {
                                if (!IsActive)
                                {
                                    return;            //Stop when housekeeping comes on
                                }
                                var query = new DataQuery {
                                    IncludeRecords = true, IncludeDimensions = false, ExcludeCount = true
                                };
                                query.FieldFilters.Add(new FieldFilter {
                                    Name = SqlHelper.HashField, Comparer = ComparisonConstants.Equals, Value = 0
                                });
                                query.RecordsPerPage = BlockCount;
                                try
                                {
                                    results = core.Manager.Query(ID, query, true);
                                    Interlocked.Add(ref _counter, results.RecordList.Count);
                                }
                                catch
                                {
                                    results = new DataQueryResults();
                                }

                                //Do not overload the system with background queries
                                if (results.ComputeTime > 2000)
                                {
                                    System.Threading.Thread.Sleep(WaitTime * 2);
                                }
                                else if (core.GetCpu() > 60)
                                {
                                    System.Threading.Thread.Sleep(WaitTime * 8);
                                }
                                else
                                {
                                    System.Threading.Thread.Sleep(WaitTime);
                                }
                            } while (results.RecordList?.Count == BlockCount && results.ComputeTime < 5000);
                        }
                    }
                }

                if (InFullIndex)
                {
                    SystemCore.PatchApply(FullIndexPatch, "DataManager:FullIndex");
                }
            }
            catch (Exception ex)
            {
                LoggerCQ.LogError(ex);
            }
            finally
            {
                _timerCheck.Enabled = true;
            }
        }