Skip to content

Commit

Permalink
feat: add custom database URL option to cli
Browse files Browse the repository at this point in the history
Added a feature to pass a custom database URL for the indexer.
  • Loading branch information
sergeytimoshin committed May 11, 2024
1 parent 6229615 commit a890648
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 5 deletions.
7 changes: 7 additions & 0 deletions cli/src/commands/test-validator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ class SetupCommand extends Command {
default: false,
exclusive: ["skip-indexer"],
}),
"indexer-db-url": Flags.string({
description:
"Custom indexer database URL to store indexing data. By default we use an in-memory SQLite database.",
required: false,
exclusive: ["skip-indexer"],
}),
};

async run() {
Expand All @@ -54,6 +60,7 @@ class SetupCommand extends Command {
proveCompressedAccounts: flags["prove-compressed-accounts"],
proveNewAddresses: flags["prove-new-addresses"],
checkPhotonVersion: !flags["relax-indexer-version-constraint"],
photonDatabaseUrl: flags["indexer-db-url"],
});

this.log("\nSetup tasks completed successfully \x1b[32m✔\x1b[0m");
Expand Down
4 changes: 3 additions & 1 deletion cli/src/utils/initTestEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export async function initTestEnv({
proveCompressedAccounts = true,
proveNewAddresses = false,
checkPhotonVersion = true,
photonDatabaseUrl,
}: {
additionalPrograms?: { address: string; path: string }[];
skipSystemAccounts?: boolean;
Expand All @@ -33,6 +34,7 @@ export async function initTestEnv({
proveCompressedAccounts?: boolean;
proveNewAddresses?: boolean;
checkPhotonVersion?: boolean;
photonDatabaseUrl?: string;
}) {
console.log("Performing setup tasks...\n");

Expand All @@ -52,7 +54,7 @@ export async function initTestEnv({
await initAccounts();

if (indexer) {
await startIndexer(checkPhotonVersion);
await startIndexer(checkPhotonVersion, photonDatabaseUrl);
}

if (prover) {
Expand Down
16 changes: 14 additions & 2 deletions cli/src/utils/process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ const waitOn = require("wait-on");

export async function killProcess(processName: string) {
const processList = await find("name", processName);
for (const proc of processList) {
const targetProcesses = processList.filter(
(proc) => proc.cmd.split(" ")[0] === processName,
);
targetProcesses.forEach((proc) => {
process.kill(proc.pid);
}
});
}

export async function killProcessByPort(port: string) {
Expand Down Expand Up @@ -116,12 +119,21 @@ export async function execute(command: string): Promise<string> {
export function spawnBinary(command: string, args: string[] = []) {
const logDir = "test-ledger";
const binaryName = path.basename(command);
console.log("Starting binary...", command, args);
console.log("binaryName", binaryName);

const dir = path.join(__dirname, logDir);
if (!fs.existsSync(dir)) {
fs.mkdirSync(dir);
}

const out = fs.openSync(`${logDir}/${binaryName}.log`, "a");
const err = fs.openSync(`${logDir}/${binaryName}.log`, "a");

const spawnedProcess = spawn(command, args, {
stdio: ["ignore", out, err],
shell: false,
detached: true,
});

spawnedProcess.on("close", (code) => {
Expand Down
11 changes: 9 additions & 2 deletions cli/src/utils/processPhotonIndexer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ import { INDEXER_PROCESS_NAME, PHOTON_VERSION } from "./constants";
import { exec } from "node:child_process";
import * as util from "node:util";

export async function startIndexer(checkPhotonVersion: boolean = true) {
export async function startIndexer(
checkPhotonVersion: boolean = true,
photonDatabaseUrl?: string,
) {
await killIndexer();
const resolvedOrNull = which.sync("photon", { nothrow: true });
if (
Expand All @@ -16,7 +19,11 @@ export async function startIndexer(checkPhotonVersion: boolean = true) {
throw new Error(message);
} else {
console.log("Starting indexer...");
spawnBinary(INDEXER_PROCESS_NAME);
let args: string[] = [];
if (photonDatabaseUrl) {
args = ["--db-url", photonDatabaseUrl];
}
spawnBinary(INDEXER_PROCESS_NAME, args);
await waitForServers([{ port: 8784, path: "/getIndexerHealth" }]);
console.log("Indexer started successfully!");
}
Expand Down

0 comments on commit a890648

Please sign in to comment.