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

Make it easier to get the global logger #633

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 0 additions & 1 deletion src/dbos-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ export class DBOSExecutor implements DBOSExecutorContext {
typeormEntities: Function[] = [];
drizzleEntities: { [key: string]: object } = {};


eventReceivers: DBOSEventReceiver[] = [];

/* WORKFLOW EXECUTOR LIFE CYCLE MANAGEMENT */
Expand Down
8 changes: 8 additions & 0 deletions src/dbos-runtime/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import path from 'node:path';
import { Server } from 'http';
import { pathToFileURL } from 'url';
import { DBOSScheduler } from '../scheduler/scheduler';
import { GlobalLogger } from '../telemetry/logs';

interface ModuleExports {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -19,6 +20,11 @@ export interface DBOSRuntimeConfig {
}
export const defaultEntryPoint = "dist/operations.js";

export class DBOS {
static globalLogger?: GlobalLogger;
static dbosConfig?: DBOSConfig;
}

export class DBOSRuntime {
private dbosConfig: DBOSConfig;
private dbosExec: DBOSExecutor | null = null;
Expand All @@ -28,6 +34,7 @@ export class DBOSRuntime {
constructor(dbosConfig: DBOSConfig, private readonly runtimeConfig: DBOSRuntimeConfig) {
// Initialize workflow executor.
this.dbosConfig = dbosConfig;
DBOS.dbosConfig = dbosConfig;
}

/**
Expand All @@ -36,6 +43,7 @@ export class DBOSRuntime {
async initAndStart() {
try {
this.dbosExec = new DBOSExecutor(this.dbosConfig);
DBOS.globalLogger = this.dbosExec.logger;
this.dbosExec.logger.debug(`Loading classes from entrypoints ${JSON.stringify(this.runtimeConfig.entrypoints)}`);
await DBOSRuntime.loadClasses(this.runtimeConfig.entrypoints);
await this.dbosExec.init();
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export {

export {
DBOSRuntimeConfig,
DBOS,
} from "./dbos-runtime/runtime";

export {
Expand Down
3 changes: 3 additions & 0 deletions src/testing/testing_runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { get, set } from "lodash";
import { Client } from "pg";
import { DBOSScheduler } from "../scheduler/scheduler";
import { StoredProcedure } from "../procedure";
import { DBOS } from "../dbos-runtime/runtime";

/**
* Create a testing runtime. Warn: this function will drop the existing system DB and create a clean new one. Don't run tests against your production database!
Expand Down Expand Up @@ -98,7 +99,9 @@ export class TestingRuntimeImpl implements TestingRuntime {
*/
async init(userClasses?: object[], testConfig?: DBOSConfig, systemDB?: SystemDatabase) {
const dbosConfig = testConfig ? [testConfig] : parseConfigFile();
DBOS.dbosConfig = dbosConfig[0];
const dbosExec = new DBOSExecutor(dbosConfig[0], systemDB);
DBOS.globalLogger = dbosExec.logger;
await dbosExec.init(userClasses);
this.#server = new DBOSHttpServer(dbosExec);
for (const evtRcvr of dbosExec.eventReceivers) {
Expand Down
2 changes: 2 additions & 0 deletions tests/httpServer/classdec.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
TransactionContext,
WorkflowContext,
TestingRuntime,
DBOS,
} from "../../src";
import { TestKvTable, generateDBOSTestConfig, setUpDBOSTestDb } from "../helpers";
import request from "supertest";
Expand Down Expand Up @@ -133,6 +134,7 @@ describe("httpserver-defsec-tests", () => {
let middlewareCounterG = 0;
const testMiddlewareG: Middleware = async (ctx, next) => {
middlewareCounterG = middlewareCounterG + 1;
expect(DBOS.globalLogger).toBeDefined();
await next();
};

Expand Down