Skip to content

Commit

Permalink
api: bundle stream-info-service (#1184)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
iameli and victorges authored Jul 23, 2023
1 parent f47ffae commit f13a420
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 66 deletions.
44 changes: 44 additions & 0 deletions packages/api/src/app/stream-info/parse-cli.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import "source-map-support/register";
import yargs from "yargs";
import { CamelKeys } from "../../types/common";

export type CliArgs = ReturnType<typeof parseCli>;

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<typeof parsed>;
}
11 changes: 7 additions & 4 deletions packages/api/src/app/stream-info/stream-info-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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();
});

Expand Down Expand Up @@ -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" });
Expand All @@ -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) => {
Expand Down
9 changes: 0 additions & 9 deletions packages/api/src/cli.js

This file was deleted.

15 changes: 15 additions & 0 deletions packages/api/src/cli.ts
Original file line number Diff line number Diff line change
@@ -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);
}
}
10 changes: 10 additions & 0 deletions packages/api/src/parse-cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
`
Expand Down
56 changes: 3 additions & 53 deletions packages/api/src/stream-info-service.ts
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit f13a420

Please sign in to comment.