Skip to content

Commit 11f564f

Browse files
committed
cli: add reloadIndexer() helper
1 parent 060f307 commit 11f564f

8 files changed

Lines changed: 133 additions & 72 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "plugin-drizzle: forward original error",
4+
"packageName": "@apibara/plugin-drizzle",
5+
"email": "jadejajaipal5@gmail.com",
6+
"dependentChangeType": "patch"
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"type": "prerelease",
3+
"comment": "cli: add reloadIndexer() and async support for indexer constructors",
4+
"packageName": "apibara",
5+
"email": "jadejajaipal5@gmail.com",
6+
"dependentChangeType": "patch"
7+
}

packages/cli/src/core/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ export { createApibara } from "./apibara";
22
export { build } from "./build/build";
33
export { prepare } from "./build/prepare";
44
export { writeTypes } from "./build/types";
5+
export { ReloadIndexerRequest, reloadIndexer } from "./utils";

packages/cli/src/core/utils.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export function reloadIndexer() {
2+
throw new ReloadIndexerRequest();
3+
}
4+
5+
export class ReloadIndexerRequest extends Error {
6+
constructor(message?: string) {
7+
super(message);
8+
this.name = "ReloadIndexerRequest";
9+
}
10+
}

packages/cli/src/rolldown/config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ const runtimeDependencies = [
1919
// https://socket.io/docs/v4/server-installation/#additional-packages
2020
"utf-8-validate",
2121
"bufferutil",
22+
"fsevents",
2223
// was giving unresolved import warnings from `node-fetch` library.
2324
"encoding",
2425
];

packages/cli/src/runtime/dev.ts

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,54 @@
11
import { runWithReconnect } from "@apibara/indexer";
22
import { createAuthenticatedClient } from "@apibara/protocol";
33
import { getRuntimeDataFromEnv } from "apibara/common";
4+
import { ReloadIndexerRequest } from "apibara/core";
45
import { defineCommand, runMain } from "citty";
6+
import type { ConsolaInstance } from "consola";
57
import { blueBright } from "picocolors";
68
import { availableIndexers, createIndexer } from "./internal/app";
79

10+
async function startIndexer(indexer: string) {
11+
let _logger: ConsolaInstance | undefined;
12+
while (true) {
13+
try {
14+
const { processedRuntimeConfig, preset } = getRuntimeDataFromEnv();
15+
16+
const { indexer: indexerInstance, logger } =
17+
(await createIndexer({
18+
indexerName: indexer,
19+
processedRuntimeConfig,
20+
preset,
21+
})) ?? {};
22+
23+
_logger = logger;
24+
25+
if (!indexerInstance) {
26+
return;
27+
}
28+
29+
const client = createAuthenticatedClient(
30+
indexerInstance.streamConfig,
31+
indexerInstance.options.streamUrl,
32+
indexerInstance.options.clientOptions,
33+
);
34+
35+
if (logger) {
36+
logger.info(`Indexer ${blueBright(indexer)} started`);
37+
}
38+
39+
await runWithReconnect(client, indexerInstance);
40+
41+
return;
42+
} catch (error) {
43+
if (error instanceof ReloadIndexerRequest) {
44+
_logger?.info(`Indexer ${blueBright(indexer)} reloaded`);
45+
continue;
46+
}
47+
throw error;
48+
}
49+
}
50+
}
51+
852
const startCommand = defineCommand({
953
meta: {
1054
name: "start",
@@ -19,8 +63,6 @@ const startCommand = defineCommand({
1963
async run({ args }) {
2064
const { indexers: indexersArgs } = args;
2165

22-
const { processedRuntimeConfig, preset } = getRuntimeDataFromEnv();
23-
2466
let selectedIndexers = availableIndexers;
2567
if (indexersArgs) {
2668
selectedIndexers = indexersArgs.split(",");
@@ -34,31 +76,7 @@ const startCommand = defineCommand({
3476
}
3577
}
3678

37-
await Promise.all(
38-
selectedIndexers.map(async (indexer) => {
39-
const { indexer: indexerInstance, logger } =
40-
(await createIndexer({
41-
indexerName: indexer,
42-
processedRuntimeConfig,
43-
preset,
44-
})) ?? {};
45-
if (!indexerInstance) {
46-
return;
47-
}
48-
49-
const client = createAuthenticatedClient(
50-
indexerInstance.streamConfig,
51-
indexerInstance.options.streamUrl,
52-
indexerInstance.options.clientOptions,
53-
);
54-
55-
if (logger) {
56-
logger.info(`Indexer ${blueBright(indexer)} started`);
57-
}
58-
59-
await runWithReconnect(client, indexerInstance);
60-
}),
61-
);
79+
await Promise.all(selectedIndexers.map((indexer) => startIndexer(indexer)));
6280
},
6381
});
6482

packages/cli/src/runtime/start.ts

Lines changed: 61 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import {
55
getProcessedRuntimeConfig,
66
getRuntimeDataFromEnv,
77
} from "apibara/common";
8+
import { ReloadIndexerRequest } from "apibara/core";
89
import { defineCommand, runMain } from "citty";
9-
import consola from "consola";
10+
import consola, { type ConsolaInstance } from "consola";
1011
import { blueBright } from "picocolors";
1112
import { register } from "#apibara-internal-virtual/instrumentation";
1213
// 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({
4445
const { indexer, preset: argPreset, standalone } = args;
4546
await checkForUnknownArgs(args, cmd);
4647

47-
let preset: string | undefined;
48-
let processedRuntimeConfig: Record<string, unknown> | undefined;
48+
let _logger: ConsolaInstance | undefined;
4949

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;
6554

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+
}
7270

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+
})) ?? {};
7777

78-
const client = createAuthenticatedClient(
79-
indexerInstance.streamConfig,
80-
indexerInstance.options.streamUrl,
81-
indexerInstance.options.clientOptions,
82-
);
78+
_logger = logger;
8379

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+
}
8986

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+
);
9392

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+
}
95114
},
96115
});
97116

packages/plugin-drizzle/src/index.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,7 @@ export function drizzleStorage<
408408
} catch (error) {
409409
await removeTriggers(db, tableNames, indexerId);
410410

411-
throw new DrizzleStorageError("Failed to run handler:middleware", {
412-
cause: error,
413-
});
411+
throw error;
414412
}
415413
});
416414
});

0 commit comments

Comments
 (0)