UNPKG

@qso-soft/shared

Version:

Shared library for QSO-soft

47 lines 2.03 kB
import { sleep } from './sleep'; export const chunkArray = (array, size) => Array.from({ length: Math.ceil(array.length / size), }, (_, index) => array.slice(index * size, index * size + size)); export const processBatchesWithInterval = async ({ chunks, interval, callback, logTemplate, logger, }) => { for (const chunk of chunks) { try { await callback(chunk); } catch (error) { logger?.error('Error with processing this data', logTemplate); } if (chunk !== chunks[chunks.length - 1]) { logger?.info(`Delay: ${interval / 1000}s`, logTemplate); await sleep(interval); } } }; export const processChunkWithRetry = async ({ chunk, retryCount, delayBetweenRetries, callback, logTemplate, logger, }) => { const results = await Promise.allSettled(chunk.map(callback)); const failedCallbacks = results.filter((result) => result.status === 'rejected'); if (failedCallbacks.length > 0 && retryCount > 0) { logger?.info(`Delay between retries: ${delayBetweenRetries}s`, logTemplate); await sleep(delayBetweenRetries); return processChunkWithRetry({ chunk: failedCallbacks.map((result) => 'reason' in result && result.reason), retryCount: retryCount - 1, delayBetweenRetries, callback, }); } return results; }; export const processChunksWithDelayAndRetry = async ({ chunks, delayBetweenChunks, logTemplate, logger, ...rest }) => { const allResults = []; for (const [index, chunk] of chunks.entries()) { const isNotFirstChunk = index > 0; if (isNotFirstChunk) { logger?.info(`Delay between chunks: ${delayBetweenChunks}s`, logTemplate); await sleep(delayBetweenChunks); } const batchResults = await processChunkWithRetry({ chunk, logger, logTemplate, ...rest }); allResults.push(...batchResults); } return allResults; }; //# sourceMappingURL=batching.js.map