public async Task ProcessStreamAsync(Stream data, string templateDefinition, ILogger log) { EnsureArg.IsNotNull(templateDefinition, nameof(templateDefinition)); EnsureArg.IsNotNull(log, nameof(log)); var template = Options.TemplateFactory.Create(templateDefinition); var measurementGroups = await ParseAsync(data, log).ConfigureAwait(false); // Group work by device to avoid race conditions when resource creation is enabled. var workItems = measurementGroups.GroupBy(grp => grp.DeviceId) .Select(grp => new Func <Task>( async() => { foreach (var m in grp) { try { await _fhirImportService.ProcessAsync(template, m).ConfigureAwait(false); } catch (Exception ex) { if (!Options.ExceptionService.HandleException(ex, log)) { log.LogError(ex, ex.Message); throw; } } } })); await StartWorker(workItems).ConfigureAwait(false); }
private async Task ProcessMeasurementGroups(IEnumerable <IMeasurementGroup> measurementGroups, ILookupTemplate <IFhirTemplate> template, ITelemetryLogger log) { // Group work by device to avoid race conditions when resource creation is enabled. var workItems = measurementGroups.GroupBy(grp => grp.DeviceId) .Select(grp => new Func <Task>( async() => { foreach (var m in grp) { try { await _fhirImportService.ProcessAsync(template, m).ConfigureAwait(false); } catch (Exception ex) { if (!Options.ExceptionService.HandleException(ex, log)) { throw; } } } })); await StartWorker(workItems).ConfigureAwait(false); }