|
5 | 5 | getProcessedRuntimeConfig, |
6 | 6 | getRuntimeDataFromEnv, |
7 | 7 | } from "apibara/common"; |
| 8 | +import { ReloadIndexerRequest } from "apibara/core"; |
8 | 9 | import { defineCommand, runMain } from "citty"; |
9 | | -import consola from "consola"; |
| 10 | +import consola, { type ConsolaInstance } from "consola"; |
10 | 11 | import { blueBright } from "picocolors"; |
11 | 12 | import { register } from "#apibara-internal-virtual/instrumentation"; |
12 | 13 | // used when running with node .apibara/build/start.mjs as these values are made static during build time (except userEnvRuntimeConfig) |
@@ -44,54 +45,72 @@ const startCommand = defineCommand({ |
44 | 45 | const { indexer, preset: argPreset, standalone } = args; |
45 | 46 | await checkForUnknownArgs(args, cmd); |
46 | 47 |
|
47 | | - let preset: string | undefined; |
48 | | - let processedRuntimeConfig: Record<string, unknown> | undefined; |
| 48 | + let _logger: ConsolaInstance | undefined; |
49 | 49 |
|
50 | | - if (standalone) { |
51 | | - // when user does node .apibara/build/start.mjs |
52 | | - preset = argPreset ?? originalPreset; |
53 | | - processedRuntimeConfig = getProcessedRuntimeConfig({ |
54 | | - preset, |
55 | | - presets, |
56 | | - runtimeConfig, |
57 | | - userEnvRuntimeConfig, |
58 | | - }); |
59 | | - } else { |
60 | | - // When user does apibara start |
61 | | - const envResult = getRuntimeDataFromEnv(); |
62 | | - preset = envResult.preset; |
63 | | - processedRuntimeConfig = envResult.processedRuntimeConfig; |
64 | | - } |
| 50 | + while (true) { |
| 51 | + try { |
| 52 | + let preset: string | undefined; |
| 53 | + let processedRuntimeConfig: Record<string, unknown> | undefined; |
65 | 54 |
|
66 | | - const { indexer: indexerInstance, logger } = |
67 | | - (await createIndexer({ |
68 | | - indexerName: indexer, |
69 | | - processedRuntimeConfig, |
70 | | - preset, |
71 | | - })) ?? {}; |
| 55 | + if (standalone) { |
| 56 | + // when user does node .apibara/build/start.mjs |
| 57 | + preset = argPreset ?? originalPreset; |
| 58 | + processedRuntimeConfig = getProcessedRuntimeConfig({ |
| 59 | + preset, |
| 60 | + presets, |
| 61 | + runtimeConfig, |
| 62 | + userEnvRuntimeConfig, |
| 63 | + }); |
| 64 | + } else { |
| 65 | + // When user does apibara start |
| 66 | + const envResult = getRuntimeDataFromEnv(); |
| 67 | + preset = envResult.preset; |
| 68 | + processedRuntimeConfig = envResult.processedRuntimeConfig; |
| 69 | + } |
72 | 70 |
|
73 | | - if (!indexerInstance) { |
74 | | - consola.error(`Specified indexer "${indexer}" but it was not defined`); |
75 | | - process.exit(1); |
76 | | - } |
| 71 | + const { indexer: indexerInstance, logger } = |
| 72 | + (await createIndexer({ |
| 73 | + indexerName: indexer, |
| 74 | + processedRuntimeConfig, |
| 75 | + preset, |
| 76 | + })) ?? {}; |
77 | 77 |
|
78 | | - const client = createAuthenticatedClient( |
79 | | - indexerInstance.streamConfig, |
80 | | - indexerInstance.options.streamUrl, |
81 | | - indexerInstance.options.clientOptions, |
82 | | - ); |
| 78 | + _logger = logger; |
83 | 79 |
|
84 | | - if (register) { |
85 | | - consola.start("Registering from instrumentation"); |
86 | | - await register(); |
87 | | - consola.success("Registered from instrumentation"); |
88 | | - } |
| 80 | + if (!indexerInstance) { |
| 81 | + consola.error( |
| 82 | + `Specified indexer "${indexer}" but it was not defined`, |
| 83 | + ); |
| 84 | + process.exit(1); |
| 85 | + } |
89 | 86 |
|
90 | | - if (logger) { |
91 | | - logger.info(`Indexer ${blueBright(indexer)} started`); |
92 | | - } |
| 87 | + const client = createAuthenticatedClient( |
| 88 | + indexerInstance.streamConfig, |
| 89 | + indexerInstance.options.streamUrl, |
| 90 | + indexerInstance.options.clientOptions, |
| 91 | + ); |
93 | 92 |
|
94 | | - await runWithReconnect(client, indexerInstance); |
| 93 | + if (register) { |
| 94 | + consola.start("Registering from instrumentation"); |
| 95 | + await register(); |
| 96 | + consola.success("Registered from instrumentation"); |
| 97 | + } |
| 98 | + |
| 99 | + if (logger) { |
| 100 | + logger.info(`Indexer ${blueBright(indexer)} started`); |
| 101 | + } |
| 102 | + |
| 103 | + await runWithReconnect(client, indexerInstance); |
| 104 | + |
| 105 | + return; |
| 106 | + } catch (error) { |
| 107 | + if (error instanceof ReloadIndexerRequest) { |
| 108 | + _logger?.info(`Indexer ${blueBright(indexer)} reloaded`); |
| 109 | + continue; |
| 110 | + } |
| 111 | + throw error; |
| 112 | + } |
| 113 | + } |
95 | 114 | }, |
96 | 115 | }); |
97 | 116 |
|
|
0 commit comments