Exemplo n.º 1
0
        private async Task SendSubmissionEvent(SubmissionEvent submissionEvent)
        {
            var endpointInstance = await factory.GetEndpointInstance().ConfigureAwait(false);

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

            var jobClient = jobClientFactory.Create();

            if (submissionEvent is SubmissionFailedEvent)
            {
                await jobClient.RecordJobFailure(submissionEvent.JobId, submissionEvent.Ukprn, submissionEvent.IlrSubmissionDateTime, submissionEvent.AcademicYear, submissionEvent.CollectionPeriod).ConfigureAwait(false);
            }
            else
            {
                await jobClient.RecordJobSuccess(submissionEvent.JobId, submissionEvent.Ukprn, submissionEvent.IlrSubmissionDateTime, submissionEvent.AcademicYear, submissionEvent.CollectionPeriod).ConfigureAwait(false);
            }
        }
        private async Task <double> ProcessFm36Global(JobContextMessage message, int collectionPeriod, FM36Global fm36Output, string ilrFileName, CancellationToken cancellationToken)
        {
            logger.LogVerbose("Now building commands.");
            var startTime = DateTimeOffset.UtcNow;
            var learners  = fm36Output.Learners ?? new List <FM36Learner>();
            var commands  = learners
                            .Select(learner => Build(learner, message.JobId, message.SubmissionDateTimeUtc, short.Parse(fm36Output.Year), collectionPeriod, fm36Output.UKPRN, ilrFileName))
                            .ToList();

            var jobStatusClient = jobClientFactory.Create();
            var messageName     = typeof(ProcessLearnerCommand).FullName;

            logger.LogVerbose($"Now sending the start job command for job: {message.JobId}");
            await jobStatusClient.StartJob(message.JobId, fm36Output.UKPRN, message.SubmissionDateTimeUtc, short.Parse(fm36Output.Year), (byte)collectionPeriod,
                                           commands.Select(cmd => new GeneratedMessage {
                StartTime = startTime, MessageId = cmd.CommandId, MessageName = messageName
            }).ToList(), startTime);

            logger.LogDebug($"Now sending the process learner commands for job: {message.JobId}");
            var stopwatch = new Stopwatch();

            stopwatch.Start();
            var endpointInstance = await factory.GetEndpointInstance();

            foreach (var learnerCommand in commands)
            {
                try
                {
                    if (cancellationToken.IsCancellationRequested)
                    {
                        logger.LogWarning($"Cancellation requested, will now stop sending learners for job: {message.JobId}");
                        return(stopwatch.ElapsedMilliseconds);
                    }

                    await endpointInstance.SendLocal(learnerCommand).ConfigureAwait(false);

                    var aims = submittedLearnerAimBuilder.Build(learnerCommand);
                    await Task.WhenAll(aims.Select(aim => submittedAimWriter.Write(aim, cancellationToken))).ConfigureAwait(false);

                    logger.LogVerbose($"Successfully sent ProcessLearnerCommand JobId: {learnerCommand.JobId}, Ukprn: {fm36Output.UKPRN}, LearnRefNumber: {learnerCommand.Learner.LearnRefNumber}, SubmissionTime: {message.SubmissionDateTimeUtc}, Collection Year: {fm36Output.Year}, Collection period: {collectionPeriod}");
                }
                catch (Exception ex)
                {
                    logger.LogError($"Error sending the command: ProcessLearnerCommand. Job Id: {message.JobId}, Ukprn: {fm36Output.UKPRN}, Error: {ex.Message}", ex);
                    throw;
                }
            }

            await submittedAimWriter.Flush(cancellationToken).ConfigureAwait(false);

            stopwatch.Stop();
            var duration = stopwatch.ElapsedMilliseconds;

            telemetry.TrackEvent("Sent All ProcessLearnerCommand Messages",
                                 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 },
            });
            logger.LogDebug($"Took {stopwatch.ElapsedMilliseconds}ms to send {commands.Count} Process Learner Commands for Job: {message.JobId}");
            return(stopwatch.ElapsedMilliseconds);
        }