private PeriodicExportRunner(DocumentDatabase database, PeriodicExportConfiguration configuration, PeriodicExportStatus status) { _database = database; _configuration = configuration; _status = status; _logger = LoggingSource.Instance.GetLogger <PeriodicExportRunner>(_database.Name); _cancellationToken = new CancellationTokenSource(); if (configuration.IntervalMilliseconds.HasValue && configuration.IntervalMilliseconds.Value > 0) { _incrementalIntermediateInterval = IncrementalInterval = TimeSpan.FromMilliseconds(configuration.IntervalMilliseconds.Value); if (_logger.IsInfoEnabled) { _logger.Info($"Incremental periodic export started, will export every {IncrementalInterval.TotalMinutes} minutes"); } if (IsValidTimespanForTimer(IncrementalInterval)) { var timeSinceLastExport = SystemTime.UtcNow - _status.LastExportAt; var nextExport = timeSinceLastExport >= IncrementalInterval ? TimeSpan.Zero : IncrementalInterval - timeSinceLastExport; _incrementalExportTimer = new Timer(TimerCallback, false, nextExport, IncrementalInterval); } else { _incrementalExportTimer = new Timer(LongPeriodTimerCallback, false, MaxTimerTimeout, Timeout.InfiniteTimeSpan); } } else { if (_logger.IsInfoEnabled) { _logger.Info("Incremental periodic export interval is set to zero or less, incremental periodic export is now disabled"); } } if (configuration.FullExportIntervalMilliseconds.HasValue && configuration.FullExportIntervalMilliseconds.Value > 0) { _fullExportIntermediateInterval = FullExportInterval = TimeSpan.FromMilliseconds(configuration.FullExportIntervalMilliseconds.Value); if (_logger.IsInfoEnabled) { _logger.Info("Full periodic export started, will export every" + FullExportInterval.TotalMinutes + "minutes"); } if (IsValidTimespanForTimer(FullExportInterval)) { var timeSinceLastExport = SystemTime.UtcNow - _status.LastFullExportAt; var nextExport = timeSinceLastExport >= FullExportInterval ? TimeSpan.Zero : FullExportInterval - timeSinceLastExport; _fullExportTimer = new Timer(TimerCallback, true, nextExport, FullExportInterval); } else { _fullExportTimer = new Timer(LongPeriodTimerCallback, true, MaxTimerTimeout, Timeout.InfiniteTimeSpan); } } else { if (_logger.IsInfoEnabled) { _logger.Info("Full periodic export interval is set to zero or less, full periodic export is now disabled"); } } }
public static PeriodicExportRunner LoadConfigurations(DocumentDatabase database) { DocumentsOperationContext context; using (database.DocumentsStorage.ContextPool.AllocateOperationContext(out context)) { context.OpenReadTransaction(); var configuration = database.DocumentsStorage.Get(context, Constants.PeriodicExport.ConfigurationDocumentKey); if (configuration == null) { return(null); } try { var periodicExportConfiguration = JsonDeserializationServer.PeriodicExportConfiguration(configuration.Data); if (periodicExportConfiguration.Active == false) { if (_logger.IsInfoEnabled) { _logger.Info("Periodic export is disabled."); } return(null); } var status = database.DocumentsStorage.Get(context, Constants.PeriodicExport.StatusDocumentKey); PeriodicExportStatus periodicExportStatus = null; if (status != null) { try { periodicExportStatus = JsonDeserializationServer.PeriodicExportStatus(status.Data); } catch (Exception e) { if (_logger.IsInfoEnabled) { _logger.Info($"Unable to read the periodic export status as the status document {Constants.PeriodicExport.StatusDocumentKey} is not valid. We will start to export from scratch. Data: {configuration.Data}", e); } } } return(new PeriodicExportRunner(database, periodicExportConfiguration, periodicExportStatus ?? new PeriodicExportStatus())); } catch (Exception e) { //TODO: Raise alert, or maybe handle this via a db load error that can be turned off with //TODO: a config if (_logger.IsInfoEnabled) { _logger.Info($"Cannot enable periodic export as the configuration document {Constants.PeriodicExport.ConfigurationDocumentKey} is not valid: {configuration.Data}", e); } /* * Database.AddAlert(new Alert * { * AlertLevel = AlertLevel.Error, * CreatedAt = SystemTime.UtcNow, * Message = ex.Message, * Title = "Could not read periodic export config", * Exception = ex.ToString(), * UniqueKey = "Periodic Export Config Error" * });*/ return(null); } } }