From ee7a1c28592ffe54f59be67e01094e3e25a88c93 Mon Sep 17 00:00:00 2001 From: Enngage Date: Wed, 22 Jan 2020 15:58:44 +0100 Subject: [PATCH] feat: adds condition to skip unsupported files (files that are > 100MB) and adds retry policy for all errors --- src/import/import.models.ts | 33 ++++++++++----------------------- src/import/import.service.ts | 22 ++++++++++++++++++---- src/import/index.ts | 1 - 3 files changed, 28 insertions(+), 28 deletions(-) diff --git a/src/import/import.models.ts b/src/import/import.models.ts index 7ca60ce..f61ec92 100644 --- a/src/import/import.models.ts +++ b/src/import/import.models.ts @@ -9,39 +9,26 @@ import { TaxonomyContracts, } from '@kentico/kontent-management'; -import { IImportItemResult, IProcessedItem, ItemType, ValidImportContract, ValidImportModel } from '../core'; +import { IProcessedItem, ItemType } from '../core'; export interface IImportConfig { workflowIdForImportedItems: string; projectId: string; apiKey: string; enableLog: boolean; + onUnsupportedBinaryFile?: (binaryFile: IBinaryFile) => void; onImport?: (item: IProcessedItem) => void; process?: { - taxonomy?: ( - item: TaxonomyContracts.ITaxonomyContract, - ) => boolean | Promise; + taxonomy?: (item: TaxonomyContracts.ITaxonomyContract) => boolean | Promise; contentTypeSnippet?: ( - item: ContentTypeSnippetContracts.IContentTypeSnippetContract, - ) => boolean | Promise; - contentType?: ( - item: ContentTypeContracts.IContentTypeContract, - ) => boolean | Promise; - contentItem?: ( - item: ContentItemContracts.IContentItemModelContract, - ) => boolean | Promise; - languageVariant?: ( - item: LanguageVariantContracts.ILanguageVariantModelContract, - ) => boolean | Promise; - language?: ( - item: LanguageContracts.ILanguageModelContract, - ) => boolean | Promise; - asset?: ( - item: AssetContracts.IAssetModelContract, - ) => boolean | Promise; - assetFolder?: ( - item: AssetFolderContracts.IAssetFolderContract, + item: ContentTypeSnippetContracts.IContentTypeSnippetContract ) => boolean | Promise; + contentType?: (item: ContentTypeContracts.IContentTypeContract) => boolean | Promise; + contentItem?: (item: ContentItemContracts.IContentItemModelContract) => boolean | Promise; + languageVariant?: (item: LanguageVariantContracts.ILanguageVariantModelContract) => boolean | Promise; + language?: (item: LanguageContracts.ILanguageModelContract) => boolean | Promise; + asset?: (item: AssetContracts.IAssetModelContract) => boolean | Promise; + assetFolder?: (item: AssetFolderContracts.IAssetFolderContract) => boolean | Promise; }; } diff --git a/src/import/import.service.ts b/src/import/import.service.ts index 653d2dc..c7a073a 100644 --- a/src/import/import.service.ts +++ b/src/import/import.service.ts @@ -39,6 +39,13 @@ export class ImportService { this.client = new ManagementClient({ apiKey: config.apiKey, projectId: config.projectId, + retryStrategy: { + addJitter: true, + canRetryError: (err) => true, // so that timeout errors are retried + maxAttempts: 3, + deltaBackoffMs: 1000, + maxCumulativeWaitTimeMs: 60000 + }, httpService: new HttpService({ axiosRequestConfig: { // required for uploading large files @@ -59,7 +66,6 @@ export class ImportService { sourceData: IImportSource ): Promise[]> { const importedItems: IImportItemResult[] = []; - if (this.config.enableLog) { console.log(`Translating object ids to codenames`); } @@ -271,6 +277,7 @@ export class ImportService { currentItems: IImportItemResult[] ): Promise[]> { const importedItems: IImportItemResult[] = []; + const unsupportedBinaryFiles: IBinaryFile[] = []; for (const asset of assets) { const binaryFile = binaryFiles.find(m => m.asset.id === asset.id); @@ -279,15 +286,22 @@ export class ImportService { throw Error(`Could not find binary file for asset with id '${asset.id}'`); } + let binaryDataToUpload: any = binaryFile.binaryData; if (binaryFile.asset.size >= this.maxAllowedAssetSizeInBytes) { - console.log('Skipping file due to size: ', asset.file_name); - continue; + if (this.config.onUnsupportedBinaryFile) { + this.config.onUnsupportedBinaryFile(binaryFile); + } + console.log(`Removing binary data from file due to size. Max. file size is '${this.maxAllowedAssetSizeInBytes}'Bytes, but file has '${asset.size}' Bytes`, asset.file_name); + // remove binary data so that import proceeds & asset is created (so that it can be referenced by + // content items ) + binaryDataToUpload = []; + unsupportedBinaryFiles.push(binaryFile); } const uploadedBinaryFile = await this.client .uploadBinaryFile() .withData({ - binaryData: binaryFile.binaryData, + binaryData: binaryDataToUpload, contentType: asset.type, filename: asset.file_name }) diff --git a/src/import/index.ts b/src/import/index.ts index ae05e10..a30ee2e 100644 --- a/src/import/index.ts +++ b/src/import/index.ts @@ -1,3 +1,2 @@ export * from './import.models'; export * from './import.service'; -export * from './import.helper';