Skip to content

Commit

Permalink
fix: don't throw error with partial initialized app (#92)
Browse files Browse the repository at this point in the history
This is for the multiple instances of the app in the same process.
  • Loading branch information
Yehonal authored Jul 12, 2024
1 parent b2e293a commit 0536e83
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
35 changes: 35 additions & 0 deletions app/src/__tests__/app.helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,22 @@ class TestModule1 {}
)
class TestModule2 {}

@Module(
yalcBaseAppModuleMetadataFactory(BrokenModule, 'brokenModule', {
configFactory: () => ({}),
logger: true,
providers: [
{
provide: 'brokenService',
useFactory: () => {
throw new Error('Test');
},
},
],
}),
)
class BrokenModule {}

describe('test standalone app functions', () => {
let mockedModule: DeepMocked<DynamicModule>;
let mockedServiceFunction: jest.Mock<() => string>;
Expand Down Expand Up @@ -135,6 +151,25 @@ describe('test standalone app functions', () => {
await expect(error).not.toBe(null);
});

it('should not trigger an error when the first app cannot be initialized because of an error', async () => {
try {
await new AppBootstrap('brokenModule', BrokenModule).initApp();
} catch (e: any) {
// nothing
}

let error: any = null;
try {
await new AppBootstrap('test2', TestModule2, {}).initApp();
} catch (e: any) {
// eslint-disable-next-line no-console
console.log(e);
error = e;
}

await expect(error).toBe(null);
});

it('should not trigger an error if we bootstrap multiple servers with the skip option', async () => {
await new AppBootstrap('test1', TestModule1, {
skipMultiServerCheck: true,
Expand Down
25 changes: 19 additions & 6 deletions app/src/app-bootstrap-base.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export abstract class BaseAppBootstrap<
protected app?: TAppType;
protected loggerService!: LoggerService;
protected module: Type<any> | DynamicModule;
private isClosed = false;
protected isClosed = false;

constructor(
protected appAlias: string,
Expand Down Expand Up @@ -98,9 +98,8 @@ export abstract class BaseAppBootstrap<
*/
const originalCloseFn = this.app.close.bind(this.app);
this.app.close = async () => {
this.isClosed = true;
const closeRes = await originalCloseFn();
getBootstrappedApps().delete(this);
this.closeCleanup();
return closeRes;
};

Expand All @@ -111,12 +110,27 @@ export abstract class BaseAppBootstrap<
this.app.init = async () => {
this.isClosed = false;
getBootstrappedApps().add(this);
return originalInitFn();
let initRes;
try {
initRes = await originalInitFn();
} catch (error) {
this.closeCleanup();
throw error;
}
return initRes;
};

return this;
}

/**
* This method is used to cleanup the app and remove it from the global set of bootstrapped apps
*/
closeCleanup() {
this.isClosed = true;
getBootstrappedApps().delete(this);
}

isAppClosed() {
return this.isClosed;
}
Expand All @@ -143,8 +157,7 @@ export abstract class BaseAppBootstrap<

await this.app?.close();

getBootstrappedApps().delete(this);
this.isClosed = true;
this.closeCleanup();
}

async cleanup() {
Expand Down
1 change: 1 addition & 0 deletions app/src/app-bootstrap-standalone.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class StandaloneAppBootstrap<
logger: getEnvLoggerLevels(),
});
} catch (err) {
this.closeCleanup();
// eslint-disable-next-line no-console
console.error(clc.red('Failed to create app'), clc.red(err));
throw new Error('Process aborted');
Expand Down
10 changes: 8 additions & 2 deletions app/src/app-bootstrap.helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,14 @@ export class AppBootstrap<
createOptions?: ICreateOptions;
fastifyInstance?: FastifyInstance;
}) {
await this.applyBootstrapGlobals(options?.createOptions);
try {
await this.applyBootstrapGlobals(options?.createOptions);

await this.getApp().init();
await this.getApp().init();
} catch (err) {
this.closeCleanup();
throw new Error('Process aborted');
}

if (envIsTrue(process.env.APP_DRY_RUN) === true) {
this.loggerService?.log('Dry run, exiting...');
Expand Down Expand Up @@ -117,6 +122,7 @@ export class AppBootstrap<
},
);
} catch (err) {
this.closeCleanup();
// eslint-disable-next-line no-console
console.error(clc.red('Failed to create app'), clc.red(err));
throw new Error('Process aborted');
Expand Down

0 comments on commit 0536e83

Please sign in to comment.