From 693db1da5487cc1c5d37ccfa495a4994eb3f30de Mon Sep 17 00:00:00 2001 From: Eric Peterson Date: Sat, 24 Jul 2021 17:45:48 +0200 Subject: [PATCH] Review feedback. Signed-off-by: Eric Peterson --- app-config.yaml | 2 +- .../src/stages/publish/local.ts | 47 +++++++++---------- .../src/cache/cacheMiddleware.ts | 2 +- 3 files changed, 24 insertions(+), 27 deletions(-) diff --git a/app-config.yaml b/app-config.yaml index 881e97aa5f..bde037dfe3 100644 --- a/app-config.yaml +++ b/app-config.yaml @@ -113,7 +113,7 @@ techdocs: publisher: type: 'local' # Alternatives - 'googleGcs' or 'awsS3' or 'azureBlobStorage' or 'openStackSwift'. Read documentation for using alternatives. cache: - ttl: 60000 # 1 minute cache for demonstration purposes. You may wish to set this higher in production. + ttl: 3600000 # 1 hour cache for demonstration purposes. You may wish to set this higher in production. sentry: organization: my-company diff --git a/packages/techdocs-common/src/stages/publish/local.ts b/packages/techdocs-common/src/stages/publish/local.ts index 67a4a22914..91a9b57a9e 100644 --- a/packages/techdocs-common/src/stages/publish/local.ts +++ b/packages/techdocs-common/src/stages/publish/local.ts @@ -98,7 +98,10 @@ export class LocalPublish implements PublisherBase { }; } - publish({ entity, directory }: PublishRequest): Promise { + async publish({ + entity, + directory, + }: PublishRequest): Promise { const entityNamespace = entity.metadata.namespace ?? 'default'; const publishDir = this.staticEntityPathJoin( @@ -112,32 +115,26 @@ export class LocalPublish implements PublisherBase { fs.mkdirSync(publishDir, { recursive: true }); } - return new Promise((resolve, reject) => { - fs.copy(directory, publishDir, async err => { - if (err) { - this.logger.debug( - `Failed to copy docs from ${directory} to ${publishDir}`, - ); - reject(err); - } - this.logger.info(`Published site stored at ${publishDir}`); + try { + await fs.copy(directory, publishDir); + this.logger.info(`Published site stored at ${publishDir}`); + } catch (error) { + this.logger.debug( + `Failed to copy docs from ${directory} to ${publishDir}`, + ); + throw error; + } - try { - const techdocsApiUrl = await this.discovery.getBaseUrl('techdocs'); - const objects = (await getFileTreeRecursively(publishDir)).map( - abs => { - return abs.split(`${staticDocsDir}/`)[1]; - }, - ); - resolve({ - remoteUrl: `${techdocsApiUrl}/static/docs/${entity.metadata.name}`, - objects, - }); - } catch (reason) { - reject(reason); - } - }); + // Generate publish response. + const techdocsApiUrl = await this.discovery.getBaseUrl('techdocs'); + const objects = (await getFileTreeRecursively(publishDir)).map(abs => { + return abs.split(`${staticDocsDir}/`)[1]; }); + + return { + remoteUrl: `${techdocsApiUrl}/static/docs/${entity.metadata.name}`, + objects, + }; } async fetchTechDocsMetadata( diff --git a/plugins/techdocs-backend/src/cache/cacheMiddleware.ts b/plugins/techdocs-backend/src/cache/cacheMiddleware.ts index 2650b39296..0c06052ba4 100644 --- a/plugins/techdocs-backend/src/cache/cacheMiddleware.ts +++ b/plugins/techdocs-backend/src/cache/cacheMiddleware.ts @@ -112,7 +112,7 @@ export const createCacheMiddleware = ({ }; // When a socket is closed, if there were no errors and the data written - // over the socket should be cached, cache it as a base64-encoded string! + // over the socket should be cached, cache it! socket.on('close', hadError => { if (writeToCache && !hadError) { cache.set(reqPath, Buffer.concat(chunks));