feat: support running single sub app (#1853)

This commit is contained in:
ChengLei Shao 2023-05-14 16:48:53 +08:00 committed by GitHub
parent ff6f37c589
commit 1c757bd025
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 3 deletions

View File

@ -8,9 +8,17 @@ type AppSelector = (req: IncomingMessage) => AppSelectorReturn | Promise<AppSele
export class AppManager { export class AppManager {
public applications: Map<string, Application> = new Map<string, Application>(); public applications: Map<string, Application> = new Map<string, Application>();
public app: Application; public app: Application;
public runningMode: 'single' | 'multiple' = 'multiple';
public singleAppName: string | null = null;
constructor(app: Application) { constructor(app: Application) {
this.bindMainApplication(app); this.bindMainApplication(app);
// if STARTUP_SUBAPP is set, run in single mode
if (process.env.STARTUP_SUBAPP) {
this.singleAppName = process.env.STARTUP_SUBAPP;
this.runningMode = 'single';
}
} }
bindMainApplication(mainApp: Application) { bindMainApplication(mainApp: Application) {
@ -72,7 +80,12 @@ export class AppManager {
return async (req: IncomingMessage, res: ServerResponse) => { return async (req: IncomingMessage, res: ServerResponse) => {
const appManager = this.app.appManager; const appManager = this.app.appManager;
let handleApp: any = (await appManager.appSelector(req)) || appManager.app; let handleApp;
if (this.runningMode === 'single') {
handleApp = this.singleAppName;
} else {
handleApp = (await appManager.appSelector(req)) || appManager.app;
}
if (typeof handleApp === 'string') { if (typeof handleApp === 'string') {
handleApp = await appManager.getApplication(handleApp); handleApp = await appManager.getApplication(handleApp);

View File

@ -177,10 +177,22 @@ export class PluginMultiAppManager extends Plugin {
this.app.on('afterUpgrade', async (app, options) => { this.app.on('afterUpgrade', async (app, options) => {
const cliArgs = options?.cliArgs; const cliArgs = options?.cliArgs;
const repository = this.db.getRepository('applications'); const repository = this.db.getRepository('applications');
const instances = await repository.find(); const findOptions = {};
const appManager = this.app.appManager;
if (appManager.runningMode == 'single') {
findOptions['filter'] = {
name: appManager.singleAppName,
};
}
const instances = await repository.find(findOptions);
for (const instance of instances) { for (const instance of instances) {
const subApp = await this.app.appManager.getApplication(instance.name, { const subApp = await appManager.getApplication(instance.name, {
upgrading: true, upgrading: true,
}); });