From f76d021c936f83e2b742a0827778ffe72a26fd6b Mon Sep 17 00:00:00 2001 From: Robert Field Date: Sat, 24 Aug 2024 11:30:12 +0100 Subject: [PATCH] fix: publish catalog delay (#244) * feat: use backoff for catalog publish waiting * chore: add changeset --- .changeset/weak-roses-approve.md | 5 +++ .../integration/klevu/tasks/klevu-task.ts | 42 +++++++++++-------- 2 files changed, 29 insertions(+), 18 deletions(-) create mode 100644 .changeset/weak-roses-approve.md diff --git a/.changeset/weak-roses-approve.md b/.changeset/weak-roses-approve.md new file mode 100644 index 00000000..72c62989 --- /dev/null +++ b/.changeset/weak-roses-approve.md @@ -0,0 +1,5 @@ +--- +"composable-cli": patch +--- + +use backoff for catalog publish waiting diff --git a/packages/composable-cli/src/commands/integration/klevu/tasks/klevu-task.ts b/packages/composable-cli/src/commands/integration/klevu/tasks/klevu-task.ts index 5cf49540..1decdcc4 100644 --- a/packages/composable-cli/src/commands/integration/klevu/tasks/klevu-task.ts +++ b/packages/composable-cli/src/commands/integration/klevu/tasks/klevu-task.ts @@ -14,6 +14,7 @@ import { import { KLEVU_INTEGRATION_NAME } from "../utility/error-messages" import { setupKlevuIntegrationTasks } from "./setup-klevu-integration-tasks" import { setupKlevuCustomApiEntryTasks } from "./setup-klevu-custom-api-entry-tasks" +import { backOff } from "exponential-backoff" export function createKlevuTask({ unsubscribe, @@ -72,27 +73,34 @@ export function createKlevuTask({ ) } - let catalogStatus = publishResult.data.meta.release_status - while ( - catalogStatus === "PENDING" || - catalogStatus === "IN_PROGRESS" - ) { - // Wait 3 seconds before checking the status again - await timer(3000) - const catalogStatusResult = await getCatalogRelease( + const backOffFn = () => + getCatalogRelease( ctx.requester, - ctx.catalog.id, + ctx.catalog!.id, publishResult.data.id, + ).then((val) => { + if ( + val.success && + (val.data.meta.release_status === "PENDING" || + val.data.meta.release_status === "IN_PROGRESS") + ) { + throw new Error("Still in progress") + } + return val + }) + + const catalogStatusResult = await backOff(backOffFn, { + startingDelay: 3000, + numOfAttempts: 5, + }) + + if (!catalogStatusResult.success) { + throw new Error( + `Failed to get catalog status - ${catalogStatusResult.error.message}`, ) - if (!catalogStatusResult.success) { - throw new Error( - `Failed to get catalog status - ${catalogStatusResult.error.message}`, - ) - } - catalogStatus = catalogStatusResult.data.meta.release_status } - if (catalogStatus === "FAILED") { + if (catalogStatusResult.data.meta.release_status === "FAILED") { throw new Error( `Failed to publish catalog - ${ctx.catalog.attributes.name} catalog`, ) @@ -109,5 +117,3 @@ export function createKlevuTask({ ) } } - -const timer = (ms: number) => new Promise((res) => setTimeout(res, ms))