public async Task TriggerAuditDataCleanup()
        {
            IEnumerable <SubmissionJobsToBeDeletedBatch> previousSubmissionJobsToBeDeletedBatches = new List <SubmissionJobsToBeDeletedBatch>();

            if (!string.IsNullOrWhiteSpace(config.PreviousAcademicYearCollectionPeriod) && !string.IsNullOrWhiteSpace(config.PreviousAcademicYear))
            {
                previousSubmissionJobsToBeDeletedBatches = await GetSubmissionJobsToBeDeletedBatches(config.PreviousAcademicYearCollectionPeriod, config.PreviousAcademicYear);
            }

            var currentSubmissionJobsToBeDeletedBatches = await GetSubmissionJobsToBeDeletedBatches(config.CurrentCollectionPeriod, config.CurrentAcademicYear);

            var submissionJobsToBeDeletedBatches     = previousSubmissionJobsToBeDeletedBatches.Union(currentSubmissionJobsToBeDeletedBatches);
            var submissionJobsToBeDeletedBatchesList = submissionJobsToBeDeletedBatches.ToList();

            var endpointInstance = await endpointInstanceFactory.GetEndpointInstance().ConfigureAwait(false);

            paymentLogger.LogInfo($"Triggering Audit Data Cleanup for {submissionJobsToBeDeletedBatchesList.Count} submission job batches. " +
                                  $"DCJobIds: {string.Join(",", submissionJobsToBeDeletedBatchesList.SelectMany(x => x.JobsToBeDeleted.Select(y => y.DcJobId)))}");

            foreach (var batch in submissionJobsToBeDeletedBatchesList)
            {
                await endpointInstance.Send(config.EarningAuditDataCleanUpQueue, batch).ConfigureAwait(false);

                await endpointInstance.Send(config.DataLockAuditDataCleanUpQueue, batch).ConfigureAwait(false);

                await endpointInstance.Send(config.FundingSourceAuditDataCleanUpQueue, batch).ConfigureAwait(false);

                await endpointInstance.Send(config.RequiredPaymentAuditDataCleanUpQueue, batch).ConfigureAwait(false);
            }
        }
        public async Task Process(ApprenticeshipCreatedEvent createdEvent)
        {
            try
            {
                logger.LogDebug($"Now processing the apprenticeship created event. " +
                                $"Apprenticeship id: {createdEvent.ApprenticeshipId}, " +
                                $"employer account id: {createdEvent.AccountId}, " +
                                $"Ukprn: {createdEvent.ProviderId}.");
                var model      = mapper.Map <ApprenticeshipModel>(createdEvent);
                var duplicates = await apprenticeshipService.NewApprenticeship(model).ConfigureAwait(false);

                logger.LogDebug($"Apprenticeship saved to database. " +
                                $"Apprenticeship id: {createdEvent.ApprenticeshipId}, " +
                                $"employer account id: {createdEvent.AccountId}, " +
                                $"Ukprn: {createdEvent.ProviderId}.");

                var updatedEvent = mapper.Map <ApprenticeshipUpdated>(model);
                updatedEvent.Duplicates = duplicates.Select(duplicate => new ApprenticeshipDuplicate
                {
                    Ukprn = duplicate.Ukprn, ApprenticeshipId = duplicate.ApprenticeshipId
                }).ToList();
                var endpointInstance = await endpointInstanceFactory.GetEndpointInstance().ConfigureAwait(false);

                await endpointInstance.Publish(updatedEvent).ConfigureAwait(false);

                logger.LogInfo($"Finished processing the apprenticeship created event. " +
                               $"Apprenticeship id: {createdEvent.ApprenticeshipId}, " +
                               $"employer account id: {createdEvent.AccountId}, " +
                               $"Ukprn: {createdEvent.ProviderId}.");
            }
            catch (ApprenticeshipAlreadyExistsException e)
            {
                logger.LogWarning($"Apprenticeship already exists while trying to add a new apprenticeship: {e.Message}\n" +
                                  $"Apprenticeship id: {createdEvent.ApprenticeshipId}, " +
                                  $"employer account id: {createdEvent.AccountId}, " +
                                  $"Ukprn: {createdEvent.ProviderId}.");
            }
            catch (InvalidOperationException e)
            {
                logger.LogError($"Unhandled exception while adding apprenticeship: {e.Message}\n" +
                                $"Apprenticeship id: {createdEvent.ApprenticeshipId}, " +
                                $"employer account id: {createdEvent.AccountId}, " +
                                $"Ukprn: {createdEvent.ProviderId}.", e);
                throw;
            }
            catch (Exception ex)
            {
                logger.LogError($"Error processing the apprenticeship event. Error: {ex.Message}", ex);
                throw;
            }
        }
        public async Task SubmissionFinished(bool succeeded, long jobId, long ukprn, short academicYear, byte collectionPeriod, DateTime ilrSubmissionTime)
        {
            var submissionJobFinished = succeeded ? (SubmissionJobFinishedEvent) new SubmissionJobSucceeded() : new SubmissionJobFailed();

            submissionJobFinished.JobId                 = jobId;
            submissionJobFinished.CollectionPeriod      = collectionPeriod;
            submissionJobFinished.Ukprn                 = ukprn;
            submissionJobFinished.AcademicYear          = academicYear;
            submissionJobFinished.IlrSubmissionDateTime = ilrSubmissionTime;
            logger.LogDebug($"Publishing {submissionJobFinished.GetType().Name} event. Event: {submissionJobFinished.ToJson()}");
            var endpointInstance = await factory.GetEndpointInstance();

            await endpointInstance.Publish(submissionJobFinished).ConfigureAwait(false);
        }
        public async Task TriggerAuditDataCleanup()
        {
            var submissionJobsToBeDeletedBatches = await GetSubmissionJobsToBeDeletedBatches(config.CollectionPeriod, config.AcademicYear);

            var endpointInstance = await endpointInstanceFactory.GetEndpointInstance().ConfigureAwait(false);

            foreach (var batch in submissionJobsToBeDeletedBatches)
            {
                await endpointInstance.Send(config.EarningAuditDataCleanUpQueue, batch).ConfigureAwait(false);

                await endpointInstance.Send(config.DataLockAuditDataCleanUpQueue, batch).ConfigureAwait(false);

                await endpointInstance.Send(config.FundingSourceAuditDataCleanUpQueue, batch).ConfigureAwait(false);

                await endpointInstance.Send(config.RequiredPaymentAuditDataCleanUpQueue, batch).ConfigureAwait(false);
            }
        }
