From e90448fbd204c3e3725938ab6af9fc3260617a07 Mon Sep 17 00:00:00 2001 From: Josh Johnson Date: Fri, 29 Jul 2022 10:49:33 -0400 Subject: [PATCH] feat(gatsby-source-shopify): Add query runtime warning for CI environments (#36142) * Add query runtime warning for CI environments * Make timer better --- .../src/create-operations.ts | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/packages/gatsby-source-shopify/src/create-operations.ts b/packages/gatsby-source-shopify/src/create-operations.ts index 0dff5a0e6724d..bd0bd08e22af3 100644 --- a/packages/gatsby-source-shopify/src/create-operations.ts +++ b/packages/gatsby-source-shopify/src/create-operations.ts @@ -57,6 +57,12 @@ export function createOperations( } async function finishLastOperation(): Promise { + const queryStartTime = Date.now() + let lastWarningTime = queryStartTime + const baseWarningInterval = 60 * 1000 + let warningInterval = baseWarningInterval + let warningCount = 0 + let { currentBulkOperation } = await currentOperation() if (currentBulkOperation && currentBulkOperation.id) { if (!finishedStatuses.includes(currentBulkOperation.status)) { @@ -68,6 +74,28 @@ export function createOperations( while (!finishedStatuses.includes(currentBulkOperation.status)) { await new Promise(resolve => setTimeout(resolve, 1000)) currentBulkOperation = (await currentOperation()).currentBulkOperation + + // add warning for CI environments + if ( + process.env.CI && + Date.now() > lastWarningTime + warningInterval + ) { + lastWarningTime = Date.now() + const runtime = Math.floor( + (lastWarningTime - queryStartTime) / 1000 + ) + gatsbyApi.reporter.warn( + `Operation ${currentBulkOperation.id} is still running after ${runtime} seconds with status "${currentBulkOperation.status}"` + ) + + // handle next interval, slowly increase time so we don't flood every minute + warningCount += 1 + warningInterval = + warningCount <= 5 + ? baseWarningInterval + : baseWarningInterval * 2 * (warningCount - 5) + } + timer.setStatus( `Polling operation ${currentBulkOperation.id} : ${currentBulkOperation.status}` )