public void Start()
 {
     Metric.Gauge("checkpoint-behind-AtomicReadModels", CheckAllAtomicsValue(), Unit.Items);
     HealthChecks.RegisterHealthCheck("Slot-All-AtomicReadmodels", CheckSlotHealth());
     foreach (var readModelType in _atomicProjectionCheckpointManager.GetAllRegisteredAtomicReadModels())
     {
         var name = CollectionNames.GetCollectionName(readModelType);
         //try to create an instance to grab the version
         try
         {
             var readmodelVersion = _readModelFactory.GetReamdodelVersion(readModelType);
             Metric.Gauge("versions-behind-" + name, () => _versionLoader.CountReadModelToUpdateByName(name, readmodelVersion), Unit.Items);
         }
         catch (Exception ex)
         {
             Logger.ErrorFormat(ex, "Unable to setup metric for atomic readmodel {0}", readModelType.FullName);
         }
     }
 }
        public AtomicMongoCollectionWrapper(
            IMongoDatabase readmodelDb,
            IAtomicReadModelFactory atomicReadModelFactory,
            ILiveAtomicReadModelProcessor liveAtomicReadModelProcessor)
        {
            _liveAtomicReadModelProcessor = liveAtomicReadModelProcessor;

            var collectionName = CollectionNames.GetCollectionName <TModel>();

            _collection = readmodelDb.GetCollection <TModel>(collectionName);

            Logger = NullLogger.Instance;

            //Auto create the basic index you need
            _collection.Indexes.CreateOne(
                new CreateIndexModel <TModel>(
                    Builders <TModel> .IndexKeys
                    .Ascending(_ => _.ProjectedPosition),
                    new CreateIndexOptions()
            {
                Name       = "ProjectedPosition",
                Background = false
            }
                    )
                );

            _collection.Indexes.CreateOne(
                new CreateIndexModel <TModel>(
                    Builders <TModel> .IndexKeys
                    .Ascending(_ => _.ReadModelVersion),
                    new CreateIndexOptions()
            {
                Name       = "ReadModelVersion",
                Background = false
            }
                    )
                );

            _actualVersion = atomicReadModelFactory.GetReamdodelVersion(typeof(TModel));
        }
            private async Task InnerFix()
            {
                Int64 fixCheckpoint = 0;
                var   actualVersion = _atomicReadModelFactory.GetReamdodelVersion(typeof(T));
                Int32 count         = 0;

                //cycle until exit point.
                while (true)
                {
                    try
                    {
                        //Sorting for version we will start fixing readmodel never re-fixing
                        var blockList = _collection
                                        .AsQueryable()
                                        .Where(_ => _.ReadModelVersion <actualVersion &&
                                                                        _.ProjectedPosition> fixCheckpoint)
                                        .OrderBy(_ => _.ProjectedPosition)
                                        .Take(100)
                                        .Select(_ => new
                        {
                            _.Id,
                            _.ProjectedPosition
                        })
                                        .ToList();
                        if (blockList.Count == 0)
                        {
                            _logger.InfoFormat("Finished fixing {0} - Fixed {1} readmodels", typeof(T), count);
                            return;
                        }
                        foreach (var elementToFix in blockList)
                        {
                            if (!_started)
                            {
                                return;
                            }

                            fixCheckpoint = elementToFix.ProjectedPosition;
                            count++;
                            _logger.DebugFormat("Fixing readmodel {0}", elementToFix.Id);
                            T fixedRm = default(T);
                            try
                            {
                                fixedRm = await _liveAtomicReadModelProcessor.ProcessAsync <T>(elementToFix.Id, Int32.MaxValue).ConfigureAwait(false);
                            }
                            catch (Exception ex)
                            {
                                _logger.ErrorFormat(ex, "Error during polling for fixer of readmodel {0}/{1} Last Position {2} - {3}", typeof(T), elementToFix.Id, fixedRm?.ProjectedPosition, ex.Message);
                                fixedRm?.MarkAsFaulted(fixedRm.ProjectedPosition);
                            }
                            if (fixedRm != null)
                            {
                                await _collection.UpdateAsync(fixedRm).ConfigureAwait(false);
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        _logger.ErrorFormat(ex, "Error during polling for fixer of readmodel {0} - {1}", typeof(T), ex.Message);
                    }
                }
            }