public async Task <List <CertificatePrintStatusUpdateMessage> > Execute() { Schedule schedule = null; List <CertificatePrintStatusUpdateMessage> printStatusUpdateMessages = null; try { _logger.LogInformation("PrintRequestCommand - Started"); schedule = await _scheduleService.Get(); if (schedule == null) { _logger.LogInformation("There is no print schedule which allows printing at this time"); return(null); } await _scheduleService.Start(schedule); var nextBatchReadyToPrint = await _batchService.BuildPrintBatchReadyToPrint(schedule.RunTime, _options.AddReadyToPrintLimit); if (nextBatchReadyToPrint != null) { if ((nextBatchReadyToPrint.Certificates?.Count ?? 0) == 0) { _logger.LogInformation("There are certificates ready to print at this time"); } else { nextBatchReadyToPrint.Status = CertificateStatus.SentToPrinter; nextBatchReadyToPrint.BatchCreated = DateTime.UtcNow; nextBatchReadyToPrint.CertificatesFileName = GetCertificatesFileName(nextBatchReadyToPrint.BatchNumber, nextBatchReadyToPrint.BatchCreated); var printOutput = _printCreator.Create(nextBatchReadyToPrint.BatchNumber, nextBatchReadyToPrint.Certificates); var fileContents = JsonConvert.SerializeObject(printOutput); nextBatchReadyToPrint.NumberOfCertificates = printOutput.Batch.TotalCertificateCount; nextBatchReadyToPrint.NumberOfCoverLetters = printOutput.Batch.PostalContactCount; nextBatchReadyToPrint.FileUploadStartTime = DateTime.UtcNow; var uploadDirectory = _options.Directory; var uploadPath = $"{uploadDirectory}/{nextBatchReadyToPrint.CertificatesFileName}"; await _externalFileTransferClient.UploadFile(fileContents, uploadPath); var archiveDirectory = _options.ArchiveDirectory; var archivePath = $"{archiveDirectory}/{nextBatchReadyToPrint.CertificatesFileName}"; await _internalFileTransferClient.UploadFile(fileContents, archivePath); nextBatchReadyToPrint.FileUploadEndTime = DateTime.UtcNow; printStatusUpdateMessages = await _batchService.Update(nextBatchReadyToPrint); await _notificationService.SendPrintRequest( nextBatchReadyToPrint.BatchNumber, nextBatchReadyToPrint.Certificates, nextBatchReadyToPrint.CertificatesFileName); } } await _scheduleService.Save(schedule); } catch (Exception ex) { try { _logger.LogError(ex, "PrintRequestCommand - Failed"); } finally { if (schedule != null && schedule.Id != Guid.Empty) { await _scheduleService.Fail(schedule); } } throw; } return(printStatusUpdateMessages); }