Exemplo n.º 5
0
        private async Task SendReceivedEarningsEvent(long jobId, DateTime ilrSubmissionDateTime, string academicYear, int collectionPeriod, long ukprn)
        {
            var message = new ReceivedProviderEarningsEvent
            {
                JobId = jobId,
                IlrSubmissionDateTime = ilrSubmissionDateTime,
                CollectionPeriod      = new CollectionPeriod {
                    AcademicYear = short.Parse(academicYear), Period = (byte)collectionPeriod
                },
                Ukprn     = ukprn,
                EventTime = DateTimeOffset.UtcNow
            };

            var endpointInstance = await factory.GetEndpointInstance().ConfigureAwait(false);

            await endpointInstance.Publish(message).ConfigureAwait(false);
        }
Exemplo n.º 6
0
        private static async Task RunLevyAccountImport(IEndpointInstanceFactory endpointInstanceFactory, IScheduledJobsConfiguration config, IPaymentLogger log)
        {
            try
            {
                var command          = new ImportEmployerAccounts();
                var endpointInstance = await endpointInstanceFactory.GetEndpointInstance().ConfigureAwait(false);

                await endpointInstance.Send(config.LevyAccountBalanceEndpoint, command).ConfigureAwait(false);
            }
            catch (Exception e)
            {
                log.LogError("Error in LevyAccountImport", e);
                throw;
            }
        }
Exemplo n.º 7
0
        public async Task Process(ApprenticeshipCreatedEvent createdEvent)
        {
            try
            {
                logger.LogDebug($"Now processing the apprenticeship created event. Apprenticeship id: {createdEvent.ApprenticeshipId}, employer account id: {createdEvent.AccountId}, Ukprn: {createdEvent.ProviderId}.");
                var model      = mapper.Map <ApprenticeshipModel>(createdEvent);
                var duplicates = await apprenticeshipService.NewApprenticeship(model).ConfigureAwait(false);

                var updatedEvent = mapper.Map <ApprenticeshipUpdated>(model);
                updatedEvent.Duplicates = duplicates.Select(duplicate => new ApprenticeshipDuplicate {
                    Ukprn = duplicate.Ukprn, ApprenticeshipId = duplicate.ApprenticeshipId
                }).ToList();
                var endpointInstance = await endpointInstanceFactory.GetEndpointInstance().ConfigureAwait(false);

                await endpointInstance.Publish(updatedEvent).ConfigureAwait(false);

                logger.LogInfo($"Finished processing the apprenticeship created event. Apprenticeship id: {createdEvent.ApprenticeshipId}, employer account id: {createdEvent.AccountId}, Ukprn: {createdEvent.ProviderId}.");
            }
            catch (Exception ex)
            {
                logger.LogError($"Error processing the apprenticeship event. Error: {ex.Message}", ex);
                throw;
            }
        }
