public async Task RunAsync(CancellationToken cancellation) { cancellation.ThrowIfCancellationRequested(); var meterReport = await _meterService .GetReportAsync(cancellation) .ConfigureAwait(false); if (!meterReport.IsSuccessful) { // TODO: log } var facts = _domainState.GetFacts(); var monitors = await _monitorRepository .GetAllAsync(cancellation) .ConfigureAwait(false); if (monitors != null) { foreach (var monitor in monitors.Where(m => _monitorValidator.Validate(m))) { cancellation.ThrowIfCancellationRequested(); if (!monitor.IsTriggered(facts)) { continue; } await monitor.WriteAsync(cancellation).ConfigureAwait(false); } } var machines = await _machineRepository .GetAllAsync(cancellation) .ConfigureAwait(false); if (machines != null) { foreach (var machine in machines.Where(m => _machineValidator.Validate(m))) { cancellation.ThrowIfCancellationRequested(); await machine.WorkAsync(facts, cancellation).ConfigureAwait(false); } } }