public async Task ProccessCancelsHandleEventsThrows() { var eventList = Enumerable.Range(0, 1).Select((x, i) => new EventData(new byte[100]).Tap(e => { e.SystemProperties = new EventData.SystemPropertiesCollection(i, DateTime.UtcNow, "45000", "test"); })); var cts = new CancellationTokenSource(); var tc = new TelemetryClient(); Func <IReadOnlyCollection <EventData>, CancellationToken, Task> handleEvents = (events, token) => { throw new Exception("test"); }; Func <EventData, Task> checkpoint = ed => Task.CompletedTask; await Assert.ThrowsAsync <Exception>(async() => { await Execution.ExecuteAsync(cts.Token, new TestLogger(), (s, o) => { }, "test", "0", ct => eventList.ProcessAsync(ct, (events, f) => tc.UseOperationLogging()(events, "test", f), Guid.NewGuid().ToString(), checkpoint, handleEvents, (s, o) => { }, (s, o) => { }, (e, s, o) => { }, 0 )); }); }
public async Task ProccessCheckpointThrows() { var eventList = Enumerable.Range(0, 1).Select(x => new EventData(new byte[100])); var cts = new CancellationTokenSource(); var tc = new TelemetryClient(); Func <IReadOnlyCollection <EventData>, CancellationToken, Task> handleEvents = (events, token) => Task.CompletedTask; Func <EventData, Task> checkpoint = ed => { throw new FabricNotPrimaryException(); }; await Assert.ThrowsAsync <FabricNotPrimaryException>(async() => { await Execution.ExecuteAsync(cts.Token, new TestLogger(), (s, o) => { }, "test", "0", ct => eventList.ProcessAsync(ct, (events, f) => tc.UseOperationLogging()(events, "test", f), Guid.NewGuid().ToString(), checkpoint, handleEvents, (s, o) => { }, (s, o) => { }, (e, s, o) => { } )); }); }
protected override async Task RunAsync(CancellationToken cancellationToken) { try { await Execution.ExecuteAsync(cancellationToken, _logger, _serviceEventSource, nameof(ReceiverService), Context.PartitionId.ToString(), _switch); await _host.RunAsync(_logger, _options, cancellationToken, _serviceEventSource, Context.PartitionId.ToString(), _f); } catch (FabricTransientException e) { _logger.LogError(e, nameof(ReceiverService) + "Exception .RunAsync for {PartitionId}", Context.PartitionId); } }
protected override async Task RunAsync(CancellationToken cancellationToken) { cancellationToken.Register(() => _logger.LogInformation($"{nameof(RunAsync)} is being cancelled")); try { await Execution .ExecuteAsync(cancellationToken, _logger, _serviceEventSource, nameof(ReceiverService), Context.PartitionId.ToString(), async ct => { await _switch(cancellationToken); cancellationToken.ThrowIfCancellationRequested(); var options = new EventProcessorOptions { OnShutdown = OnShutdown, MaxBatchSize = MaxMessageCount, PrefetchCount = MaxMessageCount, InitialPositionProvider = s => { _logger.LogInformation("Using InitialPositionProvider for {s}", s); return(_initialPositionProvider(s)); } }; _logger.LogInformation("Create ServiceFabricProcessor with {ConsumerGroup}", _options.ConsumerGroup); var processorService = new ServiceFabricProcessor( Context.ServiceName, Context.PartitionId, StateManager, Partition, CreateProcessor(_options, _telemetryClient, _logger, _serviceEventSource, _eventHandlerCreator), _options.ConnectionString, _options.ConsumerGroup, options); await processorService.RunAsync(cancellationToken); }); } catch (FabricTransientException e) { _logger.LogError(e, nameof(ReceiverService) + "Exception .RunAsync for {PartitionId}", Context.PartitionId); } }
public async Task RunAsync(CancellationToken token) { if (debug) { AttachDebugger(); } if (api != null && string.IsNullOrWhiteSpace(config.RunlyApi.OrganizationId)) { var message = "OrganizationId is required when an API token is provided."; logger.LogError(message); throw new ConfigException(message, nameof(Config.RunlyApi)); } if (config.RunlyApi.InstanceId != Guid.Empty && (api == null || string.IsNullOrWhiteSpace(config.RunlyApi.OrganizationId))) { var message = "An API token and OrganizationId are required when InstanceId is specified."; logger.LogError(message); throw new ConfigException(message, nameof(Config.RunlyApi)); } var jobCancellation = CancellationTokenSource.CreateLinkedTokenSource(token); var pingCancellation = new CancellationTokenSource(); var useApi = config.RunlyApi.InstanceId != Guid.Empty; ResultsChannel.Connection channel = null; ResultLog log = null; try { if (useApi) { channel = await api.ConnectAsync(config.RunlyApi.InstanceId); channel.CancellationRequested += () => { jobCancellation.Cancel(); return(Task.CompletedTask); }; execution.StateChanged += s => UpdateInstanceState(channel); execution.StateChanged += s => { if (s == ExecutionState.Processing && execution.TotalItemCount.HasValue) { return(channel.SetTotal(execution.TotalItemCount.Value)); } else if (s == ExecutionState.Finalizing) { return(channel.MethodResult(new MethodOutcome(JobMethod.ProcessAsync, processDuration, (Error)null))); } else { return(Task.CompletedTask); } }; execution.MethodCompleted += m => channel.MethodResult(m); execution.ItemCompleted += i => { processDuration += TimeSpan.FromMilliseconds(i.Methods.Sum(m => m.Value.Duration.TotalMilliseconds)); if (config.RunlyApi.LogSuccessfulItemResults || !i.IsSuccessful) { return(channel.ItemResult(i)); } else { return(Task.CompletedTask); } }; execution.Completed += (output, disposition, completedAt) => channel.MarkComplete(disposition, output, execution.ItemCategories.Select(c => new ItemProgress { Category = c.Category, IsSuccessful = c.IsSuccessful, Count = c.Count })); } if (config.Execution.ResultsToConsole || config.Execution.ResultsToFile) { log = new ResultLog(execution); } var executing = execution.ExecuteAsync(jobCancellation.Token); var pinging = Task.CompletedTask; if (useApi) { pinging = PingApi(channel, pingCancellation.Token); } while (!executing.IsCompleted && config.Execution.ResultsToConsole) { Console.Write($"\rRunning {execution.Job.GetType().Name}: {execution.CompletedItemCount} items{(execution.TotalItemCount.HasValue ? " of " + execution.TotalItemCount.Value : string.Empty)} processed. {(!jobCancellation.IsCancellationRequested ? "Press 'q' to quit." : "Quitting... ")}"); while (!jobCancellation.IsCancellationRequested && Console.KeyAvailable) { var key = Console.ReadKey(); if (key.KeyChar == 'q' || key.KeyChar == 'Q') { jobCancellation.Cancel(); } } await Task.Delay(100); } // Execution.RunAsync will ensure all event handlers have completed before exiting await executing; pingCancellation.Cancel(); if (config.Execution.ResultsToConsole) { Console.Write("\r" + new string(' ', 80)); Console.WriteLine(); Console.WriteLine(log); } if (config.Execution.ResultsToFile) { using var writer = new StreamWriter(File.Open(config.Execution.ResultsFilePath, FileMode.Create)); writer.WriteJson(log); await writer.FlushAsync(); } try { await pinging; } catch (TaskCanceledException) { } if (channel != null) { await channel.FlushAsync(); } } finally { if (channel != null) { await channel.DisposeAsync(); } } // Ensure the entire output can be read by the node Console.WriteLine(); await Console.Out.FlushAsync(); }