Skip to content

Commit c851dd9

Browse files
committed
improve script
1 parent b5d6474 commit c851dd9

File tree

1 file changed

+47
-15
lines changed

1 file changed

+47
-15
lines changed

src/utils/runAdapterHistorical.ts

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,18 @@ import PromisePool from "@supercharge/promise-pool";
1010
const startTs = Number(process.argv[2]);
1111
const endTs = Number(process.argv[3]);
1212
const bridgeName = process.argv[4];
13-
const chain = process.argv[5];
14-
const blockByChain: Record<string, { startBlock: number; endBlock: number }> = {};
13+
const sequential = process.argv.includes("--sequential");
14+
const chain = process.argv[5] && !process.argv[5].startsWith("--") ? process.argv[5] : undefined;
15+
let shuttingDown = false;
16+
17+
process.on("SIGINT", () => {
18+
if (shuttingDown) {
19+
console.log("\nForce shutdown.");
20+
process.exit(1);
21+
}
22+
shuttingDown = true;
23+
console.log("\nGraceful shutdown requested. Waiting for in-flight tasks to finish...");
24+
});
1525

1626
function splitIntoDailyIntervals(startTimestamp: number, endTimestamp: number): Array<{ start: number; end: number }> {
1727
const intervals = [];
@@ -41,6 +51,7 @@ async function fillAdapterHistorical(
4151
bridgeDbName: string,
4252
restrictChainTo?: string
4353
) {
54+
if (shuttingDown) return;
4455
if (!startTimestamp || !endTimestamp || !bridgeDbName) {
4556
console.error(
4657
"Missing parameters, please provide startTimestamp, endTimestamp and bridgeDbName. \nExample: npm run adapter 1704690402 1704949602 arbitrum"
@@ -50,7 +61,11 @@ async function fillAdapterHistorical(
5061

5162
const adapter = bridgeNetworkData.find((x) => x.bridgeDbName === bridgeDbName);
5263
if (!adapter) throw new Error("Invalid adapter");
53-
console.log(`Found ${bridgeDbName}`);
64+
const startDate = new Date(startTimestamp * 1000).toISOString().slice(0, 10);
65+
const endDate = new Date(endTimestamp * 1000).toISOString().slice(0, 10);
66+
console.log(`[${bridgeDbName}] Processing ${startDate} to ${endDate} (${startTimestamp}-${endTimestamp})${restrictChainTo ? ` chain=${restrictChainTo}` : ""}`);
67+
68+
const blockByChain: Record<string, { startBlock: number; endBlock: number }> = {};
5469

5570
const promises = Promise.all(
5671
adapter.chains.map(async (chain, i) => {
@@ -69,12 +84,12 @@ async function fillAdapterHistorical(
6984
if (bridgeDbName === "ibc") {
7085
startBlock = await getBlockByTimestamp(startTimestamp, nChain as Chain, adapter, "First");
7186
if (!startBlock) {
72-
console.error(`Could not find start block for ${chain} on ${bridgeDbName}`);
87+
console.error(`[${bridgeDbName}] Could not find start block for ${chain}`);
7388
return;
7489
}
7590
endBlock = await getBlockByTimestamp(endTimestamp, nChain as Chain, adapter, "Last");
7691
if (!endBlock) {
77-
console.error(`Could not find end block for ${chain} on ${bridgeDbName}`);
92+
console.error(`[${bridgeDbName}] Could not find end block for ${chain}`);
7893
return;
7994
}
8095
} else {
@@ -85,12 +100,14 @@ async function fillAdapterHistorical(
85100
startBlock: startBlock.block,
86101
endBlock: endBlock.block,
87102
};
103+
console.log(`[${bridgeDbName}] ${nChain} blocks: ${startBlock.block} -> ${endBlock.block}`);
88104
} else {
89105
startBlock = blockByChain[nChain].startBlock;
90106
endBlock = blockByChain[nChain].endBlock;
91107
}
92108
}
93109

110+
console.log(`[${bridgeDbName}] Running ${chain.toLowerCase()} (${nChain}) blocks ${startBlock.block}-${endBlock.block}`);
94111
await runAdapterHistorical(
95112
startBlock.block,
96113
endBlock.block,
@@ -100,17 +117,19 @@ async function fillAdapterHistorical(
100117
false,
101118
"upsert"
102119
);
120+
console.log(`[${bridgeDbName}] Done ${chain.toLowerCase()} blocks ${startBlock.block}-${endBlock.block}`);
103121
})
104122
);
105123
await promises;
106-
console.log(`Finished running adapter from ${startTimestamp} to ${endTimestamp} for ${bridgeDbName}`);
124+
console.log(`[${bridgeDbName}] Finished ${startDate} to ${endDate}`);
107125
}
108126

109127
const runAllAdaptersHistorical = async (startTimestamp: number, endTimestamp: number) => {
110128
try {
111129
await PromisePool.withConcurrency(20)
112130
.for(bridgeNetworkData.reverse())
113131
.process(async (adapter) => {
132+
if (shuttingDown) return;
114133
await fillAdapterHistorical(startTimestamp, endTimestamp, adapter.bridgeDbName);
115134
});
116135
} finally {
@@ -119,14 +138,27 @@ const runAllAdaptersHistorical = async (startTimestamp: number, endTimestamp: nu
119138
};
120139

121140
(async () => {
122-
if (bridgeName) {
123-
const dailyIntervals = splitIntoDailyIntervals(startTs, endTs);
124-
await PromisePool.withConcurrency(60)
125-
.for(dailyIntervals.reverse())
126-
.process(async (interval) => {
127-
await fillAdapterHistorical(interval.start, interval.end, bridgeName, chain);
128-
});
129-
} else {
130-
await runAllAdaptersHistorical(startTs, endTs);
141+
try {
142+
if (bridgeName) {
143+
const dailyIntervals = splitIntoDailyIntervals(startTs, endTs).reverse();
144+
if (sequential) {
145+
for (const interval of dailyIntervals) {
146+
if (shuttingDown) break;
147+
await fillAdapterHistorical(interval.start, interval.end, bridgeName, chain);
148+
}
149+
} else {
150+
await PromisePool.withConcurrency(60)
151+
.for(dailyIntervals)
152+
.process(async (interval) => {
153+
if (shuttingDown) return;
154+
await fillAdapterHistorical(interval.start, interval.end, bridgeName, chain);
155+
});
156+
}
157+
} else {
158+
await runAllAdaptersHistorical(startTs, endTs);
159+
}
160+
} finally {
161+
if (shuttingDown) console.log("Shutdown complete.");
162+
await sql.end();
131163
}
132164
})();

0 commit comments

Comments
 (0)