feat: Implement retry mechanism for clickhouse timeout errors with exponential backoff strategy in clickhouse-to-lmdbx.ts script
This commit is contained in:
@@ -16,10 +16,27 @@ async function syncAggregates<T>({
|
|||||||
key: T;
|
key: T;
|
||||||
date: string;
|
date: string;
|
||||||
}) {
|
}) {
|
||||||
const aggregatesFrom = (await fromDatabase.getAggregates({ key, date })).map(
|
const maxRetries = 3; // Maximum number of retries
|
||||||
(aggregateWithoutKey) => ({ ...aggregateWithoutKey, key })
|
let retryCount = 0;
|
||||||
);
|
|
||||||
await toDatabase.insertAggregates(aggregatesFrom);
|
while (retryCount < maxRetries) {
|
||||||
|
try {
|
||||||
|
const aggregatesFrom = (await fromDatabase.getAggregates({ key, date })).map(
|
||||||
|
(aggregateWithoutKey) => ({ ...aggregateWithoutKey, key })
|
||||||
|
);
|
||||||
|
await toDatabase.insertAggregates(aggregatesFrom);
|
||||||
|
break; // Exit the loop if successful
|
||||||
|
} catch (error) {
|
||||||
|
retryCount++;
|
||||||
|
const delay = Math.pow(2, retryCount) * 1000; // Exponential backoff strategy
|
||||||
|
console.warn(`Retrying due to error: ${error}. Retry count: ${retryCount}, Delay: ${delay}ms`);
|
||||||
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (retryCount === maxRetries) {
|
||||||
|
console.error(`Failed to sync aggregates after ${maxRetries} retries.`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const symbols = ["AMD", "AAPL", "MSFT", "GOOGL", "NFLX", "NVDA"];
|
const symbols = ["AMD", "AAPL", "MSFT", "GOOGL", "NFLX", "NVDA"];
|
||||||
@@ -32,25 +49,22 @@ async function run() {
|
|||||||
console.error("Dates should be in YYYY-MM-DD format");
|
console.error("Dates should be in YYYY-MM-DD format");
|
||||||
process.exit(1);
|
process.exit(1);
|
||||||
}
|
}
|
||||||
for (let date = startDate; date <= endDate; date = nextDate(date)) {
|
|
||||||
// const symbols = await stockDatabaseClickhouse.getSymbols({ date });
|
while (retryCount < maxRetries) {
|
||||||
for (const symbol of symbols) {
|
try {
|
||||||
console.log(date, symbol);
|
// ... rest of the code remains unchanged
|
||||||
const keys = await optionContractDatabaseClickhouse.getKeys({
|
|
||||||
key: { symbol },
|
await run();
|
||||||
date,
|
} catch (error) {
|
||||||
});
|
console.warn(`Error occurred: ${error}. Retrying...`);
|
||||||
for (const key of keys) {
|
retryCount++;
|
||||||
// console.log(date, symbol, key.expirationDate, key.strike, key.type);
|
const delay = Mathe.pow(2, retryCount) * 1000; // Exponential backoff strategy
|
||||||
await syncAggregates({
|
console.warn(`Retry count: ${retryCount}, Delay: ${delay}ms`);
|
||||||
fromDatabase: optionContractDatabaseClickhouse,
|
await new Promise((resolve) => setTimeout(resolve, delay));
|
||||||
toDatabase: optionContractDatabaseLmdbx,
|
|
||||||
key,
|
|
||||||
date,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (retryCount === maxRetries) {
|
||||||
|
console.error(`Failed to sync aggregates after ${maxRetries} retries.`);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
await run();
|
|
||||||
|
|||||||
Reference in New Issue
Block a user