Skip to content

Commit 57647c4

Browse files
authored
refactor: avoid recursive calls (#1001)
1 parent 53a854a commit 57647c4

File tree

3 files changed

+39
-34
lines changed

3 files changed

+39
-34
lines changed

src/graphql/operations/options.ts

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,18 @@ async function loadOptions() {
1616
}
1717

1818
async function run() {
19-
try {
20-
log.info('[options] Start options refresh');
21-
await loadOptions();
22-
log.info(`[options] ${options.length} options reloaded`);
23-
log.info('[options] End options refresh');
24-
} catch (e: any) {
25-
capture(e);
26-
log.error(`[options] failed to refresh options, ${JSON.stringify(e)}`);
19+
while (true) {
20+
try {
21+
log.info('[options] Start options refresh');
22+
await loadOptions();
23+
log.info(`[options] ${options.length} options reloaded`);
24+
log.info('[options] End options refresh');
25+
} catch (e: any) {
26+
capture(e);
27+
log.error(`[options] failed to refresh options, ${JSON.stringify(e)}`);
28+
}
29+
await snapshot.utils.sleep(RUN_INTERVAL);
2730
}
28-
await snapshot.utils.sleep(RUN_INTERVAL);
29-
run();
3031
}
3132

3233
run();

src/helpers/spaces.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -323,19 +323,19 @@ export async function getSpace(id: string) {
323323
}
324324

325325
export default async function run() {
326-
try {
327-
log.info('[spaces] Start spaces refresh');
328-
329-
await loadSpaces();
330-
await loadSpacesMetrics();
331-
332-
sortSpaces();
333-
log.info('[spaces] End spaces refresh');
334-
} catch (e: any) {
335-
capture(e);
336-
log.error(`[spaces] failed to load spaces, ${JSON.stringify(e)}`);
326+
while (true) {
327+
try {
328+
log.info('[spaces] Start spaces refresh');
329+
330+
await loadSpaces();
331+
await loadSpacesMetrics();
332+
333+
sortSpaces();
334+
log.info('[spaces] End spaces refresh');
335+
} catch (e: any) {
336+
capture(e);
337+
log.error(`[spaces] failed to load spaces, ${JSON.stringify(e)}`);
338+
}
339+
await snapshot.utils.sleep(RUN_INTERVAL);
337340
}
338-
await snapshot.utils.sleep(RUN_INTERVAL);
339-
340-
run();
341341
}

src/helpers/strategies.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { spaces } from './spaces';
77
export let strategies: any[] = [];
88
export let strategiesObj: any = {};
99

10+
const RUN_INTERVAL = 60e3;
1011
const scoreApiURL: URL = new URL(
1112
process.env.SCORE_API_URL ?? 'https://score.snapshot.org'
1213
);
@@ -56,19 +57,22 @@ async function loadStrategies() {
5657
}
5758

5859
async function run() {
59-
try {
60-
await loadStrategies();
61-
consecutiveFailsCount = 0;
62-
} catch (e: any) {
63-
consecutiveFailsCount++;
60+
while (true) {
61+
try {
62+
log.info('[strategies] Start strategies refresh');
63+
await loadStrategies();
64+
consecutiveFailsCount = 0;
65+
log.info('[strategies] End strategies refresh');
66+
} catch (e: any) {
67+
consecutiveFailsCount++;
6468

65-
if (consecutiveFailsCount >= 3) {
66-
capture(e);
69+
if (consecutiveFailsCount >= 3) {
70+
capture(e);
71+
}
72+
log.error(`[strategies] failed to load ${JSON.stringify(e)}`);
6773
}
68-
log.error(`[strategies] failed to load ${JSON.stringify(e)}`);
74+
await snapshot.utils.sleep(RUN_INTERVAL);
6975
}
70-
await snapshot.utils.sleep(60e3);
71-
run();
7276
}
7377

7478
run();

0 commit comments

Comments
 (0)