Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ describe('formatBulkOperationStatus', () => {
expect(result.value).toContain('(42 objects written)')
})

test('formats RUNNING status without object count when count is 0', () => {
const result = formatBulkOperationStatus(createMockOperation({status: 'RUNNING', type: 'QUERY', objectCount: '0'}))
expect(result.value).toBe('Bulk operation in progress')
expect(result.value).not.toContain('objects read')
})

test('formats CREATED status', () => {
const result = formatBulkOperationStatus(createMockOperation({status: 'CREATED'}))
expect(result.value).toBe('Starting')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ export function formatBulkOperationStatus(
): TokenizedString {
switch (operation.status) {
case 'RUNNING':
return outputContent`Bulk operation in progress ${outputToken.gray(
`(${String(operation.objectCount)} objects ${operation.type === 'MUTATION' ? 'written' : 'read'})`,
)}`
return outputContent`Bulk operation in progress${
(operation.objectCount as number) > 0
? outputToken.gray(
` (${String(operation.objectCount)} objects ${operation.type === 'MUTATION' ? 'written' : 'read'})`,
)
: ''
}`
case 'CREATED':
return outputContent`Starting`
case 'COMPLETED':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,34 @@ describe('watchBulkOperation', () => {
)
})

test('uses 1 second interval for first 10 polls, then 5 seconds', async () => {
// Mock 12 running responses, then completed
vi.mocked(adminRequestDoc)
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: runningOperation})
.mockResolvedValueOnce({bulkOperation: completedOperation})

await watchBulkOperation(mockAdminSession, operationId, abortController.signal, () => {})

// Verify first 10 polls use 1 second interval
for (let i = 0; i < 10; i++) {
expect(sleep).toHaveBeenNthCalledWith(i + 1, 1)
}
// Verify subsequent polls use 5 second interval
expect(sleep).toHaveBeenNthCalledWith(11, 5)
expect(sleep).toHaveBeenNthCalledWith(12, 5)
})

describe('when signal is aborted during polling', () => {
beforeEach(() => {
let callCount = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {renderSingleTask} from '@shopify/cli-kit/node/ui'
import {AbortSignal} from '@shopify/cli-kit/node/abort'

const TERMINAL_STATUSES = ['COMPLETED', 'FAILED', 'CANCELED', 'EXPIRED']
const POLL_INTERVAL_SECONDS = 5
const INITIAL_POLL_INTERVAL_SECONDS = 1
const REGULAR_POLL_INTERVAL_SECONDS = 5
const INITIAL_POLL_COUNT = 10
const API_VERSION = '2026-01'

export type BulkOperation = NonNullable<GetBulkOperationByIdQuery['bulkOperation']>
Expand Down Expand Up @@ -47,6 +49,8 @@ async function* pollBulkOperation(
operationId: string,
abortSignal: AbortSignal,
): AsyncGenerator<BulkOperation, BulkOperation> {
let pollCount = 0

while (true) {
// eslint-disable-next-line no-await-in-loop
const response = await fetchBulkOperation(adminSession, operationId)
Expand All @@ -63,11 +67,13 @@ async function* pollBulkOperation(
yield latestOperationState
}

pollCount++

// Use shorter interval for the first 10 polls, then switch to regular interval
const pollInterval = pollCount <= INITIAL_POLL_COUNT ? INITIAL_POLL_INTERVAL_SECONDS : REGULAR_POLL_INTERVAL_SECONDS

// eslint-disable-next-line no-await-in-loop
await Promise.race([
sleep(POLL_INTERVAL_SECONDS),
new Promise((resolve) => abortSignal.addEventListener('abort', resolve)),
])
await Promise.race([sleep(pollInterval), new Promise((resolve) => abortSignal.addEventListener('abort', resolve))])
}
}

Expand Down
Loading