protected virtual async Task ListenForMessages(CancellationToken cancellationToken) { await EnsureQueue(EndpointName).ConfigureAwait(false); await EnsureSubscriptions(EndpointName, cancellationToken).ConfigureAwait(false); await EnsureQueue(errorQueueName).ConfigureAwait(false); try { await Listen(cancellationToken); } catch (Exception ex) { logger.LogFatal($"Encountered fatal error. Error: {ex.Message}", ex); } }
public async Task RunAsync(CancellationToken cancellationToken) { try { logger.LogInfo($"PaymentsEventModelBatchService for {typeof(T).Name} started"); while (true) { cancellationToken.ThrowIfCancellationRequested(); try { await policy.ExecuteAsync(() => StorePayments(cancellationToken)); } catch (Exception ex) { logger.LogError($"Error while storing batch. Error: {ex.Message}", ex); } await Task.Delay(batchInterval, cancellationToken); } } catch (TaskCanceledException) { logger.LogInfo($"Cancellation requested, stopping PaymentsEventModelBatchService for {typeof(T).Name}"); } catch (Exception ex) { logger.LogFatal($"Fatal error while storing batch. Error: {ex.Message}", ex); } }
public async Task <IEndpointInstance> GetEndpointInstanceAsync() { try { if (endpointInstance != null) { return(endpointInstance); } logger.LogDebug($"Opening endpoint: {config.EndpointName}"); var endpointConfiguration = CreateEndpointConfiguration(); endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); logger.LogInfo($"Finished opening endpoint listener: {config.EndpointName}"); return(endpointInstance); } catch (Exception e) { logger.LogFatal($"Cannot start the endpoint: '{config.EndpointName}'. Error: {e.Message}", e); throw; } }
/// <summary> /// Opens the asynchronous. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public async Task <string> OpenAsync(CancellationToken cancellationToken) { try { logger.LogDebug($"Opening endpoint: {config.EndpointName}"); endpointInstance = await endpointInstanceFactory.GetEndpointInstance().ConfigureAwait(false); logger.LogInfo($"Finished opening endpoint listener: {config.EndpointName}"); return(config.EndpointName); } catch (Exception e) { logger.LogFatal($"Cannot start the endpoint: '{config.EndpointName}'. Error: {e.Message}", e); throw; } }
protected virtual async Task ListenForMessages(CancellationToken cancellationToken) { await EnsureQueue(EndpointName).ConfigureAwait(false); await EnsureQueue(errorQueueName).ConfigureAwait(false); try { //TODO: allow config to determine number of listeners await Task.WhenAll( Listen(cancellationToken), Listen(cancellationToken), Listen(cancellationToken) ); } catch (Exception ex) { logger.LogFatal($"Encountered fatal error. Error: {ex.Message}", ex); } }
/// <summary> /// Opens the asynchronous. /// </summary> /// <param name="cancellationToken">The cancellation token.</param> /// <returns></returns> public async Task <string> OpenAsync(CancellationToken cancellationToken) { try { logger.LogDebug($"Opening endpoint: {config.EndpointName}"); if (endpointInstance == null) { var endpointConfiguration = CreateEndpointConfiguration(); //endpointConfiguration.UseContainer<AutofacBuilder>(c => c.ExistingLifetimeScope(lifetimeScope)); endpointInstance = await Endpoint.Start(endpointConfiguration).ConfigureAwait(false); } logger.LogInfo($"Finished opening endpoint listener: {config.EndpointName}"); return("das endpoint comms listener"); } catch (Exception e) { logger.LogFatal($"Cannot start the endpoint: '{config.EndpointName}'. Error: {e.Message}", e); throw; } }
public void Fatal(string message) => logger.LogFatal(message);
public async Task <bool> HandleAsync(JobContextMessage message, CancellationToken cancellationToken) { logger.LogDebug($"Processing Earning Event Service event for Job Id : {message.JobId}"); try { if (await HandleSubmissionEvents(message)) { return(true); } using (var operation = telemetry.StartOperation($"FM36Processing:{message.JobId}")) { var stopwatch = Stopwatch.StartNew(); if (await jobStatusService.JobCurrentlyRunning(message.JobId)) { logger.LogWarning($"Job {message.JobId} is already running."); return(false); } var collectionPeriod = int.Parse(message.KeyValuePairs[JobContextMessageKey.ReturnPeriod].ToString()); var fileName = message.KeyValuePairs[JobContextMessageKey.Filename]?.ToString(); var fm36Output = await GetFm36Global(message, collectionPeriod, cancellationToken) .ConfigureAwait(false); if (fm36Output == null) { return(true); } cancellationToken.ThrowIfCancellationRequested(); await ClearSubmittedLearnerAims(collectionPeriod, fm36Output.Year, message.SubmissionDateTimeUtc, fm36Output.UKPRN, cancellationToken).ConfigureAwait(false); cancellationToken.ThrowIfCancellationRequested(); await ProcessFm36Global(message, collectionPeriod, fm36Output, fileName, cancellationToken) .ConfigureAwait(false); await SendReceivedEarningsEvent(message.JobId, message.SubmissionDateTimeUtc, fm36Output.Year, collectionPeriod, fm36Output.UKPRN).ConfigureAwait(false); stopwatch.Stop(); var duration = stopwatch.ElapsedMilliseconds; telemetry.TrackEvent("Processed ILR Submission", new Dictionary <string, string> { { TelemetryKeys.Count, fm36Output.Learners.Count.ToString() }, { TelemetryKeys.CollectionPeriod, collectionPeriod.ToString() }, { TelemetryKeys.AcademicYear, fm36Output.Year }, { TelemetryKeys.JobId, message.JobId.ToString() }, { TelemetryKeys.Ukprn, fm36Output.UKPRN.ToString() }, }, new Dictionary <string, double> { { TelemetryKeys.Duration, duration }, { TelemetryKeys.Count, fm36Output.Learners.Count }, }); if (cancellationToken.IsCancellationRequested) { logger.LogError($"Job {message.JobId} has been cancelled after job has started processing. Ukprn: {fm36Output.UKPRN}"); return(false); } telemetry.StopOperation(operation); if (fm36Output.Learners.Count == 0) { logger.LogWarning($"Received ILR with 0 FM36 learners. Ukprn: {fm36Output.UKPRN}, job id: {message.JobId}."); return(true); } if (await jobStatusService.WaitForJobToFinish(message.JobId, cancellationToken)) { logger.LogInfo($"Successfully processed ILR Submission. Job Id: {message.JobId}, Ukprn: {fm36Output.UKPRN}, Submission Time: {message.SubmissionDateTimeUtc}"); return(true); } logger.LogError($"Job failed to finished within the allocated time. Job Id: {message.JobId}"); return(false); } } catch (OperationCanceledException) { logger.LogError($"Cancellation token cancelled for job: {message.JobId}"); return(false); } catch (Exception ex) { logger.LogFatal($"Error while handling EarningService event. Error: {ex.Message}", ex); return(false); //TODO: change back to throw when DC code is a little more defensive } }