fix: load data source when data source load failed (#3793)

This commit is contained in:
ChengLei Shao 2024-03-22 12:20:12 +08:00 committed by GitHub
parent 9354e25d74
commit 4e145a8868
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 42 additions and 5 deletions

View File

@ -14,6 +14,7 @@ export class DataSourceManager {
} }
async add(dataSource: DataSource, options: any = {}) { async add(dataSource: DataSource, options: any = {}) {
console.log('add', dataSource.name, options);
await dataSource.load(options); await dataSource.load(options);
this.dataSources.set(dataSource.name, dataSource); this.dataSources.set(dataSource.name, dataSource);
} }

View File

@ -342,9 +342,10 @@ describe('data source', async () => {
expect(app.dataSourceManager.dataSources.get('mockInstance1')).not.toBeDefined(); expect(app.dataSourceManager.dataSources.get('mockInstance1')).not.toBeDefined();
}); });
class MockCollectionManager extends CollectionManager {}
describe('data source collections', () => { describe('data source collections', () => {
beforeEach(async () => { beforeEach(async () => {
class MockCollectionManager extends CollectionManager {}
class MockDataSource extends DataSource { class MockDataSource extends DataSource {
async load(): Promise<void> { async load(): Promise<void> {
this.collectionManager.defineCollection({ this.collectionManager.defineCollection({
@ -397,6 +398,7 @@ describe('data source', async () => {
const listResp = await app.agent().resource('dataSources').list({ const listResp = await app.agent().resource('dataSources').list({
appends: 'collections', appends: 'collections',
}); });
expect(listResp.status).toBe(200); expect(listResp.status).toBe(200);
const listEnabledResp = await app.agent().resource('dataSources').listEnabled({}); const listEnabledResp = await app.agent().resource('dataSources').listEnabled({});
@ -406,6 +408,41 @@ describe('data source', async () => {
expect(item.collections).toBeDefined(); expect(item.collections).toBeDefined();
}); });
it('should get data source with collections when data source load failed', async () => {
class MockDataSource2 extends DataSource {
async load(): Promise<void> {
throw new Error('load failed');
}
createCollectionManager(options?: any) {
return new MockCollectionManager();
}
}
app.dataSourceManager.factory.register('mock2', MockDataSource2);
await app.db.getRepository('dataSources').create({
values: {
key: 'mockInstance2',
type: 'mock2',
displayName: 'Mock 2',
options: {
password: '123456',
},
},
});
await waitSecond(1000);
const listResp = await app.agent().resource('dataSources').listEnabled({
appends: 'collections',
});
expect(listResp.status).toBe(200);
console.log(listResp.body.data);
});
it('should get collections from datasource', async () => { it('should get collections from datasource', async () => {
const listResp = await app.agent().resource('dataSources').list(); const listResp = await app.agent().resource('dataSources').list();
expect(listResp.status).toBe(200); expect(listResp.status).toBe(200);

View File

@ -159,15 +159,14 @@ export class PluginDataSourceManagerServer extends Plugin {
const dataSource = app.dataSourceManager.dataSources.get(dataSourceModel.get('key')); const dataSource = app.dataSourceManager.dataSources.get(dataSourceModel.get('key'));
const dataSourceStatus = plugin.dataSourceStatus[dataSourceModel.get('key')]; const dataSourceStatus = plugin.dataSourceStatus[dataSourceModel.get('key')];
// @ts-ignore
const isDBInstance = !!dataSource.collectionManager.db;
const item: any = { const item: any = {
key: dataSourceModel.get('key'), key: dataSourceModel.get('key'),
displayName: dataSourceModel.get('displayName'), displayName: dataSourceModel.get('displayName'),
status: dataSourceStatus, status: dataSourceStatus,
type: dataSourceModel.get('type'), type: dataSourceModel.get('type'),
isDBInstance,
// @ts-ignore
isDBInstance: !!dataSource?.collectionManager.db,
}; };
if (dataSourceStatus === 'loading-failed' || dataSourceStatus === 'reloading-failed') { if (dataSourceStatus === 'loading-failed' || dataSourceStatus === 'reloading-failed') {