From f55150824f77861c2ead32336d6ca5c8a9aa78b7 Mon Sep 17 00:00:00 2001 From: Artem Ufimtcev Date: Fri, 8 Nov 2024 16:53:55 +0100 Subject: [PATCH 1/4] Implement docs job await --- package.json | 2 +- src/commands/publish-documentation.ts | 30 ++++++++++++++++++++------- src/utils/common.ts | 3 +++ 3 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 src/utils/common.ts diff --git a/package.json b/package.json index 7247300..96d6c7e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@supernovaio/cli", "description": "Supernova.io Command Line Interface", - "version": "1.1.1", + "version": "1.1.2", "author": "Supernova.io", "homepage": "https://supernova.io/", "keywords": [ diff --git a/src/commands/publish-documentation.ts b/src/commands/publish-documentation.ts index a2a049d..a1feb59 100644 --- a/src/commands/publish-documentation.ts +++ b/src/commands/publish-documentation.ts @@ -13,7 +13,8 @@ import { Command, Flags } from "@oclif/core" import { Environment, ErrorCode } from "../types/types" import { getWritableVersion } from "../utils/sdk" import "colors" -import { DocumentationEnvironment } from "@supernovaio/sdk" +import { DocumentationEnvironment, ExportBuildStatus } from "@supernovaio/sdk" +import { sleep } from "../utils/common" // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- // MARK: - Definition @@ -76,18 +77,25 @@ export class PublishDocumentation extends Command { } // Get workspace -> design system –> version - let { instance, id } = await getWritableVersion(flags) - let result = await instance.documentation.publishDrafts(id, environment, { + let { instance, id, designSystem } = await getWritableVersion(flags) + let publishJob = await instance.documentation.publishDrafts(id, environment, { pagePersistentIds: [], groupPersistentIds: [], }) - if (result.status === "Success") { + let jobStatus = publishJob.status + while (!isJobStatusDone(jobStatus)) { + await sleep(1000) + const updatedJob = await instance.documentation.getDocumentationBuild(designSystem.workspaceId, publishJob.id) + jobStatus = updatedJob.status + } + + if (publishJob.status === "Success") { this.log("\nDone: Documentation queued for publishing".green) - } else if (result.status === "InProgress") { - this.log("\n Done: Skipped documentation publish as another build is already in progress".green) - } else if (result.status === "Failed") { - throw new Error(`Documentation publish failed with unknown failure`) + } else if (publishJob.status === "Failed") { + throw new Error(`Documentation publish failed`) + } else if (publishJob.status === "Timeout") { + throw new Error(`Documentation publish timed out`) } } catch (error) { // Catch general error @@ -109,3 +117,9 @@ function tryParseDocsEnvironment(targetArg: string) { return null } } + +function isJobStatusDone(status: ExportBuildStatus): boolean { + return ( + status === ExportBuildStatus.Failed || status === ExportBuildStatus.Success || status === ExportBuildStatus.Timeout + ) +} diff --git a/src/utils/common.ts b/src/utils/common.ts new file mode 100644 index 0000000..fcc1262 --- /dev/null +++ b/src/utils/common.ts @@ -0,0 +1,3 @@ +export async function sleep(ms: number): Promise { + return new Promise(resolve => setTimeout(resolve, ms)) +} From 1b2fedfccb569a97b3fd32bf0c54648aa076cd6e Mon Sep 17 00:00:00 2001 From: Artem Ufimtcev Date: Fri, 8 Nov 2024 17:06:43 +0100 Subject: [PATCH 2/4] Add await timeout --- src/commands/publish-documentation.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/commands/publish-documentation.ts b/src/commands/publish-documentation.ts index a1feb59..4abb544 100644 --- a/src/commands/publish-documentation.ts +++ b/src/commands/publish-documentation.ts @@ -83,11 +83,11 @@ export class PublishDocumentation extends Command { groupPersistentIds: [], }) - let jobStatus = publishJob.status - while (!isJobStatusDone(jobStatus)) { + // Timeout is roughly 30 minutes + for (let i = 0; i < 30 * 60; i++) { await sleep(1000) const updatedJob = await instance.documentation.getDocumentationBuild(designSystem.workspaceId, publishJob.id) - jobStatus = updatedJob.status + if (isJobStatusDone(updatedJob.status)) break } if (publishJob.status === "Success") { @@ -96,6 +96,8 @@ export class PublishDocumentation extends Command { throw new Error(`Documentation publish failed`) } else if (publishJob.status === "Timeout") { throw new Error(`Documentation publish timed out`) + } else { + throw new Error(`Error awaiting publish job`) } } catch (error) { // Catch general error From 40b57396d511922da2b74bf8987505ca0557a17a Mon Sep 17 00:00:00 2001 From: Artem Ufimtcev Date: Fri, 8 Nov 2024 17:17:58 +0100 Subject: [PATCH 3/4] Add an option to not await the job --- src/commands/publish-documentation.ts | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/commands/publish-documentation.ts b/src/commands/publish-documentation.ts index 4abb544..875b486 100644 --- a/src/commands/publish-documentation.ts +++ b/src/commands/publish-documentation.ts @@ -61,6 +61,14 @@ export class PublishDocumentation extends Command { hidden: true, required: false, }), + awaitPublishJob: Flags.boolean({ + description: + "Whether to block the process until the publishing is done. " + + "Setting the flag to false will exit with success as long as documentation publish was successfully triggered, " + + "but before the publish is completed. Setting the flag to true will exit once the publish is complete and will " + + "throw if documentation publish is not successful.", + default: true, + }), } // --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- @@ -83,6 +91,12 @@ export class PublishDocumentation extends Command { groupPersistentIds: [], }) + if (!(flags.awaitPublishJob ?? true)) { + this.log(`Publishing documentation in ${designSystem.name} has started, job await is disabled, exiting...`) + } else { + this.log(`Publishing documentation in ${designSystem.name}...`) + } + // Timeout is roughly 30 minutes for (let i = 0; i < 30 * 60; i++) { await sleep(1000) From 60265d2ae3ac6f385277840f5fa226f833e4a79e Mon Sep 17 00:00:00 2001 From: Artem Ufimtcev Date: Fri, 8 Nov 2024 17:19:30 +0100 Subject: [PATCH 4/4] Const --- src/commands/publish-documentation.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/publish-documentation.ts b/src/commands/publish-documentation.ts index 875b486..c3e5794 100644 --- a/src/commands/publish-documentation.ts +++ b/src/commands/publish-documentation.ts @@ -85,8 +85,8 @@ export class PublishDocumentation extends Command { } // Get workspace -> design system –> version - let { instance, id, designSystem } = await getWritableVersion(flags) - let publishJob = await instance.documentation.publishDrafts(id, environment, { + const { instance, id, designSystem } = await getWritableVersion(flags) + const publishJob = await instance.documentation.publishDrafts(id, environment, { pagePersistentIds: [], groupPersistentIds: [], })