Exemplo n.º 8
0
        /// <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;
            }
        }
Exemplo n.º 9
0
        public async Task <bool> HandleAsync(JobContextMessage message, CancellationToken cancellationToken)
        {
            try
            {
                logger.LogDebug("Getting task type from period end message.");
                var taskType = GetTask(message);
                logger.LogDebug("Got period end type now create the period end event.");
                var periodEndEvent = CreatePeriodEndEvent(taskType);
                logger.LogDebug($"Created period end event. Type: {periodEndEvent.GetType().Name}");
                periodEndEvent.JobId            = message.JobId;
                periodEndEvent.CollectionPeriod = new CollectionPeriod
                {
                    AcademicYear = Convert.ToInt16(GetMessageValue(message, JobContextMessageConstants.KeyValuePairs.CollectionYear)),
                    Period       = Convert.ToByte(GetMessageValue(message, JobContextMessageConstants.KeyValuePairs.ReturnPeriod))
                };

                logger.LogDebug($"Got period end event: {periodEndEvent.ToJson()}");
                await RecordPeriodEndJob(taskType, periodEndEvent).ConfigureAwait(false);

                var endpointInstance = await endpointInstanceFactory.GetEndpointInstance();

                await endpointInstance.Publish(periodEndEvent);

                logger.LogInfo($"Finished publishing the period end event. Name: {periodEndEvent.GetType().Name}, JobId: {periodEndEvent.JobId}, Collection Period: {periodEndEvent.CollectionPeriod.Period}-{periodEndEvent.CollectionPeriod.AcademicYear}.");

                // TODO: This is a temporary workaround to enable the PeriodEndStart and PeriodEndStop messages to return true as otherwise the service will
                // TODO: just hang as there is nothing implemented to handle the Start and Stop events and so the job status service will never get a completion and so this will never return true.
                // PV2-1345 will handle PeriodEndStart
                // PeriodEndStoppedEvent will be handled by the PeriodEndStoppedEventHandler which in turn is handled by the ProcessProviderMonthEndCommandHandler but we don't want to wait for it


                if (periodEndEvent is PeriodEndStartedEvent || periodEndEvent is PeriodEndStoppedEvent)
                {
                    logger.LogDebug("Returning as this is either a PeriodEndStart or PeriodEndStop event");
                    return(true);
                }

                await jobStatusService.WaitForJobToFinish(message.JobId, cancellationToken);

                return(true);
            }
            catch (Exception ex)
            {
                logger.LogError($"Failed to process job context message. Message: {message.ToJson()}", ex);
                throw;
            }
        }
        private async Task PublishNotLevyPayerEmployerEvents(List <LevyAccountModel> accountModels)
        {
            var notLevyPayingEmployerIds = accountModels.Where(x => !x.IsLevyPayer).Select(x => x.AccountId).ToList();

            if (!notLevyPayingEmployerIds.Any())
            {
                return;
            }
            logger.LogInfo($"Trying to Publish FoundNotLevyPayerEmployerAccount event for  Account Ids: {string.Join(",", notLevyPayingEmployerIds)}");

            var endpointInstance = await endpointInstanceFactory.GetEndpointInstance().ConfigureAwait(false);

            var publishMessageTasks = notLevyPayingEmployerIds.Select(accountId => endpointInstance.Publish(new FoundNotLevyPayerEmployerAccount {
                AccountId = accountId
            }));
            await Task.WhenAll(publishMessageTasks).ConfigureAwait(false);

            logger.LogInfo($"Successfully Published FoundNotLevyPayerEmployerAccount event for  Account Ids: {string.Join(",", notLevyPayingEmployerIds)}");
        }
Exemplo n.º 11
0
 public async Task RunAsync()
 {
     endpointInstance = await endpointInstanceFactory
                        .GetEndpointInstance()
                        .ConfigureAwait(false);
 }