public static void Clean(int deletionBatchSize, DocumentDatabase database, DateTime expiryThreshold) { var stopwatch = Stopwatch.StartNew(); var items = new List <ICommandData>(deletionBatchSize); try { var query = new IndexQuery { Start = 0, DisableCaching = true, Cutoff = SystemTime.UtcNow, PageSize = deletionBatchSize, Query = $"LastModified:[* TO {expiryThreshold.Ticks}]", FieldsToFetch = new[] { "__document_id", }, SortedFields = new[] { new SortedField("LastModified") { Field = "LastModified", Descending = false } } }; var indexName = new ExpirySagaAuditIndex().IndexName; database.Query(indexName, query, database.WorkContext.CancellationToken, doc => { var id = doc.Value <string>("__document_id"); if (string.IsNullOrEmpty(id)) { return; } items.Add(new DeleteCommandData { Key = id }); }); } catch (OperationCanceledException) { //Ignore } var deletionCount = 0; Chunker.ExecuteInChunks(items.Count, (s, e) => { logger.InfoFormat("Batching deletion of {0}-{1} sagahistory documents.", s, e); var results = database.Batch(items.GetRange(s, e - s + 1), CancellationToken.None); logger.InfoFormat("Batching deletion of {0}-{1} sagahistory documents completed.", s, e); deletionCount += results.Count(x => x.Deleted == true); }); if (deletionCount == 0) { logger.Info("No expired sagahistory documents found"); } else { logger.InfoFormat("Deleted {0} expired sagahistory documents. Batch execution took {1}ms", deletionCount, stopwatch.ElapsedMilliseconds); } }
public static void Clean(int deletionBatchSize, DocumentDatabase database, DateTime expiryThreshold, CancellationToken token) { var stopwatch = Stopwatch.StartNew(); var items = new List <ICommandData>(deletionBatchSize); try { var query = new IndexQuery { Start = 0, DisableCaching = true, Cutoff = SystemTime.UtcNow, PageSize = deletionBatchSize, Query = $"LastModified:[* TO {expiryThreshold.Ticks}]", FieldsToFetch = new[] { "__document_id" }, SortedFields = new[] { new SortedField("LastModified") { Field = "LastModified", Descending = false } } }; var indexName = new ExpirySagaAuditIndex().IndexName; database.Query(indexName, query, token, (doc, commands) => { var id = doc.Value <string>("__document_id"); if (string.IsNullOrEmpty(id)) { return; } commands.Add(new DeleteCommandData { Key = id }); }, items); } catch (OperationCanceledException) { logger.Info("Cleanup operation cancelled"); return; } if (token.IsCancellationRequested) { return; } var deletionCount = Chunker.ExecuteInChunks(items.Count, (itemsForBatch, db, s, e) => { if (logger.IsDebugEnabled) { logger.Debug($"Batching deletion of {s}-{e} saga history documents."); } var results = db.Batch(itemsForBatch.GetRange(s, e - s + 1), CancellationToken.None); if (logger.IsDebugEnabled) { logger.Debug($"Batching deletion of {s}-{e} saga history documents completed."); } return(results.Count(x => x.Deleted == true)); }, items, database, token); if (deletionCount == 0) { logger.Info("No expired saga history documents found"); } else { logger.Info($"Deleted {deletionCount} expired saga history documents. Batch execution took {stopwatch.ElapsedMilliseconds} ms"); } }