Example #1
0
        private async Task <AssetReady> Convert(ITransformAsset message)
        {
            var currentStatus = AufbereitungsStatusEnum.PaketTransferiert;
            var id            = message.AssetType == AssetType.Gebrauchskopie ? message.ArchiveRecordId : message.OrderItemId.ToString();
            var assetReady    = new AssetReady
            {
                ArchiveRecordId       = message.ArchiveRecordId,
                OrderItemId           = message.OrderItemId,
                CallerId              = message.CallerId,
                AssetType             = message.AssetType,
                Recipient             = message.Recipient,
                RetentionCategory     = message.RetentionCategory,
                PrimaerdatenAuftragId = message.PrimaerdatenAuftragId
            };

            try
            {
                // Let's convert the package
                var result = await assetManager.ConvertPackage(id, message.AssetType,
                                                               message.ProtectWithPassword, message.FileName, message.PackageId);

                currentStatus = AufbereitungsStatusEnum.AssetUmwandlungAbgeschlossen;
                await UpdatePrimaerdatenAuftragStatus(message, AufbereitungsServices.AssetService, currentStatus, result.ErrorMessage);

                if (!result.Valid)
                {
                    // We have failed in the conversion inform the world about the failed package
                    assetReady.Valid        = false;
                    assetReady.ErrorMessage = result.ErrorMessage;
                    return(assetReady);
                }

                // Upload to cache and then delete the package
                var uploadSuccess = await cacheHelper.SaveToCache(bus, message.RetentionCategory, result.FileName);

                var errorMessage = uploadSuccess ? null : $"Unable to upload file {result.FileName} to cache";
                currentStatus = AufbereitungsStatusEnum.ImCacheAbgelegt;
                await UpdatePrimaerdatenAuftragStatus(message, AufbereitungsServices.CacheService, currentStatus, errorMessage);

                File.Delete(result.FileName);

                if (!uploadSuccess)
                {
                    assetReady.Valid        = false;
                    assetReady.ErrorMessage = errorMessage;
                    return(assetReady);
                }

                assetReady.Valid = true;
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Unexpected error in the conversion of the package for ID {id} and AssetType {AssetType}.", id, message.AssetType);
                assetReady.Valid        = false;
                assetReady.ErrorMessage = $"Unexpected exception occured.\nException:\n{ex}";
                await UpdatePrimaerdatenAuftragStatus(message, AufbereitungsServices.CacheService, currentStatus, ex.Message);
            }

            return(assetReady);
        }
        private async Task PublishAssetReadyFailed(ConsumeContext <PrepareForTransformationMessage> context, string errorMessage)
        {
            try
            {
                var message = context.Message;
                // If we do have an error, we stop the sync process here.
                Log.Error(
                    "Failed to preprocess the package for transformation for archiveRecord {archiveRecordId} with conversationId {ConversationId}",
                    message.RepositoryPackage.ArchiveRecordId, context.ConversationId);

                var assetReady = new AssetReady
                {
                    ArchiveRecordId       = message.RepositoryPackage.ArchiveRecordId,
                    OrderItemId           = message.OrderItemId,
                    CallerId              = message.CallerId,
                    AssetType             = message.AssetType,
                    Recipient             = message.Recipient,
                    RetentionCategory     = message.RetentionCategory,
                    PrimaerdatenAuftragId = message.PrimaerdatenAuftragId,
                    // We have failed in the conversion inform the world about the failed package
                    Valid        = false,
                    ErrorMessage = errorMessage
                };

                await assetManager.UpdatePrimaerdatenAuftragStatus(new UpdatePrimaerdatenAuftragStatus
                {
                    PrimaerdatenAuftragId = context.Message.PrimaerdatenAuftragId,
                    Service   = AufbereitungsServices.AssetService,
                    Status    = AufbereitungsStatusEnum.PreprocessingAbgeschlossen,
                    ErrorText = errorMessage
                });

                // inform the world about the failed package
                await context.Publish <IAssetReady>(assetReady);
            }
            catch (Exception ex)
            {
                Log.Error(ex, "Unexpected error while publish a failed AssetReady event.");
            }
            finally
            {
                // Delete the temp files
                var tempFolder = GetTempFolder(context.Message.RepositoryPackage);
                if (Directory.Exists(tempFolder))
                {
                    Directory.Delete(tempFolder, true);
                }
            }
        }