fix: app fix at initialized state (#2908)

* chore: start app if app is initialized status

* fix: start app at gateway

* fix: test
This commit is contained in:
ChengLei Shao 2023-10-25 08:41:38 +08:00 committed by GitHub
parent 0a3545aabd
commit e2a420afcd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 6 deletions

View File

@ -97,8 +97,7 @@ describe('gateway', () => {
expect(data).toMatchObject({ expect(data).toMatchObject({
error: { error: {
code: 'APP_INITIALIZED', code: 'APP_COMMANDING',
message: errors.APP_INITIALIZED.message({ app: main }),
status: 503, status: 503,
maintaining: true, maintaining: true,
}, },

View File

@ -521,6 +521,7 @@ export class Application<StateT = DefaultState, ContextT = DefaultContext> exten
async stop(options: any = {}) { async stop(options: any = {}) {
this.log.debug('stop app...'); this.log.debug('stop app...');
this.setMaintainingMessage('stopping app...'); this.setMaintainingMessage('stopping app...');
if (this.stopped) { if (this.stopped) {
this.log.warn(`Application ${this.name} already stopped`); this.log.warn(`Application ${this.name} already stopped`);
return; return;

View File

@ -8,6 +8,10 @@ export default (app: Application) => {
.action(async (...cliArgs) => { .action(async (...cliArgs) => {
const [opts] = cliArgs; const [opts] = cliArgs;
if (app.db.closed()) {
await app.db.reconnect();
}
if (opts.quickstart) { if (opts.quickstart) {
if (await app.isInstalled()) { if (await app.isInstalled()) {
app.log.debug('installed....'); app.log.debug('installed....');

View File

@ -13,7 +13,7 @@ import { parse } from 'url';
import xpipe from 'xpipe'; import xpipe from 'xpipe';
import { AppSupervisor } from '../app-supervisor'; import { AppSupervisor } from '../app-supervisor';
import { ApplicationOptions } from '../application'; import { ApplicationOptions } from '../application';
import { PLUGIN_STATICS_PATH, getPackageDirByExposeUrl, getPackageNameByExposeUrl } from '../plugin-manager'; import { getPackageDirByExposeUrl, getPackageNameByExposeUrl, PLUGIN_STATICS_PATH } from '../plugin-manager';
import { applyErrorWithArgs, getErrorWithCode } from './errors'; import { applyErrorWithArgs, getErrorWithCode } from './errors';
import { IPCSocketClient } from './ipc-socket-client'; import { IPCSocketClient } from './ipc-socket-client';
import { IPCSocketServer } from './ipc-socket-server'; import { IPCSocketServer } from './ipc-socket-server';
@ -188,7 +188,7 @@ export class Gateway extends EventEmitter {
void AppSupervisor.getInstance().bootStrapApp(handleApp); void AppSupervisor.getInstance().bootStrapApp(handleApp);
} }
const appStatus = AppSupervisor.getInstance().getAppStatus(handleApp, 'initializing'); let appStatus = AppSupervisor.getInstance().getAppStatus(handleApp, 'initializing');
if (appStatus === 'not_found') { if (appStatus === 'not_found') {
this.responseErrorWithCode('APP_NOT_FOUND', res, { appName: handleApp }); this.responseErrorWithCode('APP_NOT_FOUND', res, { appName: handleApp });
@ -200,6 +200,12 @@ export class Gateway extends EventEmitter {
return; return;
} }
if (appStatus === 'initialized') {
const appInstance = await AppSupervisor.getInstance().getApp(handleApp);
appInstance.runAsCLI(['start'], { from: 'user' });
appStatus = AppSupervisor.getInstance().getAppStatus(handleApp);
}
const app = await AppSupervisor.getInstance().getApp(handleApp); const app = await AppSupervisor.getInstance().getApp(handleApp);
if (appStatus !== 'running') { if (appStatus !== 'running') {

View File

@ -244,6 +244,7 @@ describe('multiple apps', () => {
waitSubAppInstall: true, waitSubAppInstall: true,
}, },
}); });
await AppSupervisor.getInstance().removeApp(subAppName); await AppSupervisor.getInstance().removeApp(subAppName);
await app.start(); await app.start();
@ -266,6 +267,51 @@ describe('multiple apps', () => {
expect(AppSupervisor.getInstance().hasApp(subAppName)).toBeTruthy(); expect(AppSupervisor.getInstance().hasApp(subAppName)).toBeTruthy();
}); });
it('should start automatically with quick start', async () => {
const subAppName = `t_${uid()}`;
const subApp = await app.db.getRepository('applications').create({
values: {
name: subAppName,
options: {
plugins: [],
},
},
context: {
waitSubAppInstall: true,
},
});
await AppSupervisor.getInstance().removeApp(subAppName);
await app.start();
expect(AppSupervisor.getInstance().hasApp(subAppName)).toBeFalsy();
await subApp.update({
options: {
autoStart: true,
},
});
await AppSupervisor.getInstance().removeApp(subAppName);
expect(AppSupervisor.getInstance().hasApp(subAppName)).toBeFalsy();
await app.stop();
await app.db.reconnect();
await AppSupervisor.getInstance().getApp(subAppName, {
upgrading: true,
});
await app.start();
await sleep(1000);
expect(AppSupervisor.getInstance().hasApp(subAppName)).toBeTruthy();
const appStatus = AppSupervisor.getInstance().getAppStatus(subAppName);
expect(appStatus).toEqual('running');
});
it('should get same obj ref when asynchronously access with same sub app name', async () => { it('should get same obj ref when asynchronously access with same sub app name', async () => {
const subAppName = `t_${uid()}`; const subAppName = `t_${uid()}`;

View File

@ -194,6 +194,8 @@ export class PluginMultiAppManager extends Plugin {
appName: string; appName: string;
options: any; options: any;
}) { }) {
const loadButNotStart = options?.upgrading;
const name = appName; const name = appName;
if (appSupervisor.hasApp(name)) { if (appSupervisor.hasApp(name)) {
return; return;
@ -224,7 +226,7 @@ export class PluginMultiAppManager extends Plugin {
}); });
// must skip load on upgrade // must skip load on upgrade
if (!options?.upgrading) { if (!loadButNotStart) {
await subApp.runCommand('start'); await subApp.runCommand('start');
} }
} }
@ -285,7 +287,11 @@ export class PluginMultiAppManager extends Plugin {
for (const subAppInstance of subApps) { for (const subAppInstance of subApps) {
promises.push( promises.push(
(async () => { (async () => {
await AppSupervisor.getInstance().getApp(subAppInstance.name); if (!appSupervisor.hasApp(subAppInstance.name)) {
await AppSupervisor.getInstance().getApp(subAppInstance.name);
} else if (appSupervisor.getAppStatus(subAppInstance.name) === 'initialized') {
(await AppSupervisor.getInstance().getApp(subAppInstance.name)).runCommand('start');
}
})(), })(),
); );
} }