protected override bool HandleWork() { var currentTime = ExpirationReadTrigger.GetCurrentUtcDate(); var nowAsStr = DateTools.DateToString(currentTime, DateTools.Resolution.SECOND); int retries = 0; QueryResult queryResult; do { queryResult = Database.Query(RavenDocumentsByExpirationDate, new IndexQuery { Cutoff = currentTime, Query = "Expiry:[* TO " + nowAsStr + "]", FieldsToFetch = new [] { "__document_id" } }); if (queryResult.IsStale) { if (++retries > 25) { return(false); } Thread.Sleep(100); } } while (queryResult.IsStale); foreach (var result in queryResult.Results) { var docId = result.Value <string>("__document_id"); Database.Delete(docId, null, null); } return(queryResult.Results.Count > 0); }
private void TimerCallback(object state) { if (executing) { return; } executing = true; try { var currentTime = ExpirationReadTrigger.GetCurrentUtcDate(); var nowAsStr = DateTools.DateToString(currentTime, DateTools.Resolution.SECOND); while (true) { var queryResult = Database.Query(RavenDocumentsByExpirationDate, new IndexQuery { PageSize = 1024, Cutoff = currentTime, Query = "Expiry:[* TO " + nowAsStr + "]", FieldsToFetch = new[] { "__document_id" } }); if (queryResult.IsStale) { Thread.Sleep(100); continue; } if (queryResult.Results.Count == 0) { return; } var docIds = queryResult.Results.Select(result => result.Value <string>("__document_id")).ToArray(); logger.Debug(() => string.Format("Deleting {0} expired documents: [{1}]", queryResult.Results.Count, string.Join(", ", docIds))); Database.TransactionalStorage.Batch(accessor => // delete all expired items in a single tx { foreach (var docId in docIds) { Database.Delete(docId, null, null); } }); } } catch (Exception e) { logger.ErrorException("Error when trying to find expired documents", e); } finally { executing = false; } }
private static void UpdateTimes(out string nowAsStr, out DateTime currentTime) { currentTime = ExpirationReadTrigger.GetCurrentUtcDate(); nowAsStr = DateTools.DateToString(currentTime, DateTools.Resolution.SECOND); }
private void TimerCallback(object state) { if (executing) { return; } executing = true; try { DateTime currentTime = ExpirationReadTrigger.GetCurrentUtcDate(); string nowAsStr = currentTime.ToString(Default.DateTimeFormatsToWrite); logger.Debug("Trying to find expired documents to delete"); var query = "Expiry:[* TO " + nowAsStr + "]"; var list = new List <string>(); int start = 0; while (true) { const int pageSize = 1024; var queryResult = Database.Query(RavenDocumentsByExpirationDate, new IndexQuery { Start = start, PageSize = pageSize, Cutoff = currentTime, Query = query, FieldsToFetch = new[] { "__document_id" } }); if (queryResult.Results.Count == 0) { break; } start += pageSize; list.AddRange(queryResult.Results.Select(result => result.Value <string>("__document_id")).Where(x => string.IsNullOrEmpty(x) == false)); } if (list.Count == 0) { return; } logger.Debug( () => string.Format("Deleting {0} expired documents: [{1}]", list.Count, string.Join(", ", list))); foreach (var id in list) { Database.Delete(id, null, null); } } catch (Exception e) { logger.ErrorException("Error when trying to find expired documents", e); } finally { executing = false; } }