Beispiel #1
0
        public void Run()
        {
            var index = findOrCreateIndex(Name);
            // find out if we should be resuming
            var resumeFrom = ResumeFrom ?? IndexMeta.GetByName(index)?.LastId;

            Console.WriteLine();
            Console.WriteLine($"{typeof(T)}, index `{index}`, chunkSize `{AppSettings.ChunkSize}`, resume `{resumeFrom}`");
            Console.WriteLine();

            var indexCompletedArgs = new IndexCompletedArgs
            {
                Alias     = Name,
                Index     = index,
                StartedAt = DateTime.Now
            };

            dispatcher = new BulkIndexingDispatcher <T>(Name, index);

            try
            {
                var readerTask = databaseReaderTask(resumeFrom);
                dispatcher.Run();
                readerTask.Wait();

                indexCompletedArgs.Count       = readerTask.Result;
                indexCompletedArgs.CompletedAt = DateTime.Now;

                updateAlias(Name, index);
                IndexCompleted(this, indexCompletedArgs);
            }
            catch (AggregateException ae)
            {
                ae.Handle(handleAggregateException);
            }

            // Local function exception handler.
            bool handleAggregateException(Exception ex)
            {
                if (!(ex is InvalidOperationException))
                {
                    return(false);
                }

                Console.WriteLine(ex.Message);
                return(true);
            }
        }
Beispiel #2
0
        public void Run()
        {
            if (!checkIfReady())
            {
                return;
            }

            var indexMeta = getIndexMeta();

            var metaSchema = AppSettings.IsPrepMode ? null : AppSettings.Schema;

            var indexCompletedArgs = new IndexCompletedArgs
            {
                Alias     = Name,
                Index     = indexMeta.Name,
                StartedAt = DateTime.Now
            };

            dispatcher = new BulkIndexingDispatcher <T>(indexMeta.Name);

            if (AppSettings.IsRebuild)
            {
                dispatcher.BatchWithLastIdCompleted += handleBatchWithLastIdCompleted;
            }

            try
            {
                var readerTask = databaseReaderTask(indexMeta.LastId);
                dispatcher.Run();
                readerTask.Wait();

                indexCompletedArgs.Count       = readerTask.Result;
                indexCompletedArgs.CompletedAt = DateTime.Now;

                IndexMeta.Refresh();

                // when preparing for schema changes, the alias update
                // should be done by process waiting for the ready signal.
                if (AppSettings.IsRebuild)
                {
                    if (AppSettings.IsPrepMode)
                    {
                        IndexMeta.MarkAsReady(indexMeta.Name);
                    }
                    else
                    {
                        updateAlias(Name, indexMeta.Name);
                    }
                }

                IndexCompleted(this, indexCompletedArgs);
            }
            catch (AggregateException ae)
            {
                ae.Handle(handleAggregateException);
            }

            // Local function exception handler.
            bool handleAggregateException(Exception ex)
            {
                if (!(ex is InvalidOperationException))
                {
                    return(false);
                }

                Console.Error.WriteLine(ex.Message);
                if (ex.InnerException != null)
                {
                    Console.Error.WriteLine(ex.InnerException.Message);
                }

                return(true);
            }

            void handleBatchWithLastIdCompleted(object sender, ulong lastId)
            {
                // TODO: should probably aggregate responses and update to highest successful.
                IndexMeta.UpdateAsync(new IndexMeta
                {
                    Name         = indexMeta.Name,
                    Alias        = Name,
                    LastId       = lastId,
                    ResetQueueTo = indexMeta.ResetQueueTo,
                    UpdatedAt    = DateTimeOffset.UtcNow,
                    Schema       = metaSchema
                });
            }
        }