fix: throttle Graphs backfill by Tiny request
All checks were successful
Build and Deploy / build-and-push (push) Successful in 45s
All checks were successful
Build and Deploy / build-and-push (push) Successful in 45s
This commit is contained in:
@@ -67,16 +67,18 @@ const numberEnv = (name: string, fallback: number) => {
|
||||
};
|
||||
const MAX_DAYS = numberEnv('GRAPHS_BACKFILL_MAX_DAYS', 0);
|
||||
const MAX_ORDERS = numberEnv('GRAPHS_BACKFILL_MAX_ORDERS', 0);
|
||||
const TINY_SEARCH_DELAY_MS = numberEnv('TINY_SEARCH_DELAY_MS', 1000);
|
||||
const TINY_DETAIL_DELAY_MS = numberEnv('TINY_DETAIL_DELAY_MS', 1500);
|
||||
const TINY_REQUEST_DELAY_MS = numberEnv('GRAPHS_BACKFILL_TINY_REQUEST_DELAY_MS', 6000);
|
||||
const TINY_SEARCH_DELAY_MS = numberEnv('TINY_SEARCH_DELAY_MS', 0);
|
||||
const TINY_DETAIL_DELAY_MS = numberEnv('TINY_DETAIL_DELAY_MS', 0);
|
||||
const TINY_CONTACT_DELAY_MS = numberEnv('TINY_CONTACT_DELAY_MS', TINY_DETAIL_DELAY_MS);
|
||||
const ORDER_DELAY_MS = numberEnv('GRAPHS_BACKFILL_ORDER_DELAY_MS', 6000);
|
||||
const ORDER_DELAY_MS = numberEnv('GRAPHS_BACKFILL_ORDER_DELAY_MS', 0);
|
||||
const TINY_BLOCK_DELAY_MS = numberEnv('GRAPHS_BACKFILL_TINY_BLOCK_DELAY_MS', 600000);
|
||||
const MAX_TINY_BLOCK_RETRIES = numberEnv('GRAPHS_BACKFILL_MAX_TINY_BLOCK_RETRIES', 0);
|
||||
const STATE_FILE = process.env.GRAPHS_BACKFILL_STATE_FILE || path.join(process.cwd(), 'graphs_backfill_state.json');
|
||||
const STOP_FILE = process.env.GRAPHS_BACKFILL_STOP_FILE || path.join(process.cwd(), 'graphs_backfill_stop.json');
|
||||
|
||||
const contactCache = new Map<string, ContactInfo | null>();
|
||||
let lastTinyRequestAt = 0;
|
||||
|
||||
const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms));
|
||||
|
||||
@@ -220,11 +222,19 @@ function isTinyApiBlocked(data: any) {
|
||||
});
|
||||
}
|
||||
|
||||
async function waitForTinyRequestSlot(servicePhp: string) {
|
||||
const elapsed = Date.now() - lastTinyRequestAt;
|
||||
const waitMs = Math.max(0, TINY_REQUEST_DELAY_MS - elapsed);
|
||||
await sleepWithStop(waitMs, `Tiny ${servicePhp} request slot`);
|
||||
lastTinyRequestAt = Date.now();
|
||||
}
|
||||
|
||||
async function tinyPost(servicePhp: string, params: URLSearchParams) {
|
||||
let blockedAttempts = 0;
|
||||
|
||||
while (true) {
|
||||
assertNotStopped();
|
||||
await waitForTinyRequestSlot(servicePhp);
|
||||
|
||||
const response = await axios.post(`https://api.tiny.com.br/api2/${servicePhp}`, params, {
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
|
||||
@@ -452,11 +462,13 @@ async function runBackfill() {
|
||||
let processedDays = 0;
|
||||
let processedOrders = 0;
|
||||
const orderPace = ORDER_DELAY_MS ? `~${Math.floor(60000 / ORDER_DELAY_MS)}/minute` : 'unlimited';
|
||||
const tinyRequestPace = TINY_REQUEST_DELAY_MS ? `~${Math.floor(60000 / TINY_REQUEST_DELAY_MS)}/minute` : 'unlimited';
|
||||
|
||||
console.log(`[config] Start date: ${currentDate}`);
|
||||
console.log(`[config] End date: ${END_DATE}`);
|
||||
console.log(`[config] Dry run: ${DRY_RUN ? 'yes' : 'no'}`);
|
||||
console.log(`[config] Contact fallback: ${CONTACT_FALLBACK ? 'yes' : 'no'}`);
|
||||
console.log(`[config] Tiny request pace: 1 request every ${TINY_REQUEST_DELAY_MS}ms (${tinyRequestPace})`);
|
||||
console.log(`[config] Order pace: 1 order every ${ORDER_DELAY_MS}ms (${orderPace})`);
|
||||
console.log(`[config] Tiny block pause: ${TINY_BLOCK_DELAY_MS}ms`);
|
||||
console.log(`[config] State file: ${STATE_FILE}`);
|
||||
|
||||
Reference in New Issue
Block a user