Skip to content

Commit ec7fdbb

Browse files
committed
refat: guards backfill improvement
1 parent 4a5c779 commit ec7fdbb

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

indexer/src/services/sync/guards.ts

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import pLimit from 'p-limit';
2-
import { closeDatabase, rootPgPool, sequelize } from '../../config/database';
2+
import { rootPgPool, sequelize } from '../../config/database';
33
import { getGuardsFromBalances } from './payload';
44
import Guard from '../../models/guard';
55

6-
const CONCURRENCY_LIMIT = 4; // Number of concurrent fetches allowed
6+
const CONCURRENCY_LIMIT = 50; // Number of concurrent fetches allowed
77
const limitFetch = pLimit(CONCURRENCY_LIMIT);
88

99
export async function backfillGuards() {
@@ -17,13 +17,19 @@ export async function backfillGuards() {
1717
await rootPgPool.query(deleteGuardsQuery);
1818

1919
const limit = 1000; // Number of rows to process in one batch
20-
let offset = 0;
20+
let currentId = 0;
2121

2222
while (true) {
23-
console.log(`Fetching rows from offset: ${offset}, limit: ${limit}`);
23+
console.log(`Fetching rows starting from: ${currentId}`);
2424
const res = await rootPgPool.query(
25-
`SELECT b.id, b.account, b."chainId", b.module FROM "Balances" b ORDER BY b.id LIMIT $1 OFFSET $2`,
26-
[limit, offset],
25+
`
26+
SELECT b.id, b.account, b."chainId", b.module
27+
FROM "Balances" b
28+
WHERE b.id > $2
29+
ORDER BY b.id
30+
LIMIT $1
31+
`,
32+
[limit, currentId],
2733
);
2834

2935
const rows = res.rows;
@@ -53,13 +59,13 @@ export async function backfillGuards() {
5359
});
5460

5561
await tx.commit();
56-
console.log(`Batch at offset ${offset} processed successfully.`);
57-
offset += limit;
62+
console.log(`Row at ${currentId} id processed successfully.`);
63+
currentId = rows[rows.length - 1].id;
5864
} catch (batchError) {
59-
console.error(`Error processing batch at offset ${offset}:`, batchError);
65+
console.error(`Error processing at id ${currentId}:`, batchError);
6066
try {
6167
await tx.rollback();
62-
console.log(`Transaction for batch at offset ${offset} rolled back.`);
68+
console.log(`Transaction for id at ${currentId} rolled back.`);
6369
} catch (rollbackError) {
6470
console.error('Error during rollback:', rollbackError);
6571
}

indexer/src/services/sync/streaming.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ export async function startStreaming() {
5757
);
5858

5959
backfillGuards(); // run when initialize
60-
setInterval(backfillGuards, 1000 * 60 * 60); // every one hour
60+
setInterval(backfillGuards, 1000 * 60 * 60 * 12); // every one 12 hours
6161
}
6262

6363
export function processPayload(payload: any) {

0 commit comments

Comments
 (0)