From 5cadece48aeef12ba64d68781332c6590460bf17 Mon Sep 17 00:00:00 2001 From: Rinse Date: Mon, 13 Nov 2023 17:15:03 +0000 Subject: [PATCH] fix(daemon): fix rpc import problem with daemon command --- src/api/server.ts | 46 -------------------------------------- src/api/types.ts | 0 src/cli/commands/daemon.ts | 43 ++++++++++++++++++++++++++++------- 3 files changed, 35 insertions(+), 54 deletions(-) delete mode 100644 src/api/server.ts delete mode 100644 src/api/types.ts diff --git a/src/api/server.ts b/src/api/server.ts deleted file mode 100644 index 5e64192..0000000 --- a/src/api/server.ts +++ /dev/null @@ -1,46 +0,0 @@ -import { PlebbitWsServer } from "@plebbit/plebbit-js/rpc"; -import assert from "assert"; -import { seedSubplebbits } from "../seeder.js"; -import Logger from "@plebbit/plebbit-logger"; -import path from "path"; - -export async function startRpcServer( - plebbitRpcApiPort: number, - ipfsApiEndpoint: string, - ipfsGatewayEndpoint: string, - plebbitDataPath: string, - seedSubs: string[] | undefined -) { - const log = Logger("plebbit-cli:rpcServer"); - - // Run plebbit RPC server here - - const rpcServer = await PlebbitWsServer({ - port: plebbitRpcApiPort, - plebbitOptions: { - ipfsHttpClientsOptions: [ipfsApiEndpoint], - dataPath: plebbitDataPath - } - }); - - const handleExit = async (signal: NodeJS.Signals) => { - log(`in handle exit (${signal})`); - await rpcServer.destroy(); - process.exit(); - }; - - ["SIGINT", "SIGTERM", "SIGHUP", "beforeExit"].forEach((exitSignal) => process.on(exitSignal, handleExit)); - - console.log(`IPFS API listening on: ${ipfsApiEndpoint}`); - console.log(`IPFS Gateway listening on: ${ipfsGatewayEndpoint}`); - console.log(`Plebbit RPC API listening on: ws://localhost:${plebbitRpcApiPort}`); - console.log(`Plebbit data path: ${path.resolve(rpcServer.plebbit.dataPath)}`); - if (Array.isArray(seedSubs)) { - const seedSubsLoop = () => { - // I think calling setTimeout constantly here will overflow memory. Need to check later - seedSubplebbits(seedSubs, rpcServer.plebbit).then(() => setTimeout(seedSubsLoop, 600000)); // Seed subs every 10 minutes - }; - console.log(`Seeding subplebbits:`, seedSubs); - seedSubsLoop(); - } -} diff --git a/src/api/types.ts b/src/api/types.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/cli/commands/daemon.ts b/src/cli/commands/daemon.ts index 94f4a0d..a140212 100644 --- a/src/cli/commands/daemon.ts +++ b/src/cli/commands/daemon.ts @@ -1,11 +1,14 @@ import { Flags, Command } from "@oclif/core"; import Logger from "@plebbit/plebbit-logger"; import { ChildProcessWithoutNullStreams } from "child_process"; -import { startRpcServer } from "../../api/server.js"; +import { seedSubplebbits } from "../../seeder"; + import defaults from "../../common-utils/defaults.js"; import { startIpfsNode } from "../../ipfs/startIpfs.js"; +import { PlebbitWsServer } from "@plebbit/plebbit-js/dist/node/rpc/src/index"; import lodash from "lodash"; import fetch from "node-fetch"; +import path from "path"; export default class Daemon extends Command { static override description = @@ -91,12 +94,36 @@ export default class Daemon extends Command { await keepIpfsUp(); process.on("exit", () => (mainProcessExited = true) && process.kill(ipfsProcess.pid)); - await startRpcServer( - flags.plebbitRpcApiPort, - `http://localhost:${flags.ipfsApiPort}/api/v0`, - `http://localhost:${flags.ipfsGatewayPort}`, - flags.plebbitDataPath, - subsToSeed - ); + + const ipfsApiEndpoint = `http://localhost:${flags.ipfsApiPort}/api/v0`; + const ipfsGatewayEndpoint = `http://localhost:${flags.ipfsGatewayPort}`; + const rpcServer = await PlebbitWsServer({ + port: flags.plebbitRpcApiPort, + plebbitOptions: { + ipfsHttpClientsOptions: [ipfsApiEndpoint], + dataPath: flags.plebbitDataPath + } + }); + + const handleExit = async (signal: NodeJS.Signals) => { + log(`in handle exit (${signal})`); + await rpcServer.destroy(); + process.exit(); + }; + + ["SIGINT", "SIGTERM", "SIGHUP", "beforeExit"].forEach((exitSignal) => process.on(exitSignal, handleExit)); + + console.log(`IPFS API listening on: ${ipfsApiEndpoint}`); + console.log(`IPFS Gateway listening on: ${ipfsGatewayEndpoint}`); + console.log(`Plebbit RPC API listening on: ws://localhost:${flags.plebbitRpcApiPort}`); + console.log(`Plebbit data path: ${path.resolve(rpcServer.plebbit.dataPath)}`); + if (Array.isArray(subsToSeed)) { + const seedSubsLoop = () => { + // I think calling setTimeout constantly here will overflow memory. Need to check later + seedSubplebbits(subsToSeed, rpcServer.plebbit).then(() => setTimeout(seedSubsLoop, 600000)); // Seed subs every 10 minutes + }; + console.log(`Seeding subplebbits:`, subsToSeed); + seedSubsLoop(); + } } }