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

Support for multiple dbos app files in NextJs App #700

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class dbosWorkflowClass {
// This code needs to execute at least once to launch the DBOS runtime
// Do not delete this code
if (process.env.NEXT_PHASE !== "phase-production-build") {
await DBOS.launch();
await DBOS.register(dbosWorkflowClass);
Copy link
Member

Choose a reason for hiding this comment

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

Is there no longer any need for DBOS.launch()?

Copy link
Contributor Author

@manojdbos manojdbos Jan 9, 2025

Choose a reason for hiding this comment

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

Not in NextJs. register calls launch.

But we do need to run the launch method once

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is having classes registered after launch. This is guaranteed to not be 100% working at the moment...


}

// The exported function is the entry point for the workflow
Expand Down
24 changes: 19 additions & 5 deletions src/dbos-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import {
DrizzleUserDatabase,
UserDatabaseClient,
} from './user_database';
import { MethodRegistrationBase, getRegisteredOperations, getOrCreateClassRegistration, MethodRegistration, getRegisteredMethodClassName, getRegisteredMethodName, getConfiguredInstance, ConfiguredInstance, getAllRegisteredClasses } from './decorators';
import { MethodRegistrationBase, getRegisteredOperations, getOrCreateClassRegistration, MethodRegistration, getRegisteredMethodClassName, getRegisteredMethodName, getConfiguredInstance, ConfiguredInstance, getAllRegisteredClasses,getRegisteredClassByName } from './decorators';
import { SpanStatusCode } from '@opentelemetry/api';
import knex, { Knex } from 'knex';
import { DBOSContextImpl, InitContext, runWithWorkflowContext, runWithTransactionContext, runWithStepContext } from './context';
Expand Down Expand Up @@ -314,6 +314,18 @@ export class DBOSExecutor implements DBOSExecutorContext {
}
}

registerClass(className: string) {

const cls = getRegisteredClassByName(className);

if (cls === undefined) {
throw new DBOSError(`Class ${className} not found`);
}

this.#registerClass(cls.ctor);

}

#registerClass(cls: object) {
const registeredClassOperations = getRegisteredOperations(cls);
this.registeredOperations.push(...registeredClassOperations);
Expand Down Expand Up @@ -504,13 +516,14 @@ export class DBOSExecutor implements DBOSExecutorContext {
/* WORKFLOW OPERATIONS */

#registerWorkflow(ro: MethodRegistrationBase) {

const wf = ro.registeredFunction as Workflow<unknown[], unknown>;
if (wf.name === DBOSExecutor.tempWorkflowName) {
throw new DBOSError(`Unexpected use of reserved workflow name: ${wf.name}`);
}
const wfn = ro.className + '.' + ro.name;
if (this.workflowInfoMap.has(wfn)) {
throw new DBOSError(`Repeated workflow name: ${wfn}`);
return
}
const workflowInfo: WorkflowRegInfo = {
workflow: wf,
Expand All @@ -522,11 +535,12 @@ export class DBOSExecutor implements DBOSExecutorContext {
}

#registerTransaction(ro: MethodRegistrationBase) {

const txf = ro.registeredFunction as Transaction<unknown[], unknown>;
const tfn = ro.className + '.' + ro.name;

if (this.transactionInfoMap.has(tfn)) {
throw new DBOSError(`Repeated Transaction name: ${tfn}`);
return;
}
const txnInfo: TransactionRegInfo = {
transaction: txf,
Expand All @@ -541,7 +555,7 @@ export class DBOSExecutor implements DBOSExecutorContext {
const comm = ro.registeredFunction as StepFunction<unknown[], unknown>;
const cfn = ro.className + '.' + ro.name;
if (this.stepInfoMap.has(cfn)) {
throw new DBOSError(`Repeated Commmunicator name: ${cfn}`);
return
}
const stepInfo: StepRegInfo = {
step: comm,
Expand All @@ -557,7 +571,7 @@ export class DBOSExecutor implements DBOSExecutorContext {
const cfn = ro.className + '.' + ro.name;

if (this.procedureInfoMap.has(cfn)) {
throw new DBOSError(`Repeated Procedure name: ${cfn}`);
return
}
const procInfo: ProcedureRegInfo = {
procedure: proc,
Expand Down
20 changes: 19 additions & 1 deletion src/dbos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,27 @@ export class DBOS {
set(conf, key, newValue);
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
static async register(cls: any) {
Copy link
Member

Choose a reason for hiding this comment

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

How do this and launch relate? Does launch do things that this doesn't? When do you need both or just one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Register registers one class.

Launch intializes various things in the runtime like http, loggers, db connections etc. It does have some registration code that needs to be refactored out imho.

Launch needs to be called only once.

Register needs to be called once per file. Register calls launch because someone has to call launch but launch wll run only once

Copy link
Member

Choose a reason for hiding this comment

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

Got it! And is it okay if launch runs before all registers are done?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes. Because we explicitly register again


await this.launch();

const executor = DBOSExecutor.globalInstance;

if (executor !== undefined) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-argument, @typescript-eslint/no-unsafe-member-access
executor.registerClass(cls.name)
} else {
throw new DBOSExecutorNotInitializedError();
}

}

static async launch(httpApps?: DBOSHttpApps) {
// Do nothing is DBOS is already initialized
if (DBOSExecutor.globalInstance) return;
if (DBOSExecutor.globalInstance) {
return
}

// Initialize the DBOS executor
if (!DBOS.dbosConfig) {
Expand Down
8 changes: 4 additions & 4 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -456,23 +456,24 @@ export function registerAndWrapFunctionTakingContext<This, Args extends unknown[
}

const registration = getOrCreateMethodRegistration(target, propertyKey, descriptor, true);

return { descriptor, registration };
}

export function registerAndWrapDBOSFunction<This, Args extends unknown[], Return>(target: object, propertyKey: string, descriptor: TypedPropertyDescriptor<(this: This, ...args: Args) => Promise<Return>>) {
if (!descriptor.value) {
throw Error("Use of decorator when original method is undefined");
}

const registration = getOrCreateMethodRegistration(target, propertyKey, descriptor, false);

return { descriptor, registration };
}

type AnyConstructor = new (...args: unknown[]) => object;
const classesByName: Map<string, ClassRegistration<AnyConstructor> > = new Map();

export function getRegisteredClassByName(name: string) {
return classesByName.get(name);
}

export function getAllRegisteredClasses() {
const ctors: AnyConstructor[] = [];
for (const [_cn, creg] of classesByName) {
Expand All @@ -489,7 +490,6 @@ export function getOrCreateClassRegistration<CT extends { new (...args: unknown[
classesByName.set(name, new ClassRegistration<CT>(ctor));
}
const clsReg: ClassRegistration<AnyConstructor> = classesByName.get(name)!;

if (clsReg.needsInitialized) {
clsReg.name = name;
clsReg.needsInitialized = false;
Expand Down
Loading