From a89064824a97988b31e18bae6d9c5e99fd545e33 Mon Sep 17 00:00:00 2001 From: Sergey Timoshin Date: Sat, 11 May 2024 21:12:37 +0100 Subject: [PATCH] feat: add custom database URL option to cli Added a feature to pass a custom database URL for the indexer. --- cli/src/commands/test-validator/index.ts | 7 +++++++ cli/src/utils/initTestEnv.ts | 4 +++- cli/src/utils/process.ts | 16 ++++++++++++++-- cli/src/utils/processPhotonIndexer.ts | 11 +++++++++-- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/cli/src/commands/test-validator/index.ts b/cli/src/commands/test-validator/index.ts index 7be00f086b..4914039d75 100644 --- a/cli/src/commands/test-validator/index.ts +++ b/cli/src/commands/test-validator/index.ts @@ -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() { @@ -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"); diff --git a/cli/src/utils/initTestEnv.ts b/cli/src/utils/initTestEnv.ts index 74fb43578a..9c5dc576f5 100644 --- a/cli/src/utils/initTestEnv.ts +++ b/cli/src/utils/initTestEnv.ts @@ -25,6 +25,7 @@ export async function initTestEnv({ proveCompressedAccounts = true, proveNewAddresses = false, checkPhotonVersion = true, + photonDatabaseUrl, }: { additionalPrograms?: { address: string; path: string }[]; skipSystemAccounts?: boolean; @@ -33,6 +34,7 @@ export async function initTestEnv({ proveCompressedAccounts?: boolean; proveNewAddresses?: boolean; checkPhotonVersion?: boolean; + photonDatabaseUrl?: string; }) { console.log("Performing setup tasks...\n"); @@ -52,7 +54,7 @@ export async function initTestEnv({ await initAccounts(); if (indexer) { - await startIndexer(checkPhotonVersion); + await startIndexer(checkPhotonVersion, photonDatabaseUrl); } if (prover) { diff --git a/cli/src/utils/process.ts b/cli/src/utils/process.ts index c67f84d3e8..61af77fdd9 100644 --- a/cli/src/utils/process.ts +++ b/cli/src/utils/process.ts @@ -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) { @@ -116,12 +119,21 @@ export async function execute(command: string): Promise { 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) => { diff --git a/cli/src/utils/processPhotonIndexer.ts b/cli/src/utils/processPhotonIndexer.ts index 5b9fbbed69..0c65eb984c 100644 --- a/cli/src/utils/processPhotonIndexer.ts +++ b/cli/src/utils/processPhotonIndexer.ts @@ -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 ( @@ -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!"); }