From f13a420d46d182f8900590c50300ee20a34fe047 Mon Sep 17 00:00:00 2001 From: Eli Mallon Date: Sun, 23 Jul 2023 13:17:10 -0700 Subject: [PATCH] api: bundle stream-info-service (#1184) * api: bundle stream-info-service * api/stream-info: Move parse-cli to a separate file * api: Add stream-info-service arg to parseCli directly - The host field in the stream-info-service was never used - The broadcaster field was missing in the API, added with the disclaimer This introduces a small tech debt with the API CLI args containing something not relevant to it (broadcaster), but I think it's ok for now especially if we get rid of stream-info-service for good soon. * stream-info-service: identify yourself --------- Co-authored-by: Victor Elias --- packages/api/src/app/stream-info/parse-cli.ts | 44 +++++++++++++++ .../src/app/stream-info/stream-info-app.ts | 11 ++-- packages/api/src/cli.js | 9 --- packages/api/src/cli.ts | 15 +++++ packages/api/src/parse-cli.ts | 10 ++++ packages/api/src/stream-info-service.ts | 56 +------------------ 6 files changed, 79 insertions(+), 66 deletions(-) create mode 100644 packages/api/src/app/stream-info/parse-cli.ts delete mode 100755 packages/api/src/cli.js create mode 100755 packages/api/src/cli.ts diff --git a/packages/api/src/app/stream-info/parse-cli.ts b/packages/api/src/app/stream-info/parse-cli.ts new file mode 100644 index 0000000000..0484b99c53 --- /dev/null +++ b/packages/api/src/app/stream-info/parse-cli.ts @@ -0,0 +1,44 @@ +import "source-map-support/register"; +import yargs from "yargs"; +import { CamelKeys } from "../../types/common"; + +export type CliArgs = ReturnType; + +export function parseCli() { + const parsed = yargs + .usage( + ` + Livepeer Stream Info fetcher + + Options my also be provided as LP_API_ prefixed environment variables, e.g. LP_API_PORT=5000 is the same as --port=5000. + + ` + ) + .env("LP_API_") + //.strict(true) + .options({ + port: { + describe: "port to listen on", + default: 3010, + demandOption: true, + type: "number", + }, + "own-region": { + describe: "identify region in which this service runs (fra, mdw, etc)", + type: "string", + }, + broadcaster: { + describe: "broadcaster host:port to fetch info from", + type: "string", + default: "localhost:7935", + }, + "postgres-url": { + describe: "url of a postgres database", + type: "string", + demandOption: true, + }, + }) + .help() + .parse(); + return parsed as any as CamelKeys; +} diff --git a/packages/api/src/app/stream-info/stream-info-app.ts b/packages/api/src/app/stream-info/stream-info-app.ts index 197ebd4820..64bf743bd6 100644 --- a/packages/api/src/app/stream-info/stream-info-app.ts +++ b/packages/api/src/app/stream-info/stream-info-app.ts @@ -14,13 +14,14 @@ import { MasterPlaylistDictionary, } from "./livepeer-types"; import { DBStream } from "../../store/stream-table"; +import { CliArgs } from "./parse-cli"; const pollInterval = 2 * 1000; // 2s const updateInterval = 50 * 1000; // 50s, slightly lower than USER_SESSION_TIMEOUT in API const deleteTimeout = 30 * 1000; // 30s const seenSegmentsTimeout = 2 * 60 * 1000; // 2m. should be at least two time longer than HTTP push timeout in go-livepeer -async function makeRouter(params) { +async function makeRouter(params: CliArgs) { const bodyParser = require("body-parser"); // Logging, JSON parsing, store injection @@ -29,7 +30,7 @@ async function makeRouter(params) { app.use(healthCheck); app.use(bodyParser.json()); app.use((req, res, next) => { - req.config = params; + req.config = params as any; next(); }); @@ -375,7 +376,7 @@ class statusPoller { } } -export default async function makeApp(params) { +export default async function makeApp(params: CliArgs) { const { port, postgresUrl, ownRegion, listen = true, broadcaster } = params; // Storage init await db.start({ postgresUrl, appName: "stream-info" }); @@ -393,7 +394,9 @@ export default async function makeApp(params) { listener = app .listen(port, () => { listenPort = listener.address().port; - logger.info(`API server listening on http://0.0.0.0:${listenPort}`); + logger.info( + `Stream info server listening on http://0.0.0.0:${listenPort}` + ); resolve(); }) .on("error", (err) => { diff --git a/packages/api/src/cli.js b/packages/api/src/cli.js deleted file mode 100755 index 1811ec6cad..0000000000 --- a/packages/api/src/cli.js +++ /dev/null @@ -1,9 +0,0 @@ -#!/usr/bin/env node - -import "./dotenv"; -import makeApp from "./index"; -import parseCli from "./parse-cli"; - -if (!module.parent) { - makeApp(parseCli()); -} diff --git a/packages/api/src/cli.ts b/packages/api/src/cli.ts new file mode 100755 index 0000000000..eb08096ed4 --- /dev/null +++ b/packages/api/src/cli.ts @@ -0,0 +1,15 @@ +#!/usr/bin/env node + +import "./dotenv"; +import makeApp from "./index"; +import makeStreamInfoSvc from "./app/stream-info/stream-info-app"; +import parseCli from "./parse-cli"; + +if (require.main === module) { + const args = parseCli(); + if (args.streamInfoService) { + makeStreamInfoSvc(args); + } else { + makeApp(args); + } +} diff --git a/packages/api/src/parse-cli.ts b/packages/api/src/parse-cli.ts index 81f9f90767..855db95d52 100755 --- a/packages/api/src/parse-cli.ts +++ b/packages/api/src/parse-cli.ts @@ -427,6 +427,16 @@ export default function parseCli(argv?: string | readonly string[]) { type: "boolean", default: true, }, + "stream-info-service": { + describe: "start the Stream Info service instead of Studio API", + type: "boolean", + }, + broadcaster: { + describe: + "stream-info-service: broadcaster host:port to fetch info from", + type: "string", + default: "localhost:7935", + }, }) .usage( ` diff --git a/packages/api/src/stream-info-service.ts b/packages/api/src/stream-info-service.ts index 38425cfb7d..74ecced842 100644 --- a/packages/api/src/stream-info-service.ts +++ b/packages/api/src/stream-info-service.ts @@ -1,58 +1,8 @@ +import "./dotenv"; import "source-map-support/register"; import makeApp from "./app/stream-info/stream-info-app"; -import yargs from "yargs"; -import path from "path"; -import os from "os"; +import { parseCli } from "./app/stream-info/parse-cli"; -function parseCli() { - return ( - yargs - .usage( - ` - Livepeer Stream Info fetcher - - Options my also be provided as LP_API_ prefixed environment variables, e.g. LP_API_PORT=5000 is the same as --port=5000. - - ` - ) - .env("LP_API_") - //.strict(true) - .options({ - port: { - describe: "port to listen on", - default: 3010, - demandOption: true, - type: "number", - }, - host: { - describe: "host to bind to", - type: "string", - default: "localhost", - }, - "own-region": { - describe: - "identify region in which this service runs (fra, mdw, etc)", - type: "string", - }, - broadcaster: { - describe: "broadcaster host:port to fetch info from", - type: "string", - default: "127.0.0.1:7935", - }, - "postgres-url": { - describe: "url of a postgres database", - type: "string", - demandOption: true, - }, - }) - .help() - .parse() - ); -} - -function main() { - require("dotenv").config(); +if (require.main === module) { makeApp(parseCli()); } - -main();