chore(multi-app-manager): add sub app upgrade handler (#2728)

This commit is contained in:
ChengLei Shao 2023-09-27 15:16:15 +08:00 committed by GitHub
parent 4092451bc9
commit f02023db61
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -9,6 +9,52 @@ import { ApplicationModel } from '../server';
export type AppDbCreator = (app: Application, transaction?: Transactionable) => Promise<void>;
export type AppOptionsFactory = (appName: string, mainApp: Application) => any;
export type SubAppUpgradeHandler = (mainApp: Application) => Promise<void>;
const defaultSubAppUpgradeHandle: SubAppUpgradeHandler = async (mainApp: Application) => {
const repository = mainApp.db.getRepository('applications');
const findOptions = {};
const appSupervisor = AppSupervisor.getInstance();
if (appSupervisor.runningMode == 'single') {
findOptions['filter'] = {
name: appSupervisor.singleAppName,
};
}
const instances = await repository.find(findOptions);
for (const instance of instances) {
const instanceOptions = instance.get('options');
// skip standalone deployment application
if (instanceOptions?.standaloneDeployment && appSupervisor.runningMode !== 'single') {
continue;
}
const beforeSubAppStatus = AppSupervisor.getInstance().getAppStatus(instance.name);
const subApp = await appSupervisor.getApp(instance.name, {
upgrading: true,
});
console.log({ beforeSubAppStatus });
try {
mainApp.setMaintainingMessage(`upgrading sub app ${instance.name}...`);
console.log(`${instance.name}: upgrading...`);
await subApp.runAsCLI(['upgrade'], { from: 'user' });
if (!beforeSubAppStatus && AppSupervisor.getInstance().getAppStatus(instance.name) === 'initialized') {
await AppSupervisor.getInstance().removeApp(instance.name);
}
} catch (error) {
console.log(`${instance.name}: upgrade failed`);
mainApp.logger.error(error);
console.error(error);
}
}
};
const defaultDbCreator = async (app: Application) => {
const databaseOptions = app.options.database as any;
@ -72,6 +118,7 @@ const defaultAppOptionsFactory = (appName: string, mainApp: Application) => {
export class PluginMultiAppManager extends Plugin {
appDbCreator: AppDbCreator = defaultDbCreator;
appOptionsFactory: AppOptionsFactory = defaultAppOptionsFactory;
subAppUpgradeHandler: SubAppUpgradeHandler = defaultSubAppUpgradeHandle;
private beforeGetApplicationMutex = new Mutex();
@ -84,6 +131,10 @@ export class PluginMultiAppManager extends Plugin {
return lodash.cloneDeep(lodash.omit(oldConfig, ['migrator']));
}
setSubAppUpgradeHandler(handler: SubAppUpgradeHandler) {
this.subAppUpgradeHandler = handler;
}
setAppOptionsFactory(factory: AppOptionsFactory) {
this.appOptionsFactory = factory;
}
@ -243,50 +294,7 @@ export class PluginMultiAppManager extends Plugin {
});
this.app.on('afterUpgrade', async (app, options) => {
const cliArgs = options?.cliArgs;
const repository = this.db.getRepository('applications');
const findOptions = {};
const appSupervisor = AppSupervisor.getInstance();
if (appSupervisor.runningMode == 'single') {
findOptions['filter'] = {
name: appSupervisor.singleAppName,
};
}
const instances = await repository.find(findOptions);
for (const instance of instances) {
const instanceOptions = instance.get('options');
// skip standalone deployment application
if (instanceOptions?.standaloneDeployment && appSupervisor.runningMode !== 'single') {
continue;
}
const beforeSubAppStatus = AppSupervisor.getInstance().getAppStatus(instance.name);
const subApp = await appSupervisor.getApp(instance.name, {
upgrading: true,
});
console.log({ beforeSubAppStatus });
try {
this.app.setMaintainingMessage(`upgrading sub app ${instance.name}...`);
console.log(`${instance.name}: upgrading...`);
await subApp.runAsCLI(['upgrade'], { from: 'user' });
if (!beforeSubAppStatus && AppSupervisor.getInstance().getAppStatus(instance.name) === 'initialized') {
await AppSupervisor.getInstance().removeApp(instance.name);
}
} catch (error) {
console.log(`${instance.name}: upgrade failed`);
this.app.logger.error(error);
console.error(error);
}
}
await this.subAppUpgradeHandler(app);
});
this.app.resourcer.registerActionHandlers({