chore(multi-app-manager): add sub app upgrade handler (#2728)
This commit is contained in:
parent
4092451bc9
commit
f02023db61
@ -9,6 +9,52 @@ import { ApplicationModel } from '../server';
|
|||||||
|
|
||||||
export type AppDbCreator = (app: Application, transaction?: Transactionable) => Promise<void>;
|
export type AppDbCreator = (app: Application, transaction?: Transactionable) => Promise<void>;
|
||||||
export type AppOptionsFactory = (appName: string, mainApp: Application) => any;
|
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 defaultDbCreator = async (app: Application) => {
|
||||||
const databaseOptions = app.options.database as any;
|
const databaseOptions = app.options.database as any;
|
||||||
@ -72,6 +118,7 @@ const defaultAppOptionsFactory = (appName: string, mainApp: Application) => {
|
|||||||
export class PluginMultiAppManager extends Plugin {
|
export class PluginMultiAppManager extends Plugin {
|
||||||
appDbCreator: AppDbCreator = defaultDbCreator;
|
appDbCreator: AppDbCreator = defaultDbCreator;
|
||||||
appOptionsFactory: AppOptionsFactory = defaultAppOptionsFactory;
|
appOptionsFactory: AppOptionsFactory = defaultAppOptionsFactory;
|
||||||
|
subAppUpgradeHandler: SubAppUpgradeHandler = defaultSubAppUpgradeHandle;
|
||||||
|
|
||||||
private beforeGetApplicationMutex = new Mutex();
|
private beforeGetApplicationMutex = new Mutex();
|
||||||
|
|
||||||
@ -84,6 +131,10 @@ export class PluginMultiAppManager extends Plugin {
|
|||||||
return lodash.cloneDeep(lodash.omit(oldConfig, ['migrator']));
|
return lodash.cloneDeep(lodash.omit(oldConfig, ['migrator']));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setSubAppUpgradeHandler(handler: SubAppUpgradeHandler) {
|
||||||
|
this.subAppUpgradeHandler = handler;
|
||||||
|
}
|
||||||
|
|
||||||
setAppOptionsFactory(factory: AppOptionsFactory) {
|
setAppOptionsFactory(factory: AppOptionsFactory) {
|
||||||
this.appOptionsFactory = factory;
|
this.appOptionsFactory = factory;
|
||||||
}
|
}
|
||||||
@ -243,50 +294,7 @@ export class PluginMultiAppManager extends Plugin {
|
|||||||
});
|
});
|
||||||
|
|
||||||
this.app.on('afterUpgrade', async (app, options) => {
|
this.app.on('afterUpgrade', async (app, options) => {
|
||||||
const cliArgs = options?.cliArgs;
|
await this.subAppUpgradeHandler(app);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
this.app.resourcer.registerActionHandlers({
|
this.app.resourcer.registerActionHandlers({
|
||||||
|
Loading…
Reference in New Issue
Block a user