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 1 commit
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
Prev Previous commit
Next Next commit
clean up
  • Loading branch information
manojdbos committed Jan 9, 2025
commit ef83cccfdcad75b0b9d7c263650d32f57c7ba7d3
29 changes: 0 additions & 29 deletions src/dbos-executor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ export class DBOSExecutor implements DBOSExecutorContext {
constructor(readonly config: DBOSConfig, systemDatabase?: SystemDatabase) {
this.debugMode = config.debugMode ?? false;
this.debugProxy = config.debugProxy;
console.log("start DBOSExecutor constructor")

// Set configured environment variables
if (config.env) {
Expand Down Expand Up @@ -243,7 +242,6 @@ export class DBOSExecutor implements DBOSExecutorContext {
this.initialized = false;
DBOSExecutor.globalInstance = this;

console.log("exit DBOSExecutor constructor")
}

configureDbClient() {
Expand Down Expand Up @@ -317,29 +315,21 @@ export class DBOSExecutor implements DBOSExecutorContext {

registerClass(className: string) {

console.log("DBOSExecutor Registering class", className)

const cls = getRegisteredClassByName(className);

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

// this.#registerClass(cls);
this.#registerClass(cls.ctor);


// cls.regComplete = true;
}


#registerClass(cls: object) {

console.log("#Registering class", cls)


const registeredClassOperations = getRegisteredOperations(cls);
// console.log("registeredClassOperations", registeredClassOperations)
this.registeredOperations.push(...registeredClassOperations);
for (const ro of registeredClassOperations) {
if (ro.workflowConfig) {
Expand Down Expand Up @@ -369,24 +359,13 @@ export class DBOSExecutor implements DBOSExecutorContext {
}

async init(classes?: object[]): Promise<void> {
console.log("DbosExecutor init")
if (this.initialized) {
/* this.logger.error("Workflow executor already initialized!");
if (!classes || !classes.length) {
classes = getAllRegisteredClasses();
console.log("registered Classes", classes)
}
for (const cls of classes) {
console.log("Registering class", cls)
this.#registerClass(cls);
} */
this.logger.error("Workflow executor already initialized!");
return;
}

if (!classes || !classes.length) {
classes = getAllRegisteredClasses();
console.log("registered Classes", classes)
}

type AnyConstructor = new (...args: unknown[]) => object;
Expand Down Expand Up @@ -418,7 +397,6 @@ export class DBOSExecutor implements DBOSExecutorContext {
}

for (const cls of classes) {
console.log("Registering class", cls)
this.#registerClass(cls);
}

Expand Down Expand Up @@ -541,16 +519,13 @@ export class DBOSExecutor implements DBOSExecutorContext {

#registerWorkflow(ro: MethodRegistrationBase) {

console.log("registering workflow")

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)) {
return
// throw new DBOSError(`Repeated workflow name: ${wfn}`);
}
const workflowInfo: WorkflowRegInfo = {
workflow: wf,
Expand All @@ -563,14 +538,10 @@ export class DBOSExecutor implements DBOSExecutorContext {

#registerTransaction(ro: MethodRegistrationBase) {

console.log("registering transaction")

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}`);
console.log(`Repeated Transaction name: ${tfn}`);
return;
}
const txnInfo: TransactionRegInfo = {
Expand Down
11 changes: 0 additions & 11 deletions src/dbos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,17 +147,14 @@
set(conf, key, newValue);
}

static async register(cls: any) {

Check failure on line 150 in src/dbos.ts

View workflow job for this annotation

GitHub Actions / test / build (18)

Unexpected any. Specify a different type

Check failure on line 150 in src/dbos.ts

View workflow job for this annotation

GitHub Actions / test / build (20)

Unexpected any. Specify a different type

Check failure on line 150 in src/dbos.ts

View workflow job for this annotation

GitHub Actions / test / build (22)

Unexpected any. Specify a different type
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


console.log("DBOS.register class: ", cls.name);

await this.launch();

const executor = DBOSExecutor.globalInstance;

if (executor !== undefined) {
// executor.registerClass(cls.mjclassName);
executor.registerClass(cls.name)

Check failure on line 157 in src/dbos.ts

View workflow job for this annotation

GitHub Actions / test / build (18)

Unsafe argument of type `any` assigned to a parameter of type `string`

Check failure on line 157 in src/dbos.ts

View workflow job for this annotation

GitHub Actions / test / build (18)

Unsafe member access .name on an `any` value

Check failure on line 157 in src/dbos.ts

View workflow job for this annotation

GitHub Actions / test / build (20)

Unsafe argument of type `any` assigned to a parameter of type `string`

Check failure on line 157 in src/dbos.ts

View workflow job for this annotation

GitHub Actions / test / build (20)

Unsafe member access .name on an `any` value

Check failure on line 157 in src/dbos.ts

View workflow job for this annotation

GitHub Actions / test / build (22)

Unsafe argument of type `any` assigned to a parameter of type `string`

Check failure on line 157 in src/dbos.ts

View workflow job for this annotation

GitHub Actions / test / build (22)

Unsafe member access .name on an `any` value
} else {
throw new DBOSExecutorNotInitializedError();
}
Expand All @@ -167,13 +164,9 @@
static async launch(httpApps?: DBOSHttpApps) {
// Do nothing is DBOS is already initialized
if (DBOSExecutor.globalInstance) {
// console.log("DBOS already initialized. But running init again");
// DBOSExecutor.globalInstance.init();
return
}

console.log("Launching DBOS.launch()");

// Initialize the DBOS executor
if (!DBOS.dbosConfig) {
const [dbosConfig, runtimeConfig]: [DBOSConfig, DBOSRuntimeConfig] = parseConfigFile();
Expand Down Expand Up @@ -839,10 +832,6 @@
inDescriptor: TypedPropertyDescriptor<(this: This, ...args: Args) => Promise<Return>>)
{

console.log("mjjjj Transaction decorator called");



const { descriptor, registration } = registerAndWrapContextFreeFunction(target, propertyKey, inDescriptor);
registration.txnConfig = config;

Expand Down
38 changes: 1 addition & 37 deletions src/decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,25 +260,15 @@
export function getRegisteredOperations(target: object): ReadonlyArray<MethodRegistrationBase> {
const registeredOperations: MethodRegistrationBase[] = [];



// const clname = (target as any).mjclassName

// console.log("We are in getRegisteredOperations", clname)


if (typeof target === 'function') { // Constructor case
const classReg = classesByName.get(target.name);
// const classReg = classesByName.get(clname);
console.log("function classReg", classReg)
classReg?.registeredOperations?.forEach((m) =>registeredOperations.push(m));
}
else {
let current: object | undefined = target;
while (current) {
const cname = current.constructor.name;
// const cname = clname;
console.log("not a function classReg", cname, classesByName)
if (classesByName.has(cname)) {
registeredOperations.push(...getRegisteredOperations(current.constructor));
}
Expand Down Expand Up @@ -467,11 +457,7 @@
throw Error("Use of decorator when original method is undefined");
}


console.log("mjjjj We are in registerAndWrapContextFreeFunction", target.constructor.name, target, propertyKey, descriptor);
console.log("generated class name", (target as any).mjclassName);
const registration = getOrCreateMethodRegistration(target, propertyKey, descriptor, false);

return { descriptor, registration };
}

Expand All @@ -491,26 +477,15 @@
}

export function getOrCreateClassRegistration<CT extends { new (...args: unknown[]) : object }>(
ctor: CT, propertyKey?: string | symbol

Check failure on line 480 in src/decorators.ts

View workflow job for this annotation

GitHub Actions / test / build (18)

'propertyKey' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 480 in src/decorators.ts

View workflow job for this annotation

GitHub Actions / test / build (20)

'propertyKey' is defined but never used. Allowed unused args must match /^_/u

Check failure on line 480 in src/decorators.ts

View workflow job for this annotation

GitHub Actions / test / build (22)

'propertyKey' is defined but never used. Allowed unused args must match /^_/u
) {

console.log("We are in getOrCreateClassRegistration", ctor);
// console.log("before number of regis", classesByName.size);
// console.trace();

const name = ctor.name;
// const name = propertyKey ? propertyKey.toString() : ctor.name;
// const name: string = (ctor as any).mjclassName
console.log("name we are using ",name)

if (!classesByName.has(name)) {

console.log("Inserting class registration", name);
classesByName.set(name, new ClassRegistration<CT>(ctor));
console.log("Done Inserting class registration", name);

}

console.log("after number of regis", classesByName.size);
const clsReg: ClassRegistration<AnyConstructor> = classesByName.get(name)!;

if (clsReg.needsInitialized) {
Expand Down Expand Up @@ -623,14 +598,12 @@

export function DefaultArgOptional<T extends { new (...args: unknown[]) : object }>(ctor: T)
{
console.log("We are in DefaultArgOptional", ctor.name);
const clsreg = getOrCreateClassRegistration(ctor);
clsreg.defaultArgRequired = ArgRequiredOptions.OPTIONAL;
}

export function configureInstance<R extends ConfiguredInstance, T extends unknown[]>(cls: new (name:string, ...args: T) => R, name: string, ...args: T) : R
{
console.log("We are in configureInstance", cls.name, name);
const inst = new cls(name, ...args);
const creg = getOrCreateClassRegistration(cls as new(...args: unknown[])=>R);
if (creg.configuredInstances.has(name)) {
Expand Down Expand Up @@ -678,15 +651,6 @@
// eslint-disable-next-line @typescript-eslint/no-explicit-any
inDescriptor: TypedPropertyDescriptor<(this: This, ctx: TransactionContext<any>, ...args: Args) => Promise<Return>>)
{

console.log("mjjjj We are in Transaction decorator", target, propertyKey, inDescriptor);
const xxxName = Reflect.getMetadata('design:type', target)?.name ?? 'UnknownClass';

console.log(`Transaction decorator applied to: ${xxxName}.${propertyKey}`);


const className = (target as any).constructor.mjclassName;
console.log("In Transaction decorator Class Name:", className, propertyKey);
const { descriptor, registration } = registerAndWrapFunction(target, propertyKey, inDescriptor);
registration.txnConfig = config;
return descriptor;
Expand Down
Loading