コード例 #1
0
        public void Handle(ReclassifyErrors message)
        {
            if (Interlocked.Exchange(ref executing, 1) != 0)
            {
                // Prevent more then one execution at a time
                return;
            }

            try
            {
                using (var session = store.OpenSession())
                {
                    ReclassifyErrorSettings settings = null;

                    if (!message.Force)
                    {
                        settings = session.Load <ReclassifyErrorSettings>(ReclassifyErrorSettings.IdentifierCase);

                        if (settings != null && settings.ReclassificationDone)
                        {
                            logger.Info("Skipping reclassification of failures as classification has already been done.");
                            return;
                        }
                    }

                    logger.Info("Reclassification of failures started.");

                    var query = session.Query <FailedMessage, FailedMessageViewIndex>()
                                .Where(f => f.Status == FailedMessageStatus.Unresolved);

                    var currentBatch = new List <Tuple <string, ClassifiableMessageDetails> >();

                    using (var stream = session.Advanced.Stream(query.As <FailedMessage>()))
                    {
                        while (!abort && stream.MoveNext())
                        {
                            if (stream.Current.Document.FailureGroups.Count > 0)
                            {
                                continue;
                            }

                            currentBatch.Add(Tuple.Create(stream.Current.Document.Id, new ClassifiableMessageDetails(stream.Current.Document)));

                            if (currentBatch.Count == BatchSize)
                            {
                                ReclassifyBatch(currentBatch);
                                currentBatch.Clear();
                            }
                        }
                    }

                    if (currentBatch.Any())
                    {
                        ReclassifyBatch(currentBatch);
                    }

                    logger.Info("Reclassification of failures ended.");

                    if (settings == null)
                    {
                        settings = new ReclassifyErrorSettings();
                    }

                    settings.ReclassificationDone = true;
                    session.Store(settings);
                    session.SaveChanges();
                }

                if (failedMessagesReclassified > 0)
                {
                    bus.Publish(new ReclassificationOfErrorMessageComplete
                    {
                        NumberofMessageReclassified = failedMessagesReclassified
                    });
                }
            }
            finally
            {
                Interlocked.Exchange(ref executing, 0);
            }
        }
コード例 #2
0
        internal async Task <int> ReclassifyFailedMessages(IDocumentStore store, bool force, IEnumerable <IFailureClassifier> classifiers)
        {
            logger.Info("Reclassification of failures started.");

            var failedMessagesReclassified = 0;
            var currentBatch = new List <Tuple <string, ClassifiableMessageDetails> >();

            using (var session = store.OpenAsyncSession())
            {
                ReclassifyErrorSettings settings = null;

                if (!force)
                {
                    settings = await session.LoadAsync <ReclassifyErrorSettings>(ReclassifyErrorSettings.IdentifierCase)
                               .ConfigureAwait(false);

                    if (settings != null && settings.ReclassificationDone)
                    {
                        logger.Info("Skipping reclassification of failures as classification has already been done.");
                        return(0);
                    }
                }

                var query = session.Query <FailedMessage, FailedMessageViewIndex>()
                            .Where(f => f.Status == FailedMessageStatus.Unresolved);

                var totalMessagesReclassified = 0;

                using (var stream = await session.Advanced.StreamAsync(query.As <FailedMessage>())
                                    .ConfigureAwait(false))
                {
                    while (!abort && await stream.MoveNextAsync().ConfigureAwait(false))
                    {
                        currentBatch.Add(Tuple.Create(stream.Current.Document.Id, new ClassifiableMessageDetails(stream.Current.Document)));

                        if (currentBatch.Count == BatchSize)
                        {
                            failedMessagesReclassified += ReclassifyBatch(store, currentBatch, classifiers);
                            currentBatch.Clear();

                            totalMessagesReclassified += BatchSize;
                            logger.Info($"Reclassification of batch of {BatchSize} failed messages completed. Total messages reclassified: {totalMessagesReclassified}");
                        }
                    }
                }

                if (currentBatch.Any())
                {
                    ReclassifyBatch(store, currentBatch, classifiers);
                }

                logger.Info($"Reclassification of failures ended. Reclassified {failedMessagesReclassified} messages");

                if (settings == null)
                {
                    settings = new ReclassifyErrorSettings();
                }

                settings.ReclassificationDone = true;
                await session.StoreAsync(settings).ConfigureAwait(false);

                await session.SaveChangesAsync().ConfigureAwait(false);

                return(failedMessagesReclassified);
            }
        }