Example #1
0
 private async Task HandleMessage(UpdateIndexDto dto)
 {
     using (var db = new ApplicationDbContext())
     {
         Farm farm;
         switch (dto.UpdateMethod)
         {
             case EUpdateMethod.Create:
                 farm =
                     await db.Farms.Include(f => f.Products).Where(f => f.FarmId == dto.FarmId).FirstAsync();
                 //good idea but different time zone or system time on different servers do not allow to use a timestamp to filter out old messages
                 if (farm != null && farm.UpdateDateTime > farm.IndexDateTime) // && farm.UpdateDateTime == dto.UpdateTime) 
                 {
                     AddFarmToIndex(farm, _indexWriter, db);
                     await db.SaveChangesAsync();
                     _indexWriter.Commit();
                 }
                 break;
             case EUpdateMethod.Update:
                 farm =
                     await db.Farms.Include(f => f.Products).Where(f => f.FarmId == dto.FarmId).FirstAsync();
                 if (farm != null && farm.UpdateDateTime > farm.IndexDateTime) // && farm.UpdateDateTime == dto.UpdateTime)
                 {
                     UpdateFarmInIndex(farm, _indexWriter, db);
                     await db.SaveChangesAsync();
                     _indexWriter.Commit();
                 }
                 break;
             case EUpdateMethod.Delete:
                 farm = await db.Farms.FindAsync(dto.FarmId);
                 if (farm != null)
                 {
                     DeleteFarmFromIndex(farm, _indexWriter, db);
                     await db.SaveChangesAsync();
                     _indexWriter.Commit();
                 }
                 break;
         }
     }
 }
Example #2
0
        private async Task CreateIndex(ApplicationDbContext db)
        {
            var farms = await db.Farms.Include(f => f.Products).ToListAsync();
            Parallel.ForEach(farms, farm =>
            //foreach (var farm in farms)
            {
                try
                {
                    if (!farm.DeleteWhenRemovedFromIndex)
                    {
                        AddFarmToIndex(farm, _indexWriter, db);
                    }
                    else
                    {
                        //since it is not yet in the index we can simply remove it from the db
                        db.Farms.Remove(farm);
                    }
                }
                catch (Exception)
                {
                    //TODO error handling. Should certainly not abort the adddition of documents
                }
            });

            await db.SaveChangesAsync();
            _indexWriter.Optimize();
            _indexWriter.Commit();
        }
Example #3
0
        private async Task UpdateIndex(ApplicationDbContext db)
        {
            var farms = await GetNewAddedQueryable(db).ToListAsync();
            Parallel.ForEach(farms, farm =>
            {
                try
                {
                    AddFarmToIndex(farm, _indexWriter, db);
                }
                catch (Exception)
                {
                    //TODO error handling. Should certainly not abort the adddition of documents
                }
            });

            farms = await db.Farms.Include(f => f.Products).Where(f => f.UpdateDateTime > f.IndexDateTime && f.IndexDateTime != DateTime.MinValue).ToListAsync();
            Parallel.ForEach(farms, farm =>
            {
                try
                {
                    UpdateFarmInIndex(farm, _indexWriter, db);
                }
                catch (Exception)
                {
                    //TODO error handling. Should certainly not abort the adddition of documents
                }
            });

            farms = await db.Farms.Where(f => f.DeleteWhenRemovedFromIndex).ToListAsync();
            Parallel.ForEach(farms, farm =>
            {
                try
                {
                    DeleteFarmFromIndex(farm, _indexWriter, db);
                }
                catch (Exception)
                {
                    //TODO error handling. Should certainly not abort the adddition of documents
                }
            });

            await db.SaveChangesAsync();

            _indexWriter.Optimize();
            _indexWriter.Commit();
        }