/// <inheritdoc /> public async Task <long> WorkAsync(CancellationToken token) { // check if we are active at the current time. if (!_active.IsActive()) { // we are not active ... so we have nothing to do. _logger.Verbose("Maintenance Process ignored, out of active hours."); return(0); } var stopwatch = new Stopwatch(); stopwatch.Start(); var success = false; try { _logger.Information("Started Maintenance Process."); await _persister.MaintenanceAsync(token).ConfigureAwait(false); await _parser.MaintenanceAsync(token).ConfigureAwait(false); // it worked success = true; } catch (OperationCanceledException) { // nothing to log throw; } catch (Exception e) { _logger.Exception("Error while processing Maintenance.", e); throw; } finally { _logger.Information(success ? $"Complete Maintenance Process (Time Elapsed: {stopwatch.Elapsed:g})." : $"Complete Maintenance Process with errors (Time Elapsed: {stopwatch.Elapsed:g})."); } return(0); }
/// <summary> /// Make sure that we have a valid category. /// </summary> /// <param name="counter"></param> /// <param name="logger"></param> public void Initialise(Counter counter, ILogger logger) { // we will need to re-create all the counters... DisposeAllCounters(); // enter the lock so we can create the counter. lock (Lock) { // build the collection data var counterCreationDataCollection = BuildCollectionData(counter); // if the category does not exist, try and create it. if (!PerformanceCounterCategory.Exists(counter.CategoryName)) { PerformanceCounterCategory.Create(counter.CategoryName, counter.CategoryHelp, PerformanceCounterCategoryType.SingleInstance, counterCreationDataCollection); logger.Information($"Created performance category: {counter.CategoryName} for counter: {counter.Name}"); return; } if (PerformanceCounterCategory.CounterExists(counter.Name, counter.CategoryName)) { return; } var category = PerformanceCounterCategory.GetCategories() .First(cat => cat.CategoryName == counter.CategoryName); var counters = category.GetCounters(); foreach (var c in counters) { counterCreationDataCollection.Add(new CounterCreationData(c.CounterName, c.CounterHelp, c.CounterType)); } PerformanceCounterCategory.Delete(counter.CategoryName); PerformanceCounterCategory.Create(counter.CategoryName, counter.CategoryHelp, PerformanceCounterCategoryType.SingleInstance, counterCreationDataCollection); logger.Information($"Updated performance category: {counter.CategoryName} for counter: {counter.Name}"); } }
/// <inheritdoc /> public async Task MaintenanceAsync(CancellationToken token) { lock (_taskLock) { // are we still running the start? if (!_runningTask?.IsCompleted ?? true) { return; } } var factory = await _persister.BeginWrite(token).ConfigureAwait(false); try { const string configParse = "maintenance.parse"; var lastParse = await _persister.Config.GetConfigValueAsync(configParse, DateTime.MinValue, factory, token).ConfigureAwait(false); // between 3 and 5 hours to prevent running at the same time as others. var randomHours = (new Random(DateTime.UtcNow.Millisecond)).Next(3, 5); if ((DateTime.UtcNow - lastParse).TotalHours > randomHours) { // // Do maintenance work // _logger.Information("Maintenance files/folders parser"); // save the date and commit ... because the parser needs its own factory. await _persister.Config.SetConfigValueAsync(configParse, DateTime.UtcNow, factory, token).ConfigureAwait(false); } _persister.Commit(factory); } catch (Exception e) { _persister.Rollback(factory); _logger.Exception("There was an exception re-parsing the directories.", e); throw; } }