Skip to content

Commit

Permalink
Configurable admin server port
Browse files Browse the repository at this point in the history
  • Loading branch information
qianl15 committed Oct 9, 2024
1 parent d577edd commit 9379583
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 8 deletions.
4 changes: 4 additions & 0 deletions dbos-config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@
},
"port": {
"type": "number"
},
"admin_port": {
"type": "number",
"description": "The port number of the admin server (Default: 3001)"
}
}
},
Expand Down
4 changes: 3 additions & 1 deletion src/dbos-runtime/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,9 +239,11 @@ export function parseConfigFile(cliOptions?: ParseOptions, useProxy: boolean = f
entrypoints.add(defaultEntryPoint);
}

const appPort = Number(cliOptions?.port) || Number(configFile.runtimeConfig?.port) || 3000;
const runtimeConfig: DBOSRuntimeConfig = {
entrypoints: [...entrypoints],
port: Number(cliOptions?.port) || Number(configFile.runtimeConfig?.port) || 3000,
port: appPort,
admin_port: Number(configFile.runtimeConfig?.admin_port) || appPort + 1,
};

return [dbosConfig, runtimeConfig];
Expand Down
3 changes: 2 additions & 1 deletion src/dbos-runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface ModuleExports {
export interface DBOSRuntimeConfig {
entrypoints: string[];
port: number;
admin_port: number;
}
export const defaultEntryPoint = "dist/operations.js";

Expand Down Expand Up @@ -50,7 +51,7 @@ export class DBOSRuntime {
await DBOSRuntime.loadClasses(this.runtimeConfig.entrypoints);
await this.dbosExec.init();
const server = new DBOSHttpServer(this.dbosExec);
this.servers = await server.listen(this.runtimeConfig.port);
this.servers = await server.listen(this.runtimeConfig.port, this.runtimeConfig.admin_port);
this.dbosExec.logRegisteredHTTPUrls();

this.scheduler = new DBOSScheduler(this.dbosExec);
Expand Down
3 changes: 1 addition & 2 deletions src/httpServer/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ export class DBOSHttpServer {
* Register HTTP endpoints and attach to the app. Then start the server at the given port.
* @param port
*/
async listen(port: number) {
async listen(port: number, adminPort: number) {
try {
await this.checkPortAvailability(port, "127.0.0.1");
} catch (error) {
Expand Down Expand Up @@ -93,7 +93,6 @@ export class DBOSHttpServer {
this.logger.info(`DBOS Server is running at http://localhost:${port}`);
});

const adminPort = port + 1
const adminServer = this.adminApp.listen(adminPort, () => {
this.logger.info(`DBOS Admin Server is running at http://localhost:${adminPort}`);
});
Expand Down
2 changes: 2 additions & 0 deletions tests/dbos-runtime/config.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,15 @@ describe("dbos-config", () => {
- a
- b
- b
admin_port: 2345
`;
jest.spyOn(utils, "readFileSync").mockReturnValue(mockDBOSConfigWithEntryPoints);

const [_, runtimeConfig]: [DBOSConfig, DBOSRuntimeConfig] = parseConfigFile(mockCLIOptions);

expect(runtimeConfig).toBeDefined();
expect(runtimeConfig?.port).toBe(1234);
expect(runtimeConfig?.admin_port).toBe(2345);
expect(runtimeConfig.entrypoints).toBeDefined();
expect(runtimeConfig.entrypoints).toBeInstanceOf(Array);
expect(runtimeConfig.entrypoints).toHaveLength(2);
Expand Down
16 changes: 12 additions & 4 deletions tests/dbos-runtime/runtime.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@ import { Writable } from "stream";
import { Client } from "pg";
import { generateDBOSTestConfig, setUpDBOSTestDb } from "../helpers";
import fs from "fs";
import { HealthUrl } from "../../src/httpServer/server";

async function waitForMessageTest(command: ChildProcess, port: string) {
async function waitForMessageTest(command: ChildProcess, port: string, adminPort?: string) {
const stdout = command.stdout as unknown as Writable;
const stdin = command.stdin as unknown as Writable;
const stderr = command.stderr as unknown as Writable;

if (!adminPort) {
adminPort = (Number(port) + 1).toString();
}

const waitForMessage = new Promise<void>((resolve, reject) => {
const onData = (data: Buffer) => {
const message = data.toString();
process.stdout.write(message);
if (message.includes("Server is running at")) {
if (message.includes("DBOS Admin Server is running at")) {
stdout.off("data", onData); // remove listener
resolve();
}
Expand All @@ -34,6 +39,8 @@ async function waitForMessageTest(command: ChildProcess, port: string) {
try {
const response = await axios.get(`http://127.0.0.1:${port}/greeting/dbos`);
expect(response.status).toBe(200);
const healthRes = await axios.get(`http://127.0.0.1:${adminPort}${HealthUrl}`);
expect(healthRes.status).toBe(200);
} catch (error) {
const errMsg = `Error sending test request: status: ${(error as AxiosError).response?.status}, statusText: ${(error as AxiosError).response?.statusText}`;
console.error(errMsg);
Expand Down Expand Up @@ -162,7 +169,7 @@ describe("runtime-tests", () => {
}
});

test("runtime hello with port provided in configuration file", async () => {
test("runtime hello with ports provided in configuration file", async () => {
const mockDBOSConfigYamlString = `
database:
hostname: 'localhost'
Expand All @@ -174,6 +181,7 @@ database:
app_db_client: 'knex'
runtimeConfig:
port: 6666
admin_port: 6789
`;
const filePath = "dbos-config.yaml";
fs.copyFileSync(filePath, `${filePath}.bak`);
Expand All @@ -183,7 +191,7 @@ runtimeConfig:
const command = spawn("node_modules/@dbos-inc/dbos-sdk/dist/src/dbos-runtime/cli.js", ["start"], {
env: process.env,
});
await waitForMessageTest(command, "6666");
await waitForMessageTest(command, "6666", "6789");
} finally {
fs.copyFileSync(`${filePath}.bak`, filePath);
fs.unlinkSync(`${filePath}.bak`);
Expand Down

0 comments on commit 9379583

Please sign in to comment.