Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Env vars to override DB connection #748

Merged
merged 5 commits into from
Feb 7, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 3 additions & 14 deletions src/dbos-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,6 @@ export class DBOSExecutor implements DBOSExecutorContext {
static readonly defaultNotificationTimeoutSec = 60;

readonly debugMode: boolean;
readonly debugProxy: string | undefined;
static systemDBSchemaName = 'dbos';

readonly logger: Logger;
Expand All @@ -222,7 +221,6 @@ export class DBOSExecutor implements DBOSExecutorContext {
systemDatabase?: SystemDatabase,
) {
this.debugMode = config.debugMode ?? false;
this.debugProxy = config.debugProxy;

// Set configured environment variables
if (config.env) {
Expand All @@ -247,17 +245,6 @@ export class DBOSExecutor implements DBOSExecutorContext {

if (this.debugMode) {
this.logger.info('Running in debug mode!');
if (this.debugProxy) {
try {
const url = new URL(this.config.debugProxy!);
this.config.poolConfig.host = url.hostname;
this.config.poolConfig.port = parseInt(url.port, 10);
this.logger.info(`Debugging mode proxy: ${this.config.poolConfig.host}:${this.config.poolConfig.port}`);
} catch (err) {
this.logger.error(err);
throw err;
}
}
}

this.procedurePool = new Pool(this.config.poolConfig);
Expand Down Expand Up @@ -416,7 +403,9 @@ export class DBOSExecutor implements DBOSExecutorContext {
this.logger.debug(`Loaded ${length} ORM entities`);
}

await createDBIfDoesNotExist(this.config.poolConfig, this.logger);
if (!this.debugMode) {
await createDBIfDoesNotExist(this.config.poolConfig, this.logger);
}
this.configureDbClient();

if (!this.userDatabase) {
Expand Down
4 changes: 2 additions & 2 deletions src/dbos-runtime/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ program
program
.command('debug')
.description('Debug a workflow')
.option('-x, --proxy <string>', 'Specify the time-travel debug proxy URL for debugging cloud traces')
.option('-x, --proxy <string>', 'Specify the time-travel debug proxy URL for debugging cloud traces (DEPRECATED)')
.requiredOption('-u, --uuid <string>', 'Specify the workflow UUID to replay')
.option('-l, --loglevel <string>', 'Specify log level')
.option('-c, --configfile <string>', 'Specify the config file path (DEPRECATED)')
Expand All @@ -95,7 +95,7 @@ program
.option('--no-app-version', 'ignore DBOS__APPVERSION environment variable')
.action(async (options: DBOSDebugOptions) => {
const [dbosConfig, runtimeConfig]: [DBOSConfig, DBOSRuntimeConfig] = parseConfigFile(options);
await debugWorkflow(dbosConfig, runtimeConfig, options.uuid, options.proxy);
await debugWorkflow(dbosConfig, runtimeConfig, options.uuid);
});

program
Expand Down
30 changes: 19 additions & 11 deletions src/dbos-runtime/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,23 +120,31 @@ export function constructPoolConfig(configFile: ConfigFile, cliOptions?: ParseOp
const databaseConnection = loadDatabaseConnection();
if (!cliOptions?.skipLoggingInParse) {
const logger = new GlobalLogger();
if (configFile['database']['hostname']) {
if (process.env.DBOS_DBHOST) {
logger.info('Loading database connection parameters from debug environment variables');
} else if (configFile.database.hostname) {
logger.info('Loading database connection parameters from dbos-config.yaml');
} else if (databaseConnection['hostname']) {
} else if (databaseConnection.hostname) {
logger.info('Loading database connection parameters from .dbos/db_connection');
} else {
logger.info('Using default database connection parameters');
}
}
configFile['database']['hostname'] =
configFile['database']['hostname'] || databaseConnection['hostname'] || 'localhost';
configFile['database']['port'] = configFile['database']['port'] || databaseConnection['port'] || 5432;
configFile['database']['username'] =
configFile['database']['username'] || databaseConnection['username'] || 'postgres';
configFile['database']['password'] =
configFile['database']['password'] || databaseConnection['password'] || process.env.PGPASSWORD || 'dbos';
configFile['database']['local_suffix'] =
configFile['database']['local_suffix'] || databaseConnection['local_suffix'] || false;
configFile.database.hostname =
process.env.DBOS_DBHOST ?? configFile.database.hostname ?? databaseConnection.hostname ?? 'localhost';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did we change to ?? (nullish coalescing) instead of || (the OR operator)? I think the benefit of || is that if the env variable is set to an empty string, it will consider to be not set. Otherwise, we might get some empty string variables.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, will change it back

const dbos_dbport = process.env.DBOS_DBPORT ? parseInt(process.env.DBOS_DBPORT) : undefined;
configFile.database.port = dbos_dbport ?? configFile.database.port ?? databaseConnection.port ?? 5432;
configFile.database.username =
process.env.DBOS_DBUSER ?? configFile.database.username ?? databaseConnection.username ?? 'postgres';
configFile.database.password =
process.env.DBOS_DBPASSWORD ??
configFile.database.password ??
databaseConnection.password ??
process.env.PGPASSWORD ??
'dbos';
const dbos_dblocalsuffix = process.env.DBOS_DBLOCALSUFFIX ? process.env.DBOS_DBLOCALSUFFIX === 'true' : undefined;
configFile.database.local_suffix =
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note, using || for boolean | undefined values is probably not what we want

dbos_dblocalsuffix ?? configFile.database.local_suffix ?? databaseConnection.local_suffix ?? false;

let databaseName: string | undefined = configFile.database.app_db_name;
if (databaseName === undefined) {
Expand Down
15 changes: 3 additions & 12 deletions src/dbos-runtime/debug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@ import { DBOSFailLoadOperationsError, DBOSInitializationError, DBOSNotRegistered
import { GlobalLogger } from '../telemetry/logs';
import { DBOSRuntime, DBOSRuntimeConfig } from './runtime';

export async function debugWorkflow(
dbosConfig: DBOSConfig,
runtimeConfig: DBOSRuntimeConfig,
workflowUUID: string,
proxy?: string,
) {
dbosConfig = { ...dbosConfig, debugProxy: proxy, debugMode: true };
export async function debugWorkflow(dbosConfig: DBOSConfig, runtimeConfig: DBOSRuntimeConfig, workflowUUID: string) {
dbosConfig = { ...dbosConfig, debugMode: true };
const logger = new GlobalLogger();
try {
const dbosExec = new DBOSExecutor(dbosConfig);
Expand All @@ -31,11 +26,7 @@ export async function debugWorkflow(
for (const err of e.errors) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
if (err.code && err.code === 'ECONNREFUSED') {
if (proxy !== undefined) {
console.error('\x1b[31m%s\x1b[0m', `Is DBOS time-travel debug proxy running at ${proxy} ?`);
} else {
console.error('\x1b[31m%s\x1b[0m', `Is database running at ${dbosConfig.poolConfig.host} ?`);
}
console.error('\x1b[31m%s\x1b[0m', `Is database running at ${dbosConfig.poolConfig.host} ?`);
break;
}
}
Expand Down
3 changes: 2 additions & 1 deletion tsconfig.build.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"extends": "./tsconfig.json",
"include": ["src/**/*", "schemas/**/*"]
"include": ["src/**/*", "schemas/**/*"],
"exclude": ["dist/**"]
}