private static async Task InsertDocument(DocModel doc) { bool isSucceed = false; for (int i = 0; i < InsertRetries; i++) { try { await docStoreCollection.InsertOneAsync(doc); isSucceed = true; //Operation succeed just break the loop break; } catch (Exception ex) { if (!IsThrottled(ex)) { Console.WriteLine("ERROR: With collection {0}", ex.ToString()); throw; } else { // Thread will wait in between 1.5 secs and 3 secs. System.Threading.Thread.Sleep(new Random().Next(MinWait, MaxWait)); } } } if (!isSucceed) { InsertFailedDocs.Add(doc); } }
private static async Task ReadDocument(DocModel doc) { var builder = Builders <DocModel> .Filter; FilterDefinition <DocModel> filter = builder.Eq("_id", doc.Id); bool isSucceed = false; for (int i = 0; i < ReadRetries; i++) { try { await docStoreCollection.Find(filter).ForEachAsync <DocModel>(document => receivedDocuments.Add(document)); //Operation succeed just break the loop isSucceed = true; break; } catch (Exception ex) { if (!IsThrottled(ex)) { Console.WriteLine("ERROR: With collection {0}", ex.ToString()); throw; } else { // Thread will wait in between 1.5 secs and 3 secs. System.Threading.Thread.Sleep(new Random().Next(MinWait, MaxWait)); } } } if (!isSucceed) { ReadFailedDocs.Add(doc); } }
private static async Task GenerateSampleDocuments() { string JSONstr = File.ReadAllText("SampleDocumentToInsert.txt"); JArray ObjModel = (JArray)JsonConvert.DeserializeObject(JSONstr); for (int i = 0; i < DocsCount; i++) { DocModel dm = BsonSerializer.Deserialize <DocModel>(ObjModel[0].ToString()); dm.Id = Guid.NewGuid().ToString(); sampleDocuments.Add(dm); } }